-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (52 loc) · 2.24 KB
/
main.py
File metadata and controls
67 lines (52 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
Program Developed by: Kristian Vazquez
'''
# Importing necessary libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from mlxtend.frequent_patterns import apriori, association_rules
# Load the dataset
crohn_patients = pd.read_csv('./data/crohn_patients.csv')
# Making sure the data is printed correctly
print(crohn_patients)
print(crohn_patients.columns)
# Split the "Preferred Foods" column into lists of foods
crohn_patients['Preferred Foods'] = crohn_patients['Preferred Foods'].str.split(', ')
# Apply one-hot encoding to categorical variables
encoded_df = pd.get_dummies(crohn_patients, columns=['Gender', "Crohn's Disease Severity", 'Medication'])
# Split the "Preferred Foods" column into lists of foods
encoded_df['Preferred Foods'] = encoded_df['Preferred Foods'].str.split(', ')
# Apply one-hot encoding to the "Preferred Foods" column
encoded_df = encoded_df.explode('Preferred Foods')
encoded_df = pd.get_dummies(encoded_df, columns=['Preferred Foods'])
# Drop unnecessary columns
encoded_df.drop(columns=['Patient ID', 'Age'], inplace=True)
# Apriori algorithm to find frequent itemsets
frequent_itemsets = apriori(encoded_df, min_support=0.1, use_colnames=True)
# Generate assocaition rules
association_rules_df = association_rules(frequent_itemsets, metric='lift', min_threshold=1.0)
# Filter the rules based on confidence and support
filtered_rules = association_rules_df[(association_rules_df['confidence'] > 0.6) & (association_rules_df['support'] > 0.1)]
# Print the filtered rules
print(filtered_rules)
# Scatter Plot
plt.figure(figsize=(10, 6))
plt.scatter(filtered_rules['support'], filtered_rules['confidence'], alpha=0.5)
plt.xlabel('Support')
plt.ylabel('Confidence')
plt.title('Association Rules Scatter Plot')
plt.grid(True)
plt.tight_layout()
plt.show()
# Netowrk Plot
plt.figure(figsize=(10, 6))
G = nx.Graph()
for i, row in filtered_rules.iterrows():
G.add_edge(row['antecedents'], row['consequents'], weight=row['lift'])
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_size=2000, node_color='skyblue', font_size=10, font_weight='bold', edge_color='gray', width=filtered_rules['lift']*0.1)
plt.title('Association Rules Netowrk Plot')
plt.tight_layout()
plt.show()