|
import numpy as np
|
|
import pandas as pd
|
|
import pyAgrum as gum
|
|
from sklearn.preprocessing import StandardScaler
|
|
|
|
|
|
data = pd.read_excel('./Final_BN_0323.xlsx')
|
|
data.columns = data.columns.astype(str)
|
|
|
|
for column in data.columns:
|
|
print(column)
|
|
|
|
|
|
|
|
|
|
data = data.replace(' ', -1)
|
|
|
|
for col in data.columns:
|
|
if data[col].dtype == np.float64:
|
|
print(f'{col} is numerical')
|
|
if col in ['satisfaction_infrastructure', 'satisfaction_facility', 'satisfaction_nature', 'satisfaction_service']:
|
|
scaler = StandardScaler()
|
|
|
|
mask = data[col] != -1
|
|
|
|
|
|
data.loc[mask, col] = scaler.fit_transform(
|
|
data.loc[mask, col].values.reshape(-1, 1)
|
|
)
|
|
|
|
data[col][data[col] != -1] = pd.cut(data[col][data[col] != -1], bins=5, labels=False)
|
|
data[col] = data[col].fillna(-1).astype(int)
|
|
else:
|
|
print(f'{col} is categorical')
|
|
|
|
|
|
data[col] = data[col].astype('category')
|
|
|
|
data[col] = data[col].cat.codes
|
|
|
|
print(data.columns)
|
|
|
|
for column in data.columns:
|
|
print(f'{column}: {data[column].unique()}')
|
|
|
|
|
|
|
|
structure1 = [
|
|
|
|
|
|
('gender', 'travel motivation'),
|
|
('residential status', 'travel motivation'),
|
|
('residential status', 'duration'),
|
|
('residential status', 'travel group'),
|
|
('nationality', 'travel motivation'),
|
|
('nationality', 'duration'),
|
|
('nationality', 'travel group'),
|
|
|
|
|
|
('travel motivation', 'Satisfaction_nature'),
|
|
('duration', 'Satisfaction_service'),
|
|
('duration', 'Satisfaction_nature'),
|
|
('travel group', 'Satisfaction_nature'),
|
|
|
|
|
|
|
|
('nature_based_activity', 'Satisfaction_service'),
|
|
('nature_based_activity', 'Satisfaction_nature'),
|
|
('facility_based_activity', 'Satisfaction_service'),
|
|
('facility_based_activity', 'Satisfaction_nature'),
|
|
('human_in_landmark', 'Satisfaction_service'),
|
|
('human_in_landmark', 'Satisfaction_nature'),
|
|
|
|
|
|
|
|
('importance to infrastructure', 'Satisfaction_infrastructure'),
|
|
('importance to service', 'Satisfaction_service'),
|
|
('importance to nature', 'Satisfaction_service'),
|
|
|
|
|
|
|
|
('Satisfaction_infrastructure', 'overall satisfaction level'),
|
|
('Satisfaction_service', 'overall satisfaction level'),
|
|
('Satisfaction_nature', 'overall satisfaction level')
|
|
]
|
|
|
|
structure = [
|
|
|
|
('Year', 'gender'),
|
|
('Year', 'age'),
|
|
('Year', 'residential status'),
|
|
('Year', 'nationality'),
|
|
('Year', 'educational level'),
|
|
('Year', 'employment status'),
|
|
('Year', 'income level'),
|
|
('Year', 'preferred language'),
|
|
|
|
|
|
('park', 'gender'),
|
|
('park', 'age'),
|
|
('park', 'residential status'),
|
|
('park', 'nationality'),
|
|
('park', 'educational level'),
|
|
('park', 'employment status'),
|
|
('park', 'income level'),
|
|
('park', 'preferred language'),
|
|
|
|
|
|
('gender', 'satisfaction_infrastructure'),
|
|
('gender', 'satisfaction_service'),
|
|
('gender', 'satisfaction_nature'),
|
|
('gender', 'satisfaction_facility'),
|
|
('age', 'satisfaction_infrastructure'),
|
|
('age', 'satisfaction_service'),
|
|
('age', 'satisfaction_nature'),
|
|
('age', 'satisfaction_facility'),
|
|
('residential status', 'satisfaction_service'),
|
|
('residential status', 'satisfaction_nature'),
|
|
('residential status', 'satisfaction_facility'),
|
|
('nationality', 'satisfaction_infrastructure'),
|
|
('nationality', 'satisfaction_service'),
|
|
('nationality', 'satisfaction_nature'),
|
|
('educational level', 'satisfaction_service'),
|
|
('educational level', 'satisfaction_facility'),
|
|
('employment status', 'satisfaction_nature'),
|
|
('income level', 'satisfaction_infrastructure'),
|
|
('income level', 'satisfaction_service'),
|
|
('income level', 'satisfaction_facility'),
|
|
('preferred language', 'satisfaction_infrastructure'),
|
|
('preferred language', 'satisfaction_service'),
|
|
('preferred language', 'satisfaction_nature'),
|
|
('preferred language', 'satisfaction_facility'),
|
|
|
|
|
|
('source of knowing', 'satisfaction_service'),
|
|
('travel motivation', 'satisfaction_service'),
|
|
('means of transportation', 'satisfaction_service'),
|
|
('means of transportation', 'satisfaction_nature'),
|
|
('duration', 'satisfaction_infrastructure'),
|
|
('duration', 'satisfaction_service'),
|
|
('duration', 'satisfaction_nature'),
|
|
('travel group', 'satisfaction_infrastructure'),
|
|
('travel group', 'satisfaction_facility'),
|
|
|
|
|
|
('nature_based_activity', 'satisfaction_infrastructure'),
|
|
('nature_based_activity', 'satisfaction_service'),
|
|
('nature_based_activity', 'satisfaction_facility'),
|
|
('facility_based_activity', 'satisfaction_nature'),
|
|
('human_in_landmark', 'satisfaction_nature'),
|
|
('human_in_landmark', 'satisfaction_facility'),
|
|
|
|
|
|
('importance_infrastructure', 'satisfaction_infrastructure'),
|
|
('importance_service', 'satisfaction_service'),
|
|
('importance_nature', 'satisfaction_nature'),
|
|
('importance_facility', 'satisfaction_facility'),
|
|
|
|
|
|
('satisfaction_infrastructure', 'overall satisfaction level'),
|
|
('satisfaction_service', 'overall satisfaction level'),
|
|
('satisfaction_nature', 'overall satisfaction level'),
|
|
('satisfaction_facility', 'overall satisfaction level'),
|
|
|
|
|
|
('overall satisfaction level', 'Intention to return'),
|
|
('overall satisfaction level', 'Intention to recommend'),
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
print(len(structure))
|
|
|
|
nodes = set()
|
|
|
|
for edge in structure:
|
|
nodes.add(edge[0])
|
|
nodes.add(edge[1])
|
|
|
|
data_subset = data[list(nodes)]
|
|
|
|
|
|
data_path = './data.csv'
|
|
data_subset.to_csv(data_path, index=False)
|
|
|
|
df = pd.read_csv(data_path)
|
|
|
|
unique_labels = {col: [int(i) for i in df[col].dropna().unique() if i != -1] for col in df.columns}
|
|
|
|
|
|
bn = gum.BayesNet("MyBN")
|
|
|
|
|
|
|
|
for var, labs in unique_labels.items():
|
|
|
|
|
|
|
|
var_id = bn.add(gum.IntegerVariable(var, var, labs))
|
|
|
|
print(111111)
|
|
|
|
|
|
cnt = 0
|
|
for parent, child in structure:
|
|
bn.addArc(bn.idFromName(parent), bn.idFromName(child))
|
|
cnt += 1
|
|
print(cnt)
|
|
|
|
print(111111)
|
|
|
|
|
|
learner = gum.BNLearner(data_path, bn, ["-1"])
|
|
learner.setVerbosity(True)
|
|
learner.setNumberOfThreads(20)
|
|
learner.setMaxTime(604800)
|
|
learner.useEM(1e-3)
|
|
learner.useSmoothingPrior()
|
|
print(learner)
|
|
bn = learner.learnParameters(bn.dag())
|
|
print(f"# iterations : {learner.nbrIterations()}")
|
|
|
|
gum.saveBN(bn, "bn_final.bif", allowModificationWhenSaving=True)
|
|
|
|
import pyAgrum.lib.image as gumimage
|
|
gumimage.exportInference(bn, "./bn_final.png")
|
|
|
|
print(learner.history())
|
|
|
|
a = 1
|
|
|