Skip to content
Snippets Groups Projects
Commit 94ce96b6 authored by Maxime BURON's avatar Maxime BURON
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
mar.net 0 → 100644
net {
name = mar;
software = "aGrUM 1.17.1";
node_size = (50 50);
}
node A {
states = (0 1 );
label = "A";
ID = "A";
}
node B {
states = (0 1 );
label = "B";
ID = "B";
}
node C {
states = (0 1 );
label = "C";
ID = "C";
}
node Ia {
states = (0 1 );
label = "Ia";
ID = "Ia";
}
node Ib {
states = (0 1 );
label = "Ib";
ID = "Ib";
}
node Ic {
states = (0 1 );
label = "Ic";
ID = "Ic";
}
potential (A) {
data = ( 0.561407 0.438593);
}
potential ( B | A ) {
data =
(( 0.515132 0.484868) % A=0
( 0.371633 0.628367)); % A=1
}
potential ( C | B ) {
data =
(( 0.515132 0.484868) % B=0
( 0.371633 0.628367)); % B=1
}
potential (Ia) {
data = ( 0.8 0.2);
}
potential ( Ib | A ) {
data =
(( 0.664707 0.335293) % A=0
( 0.344864 0.655136)); % A=1
}
potential ( Ic | B ) {
data =
(( 0.664707 0.335293) % B=0
( 0.344864 0.655136)); % B=1
}
mcc.py 0 → 100755
#!.venv/bin/python3
import pyAgrum as gum
import psycopg2
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("BNFile", help="Bayesian network file")
parser.add_argument("DBSize", help="size of the database")
args = parser.parse_args()
# inputs
bn_file = args.BNFile
db_size = int(args.DBSize)
out_file = "mcc.csv"
table_name = "test"
# loading the BN file
bn = gum.loadBN(bn_file)
var_names = sorted(list(bn.names()))
# generating the CSV file
g=gum.BNDatabaseGenerator(bn)
g.drawSamples(db_size)
g.toCSV(out_file)
# PG connection
conn = psycopg2.connect(database="mcc",
user='postgres', password='postgres',
host='127.0.0.1', port='5432')
conn.autocommit = True
cursor = conn.cursor()
# deleting the existing table
deletion_sql = "DROP TABLE IF EXISTS {};".format(table_name)
cursor.execute(deletion_sql)
# creating the table to store the
col_def = map(lambda name : "{} int NOT NULL".format(name), var_names)
creation_sql = "CREATE TABLE {}({});".format(table_name, ",".join(col_def))
print(creation_sql)
cursor.execute(creation_sql)
# loading the CSV data to the table
with open(out_file, "r") as file:
next(file) # skip the header's line
cursor.copy_from(file, table_name, sep=",", null="")
missing_selects = []
for v in var_names:
if not v.lower().startswith('i') and v[1:] not in var_names:
missing_selects.append("case when i{}=1 then NULL ELSE {} END".format(v,v))
elif not v.lower().startswith('i'):
missing_selects.append(v)
missing_data_table = "{}_star".format(table_name)
deletion_sql = "DROP TABLE IF EXISTS {};".format(missing_data_table)
cursor.execute(deletion_sql)
missing_data_sql = "CREATE TABLE {} AS SELECT {} FROM {}".format(missing_data_table, ",".join(missing_selects), table_name)
print(missing_data_sql)
cursor.execute(missing_data_sql)
###### Imputation
import numpy as np
import pandas as pd
from sklearn.impute import KNNImputer
cursor.execute("SELECT * FROM {}".format(missing_data_table))
tuples_list = cursor.fetchall()
df = pd.DataFrame(tuples_list)
imputer = KNNImputer(n_neighbors=2)
impute = imputer.fit_transform(df)
print(impute)
test.net 0 → 100644
net {
name = unnamedBN;
software = "aGrUM 1.17.1";
node_size = (50 50);
}
node B {
states = (0 1 );
label = "B";
ID = "B";
}
node A {
states = (0 1 );
label = "A";
ID = "A";
}
node Ia {
states = (0 1 );
label = "Ia";
ID = "Ia";
}
node Ib {
states = (0 1 );
label = "Ib";
ID = "Ib";
}
potential ( B | A ) {
data =
(( 0.515132 0.484868) % A=0
( 0.371633 0.628367)); % A=1
}
potential (A) {
data = ( 0.561407 0.438593);
}
potential (Ib) {
data = ( 0.8 0.2);
}
potential ( Ia | B ) {
data =
(( 0.664707 0.335293) % B=0
( 0.344864 0.655136)); % B=1
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment