69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
# Common imports
|
|
import numpy as np
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.tree import export_graphviz
|
|
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
|
from sklearn.compose import ColumnTransformer
|
|
from IPython.display import Image
|
|
from pydot import graph_from_dot_data
|
|
import os
|
|
|
|
# Where to save the figures and data files
|
|
PROJECT_ROOT_DIR = "Results"
|
|
FIGURE_ID = "Results/FigureFiles"
|
|
DATA_ID = "DataFiles/"
|
|
|
|
if not os.path.exists(PROJECT_ROOT_DIR):
|
|
os.mkdir(PROJECT_ROOT_DIR)
|
|
|
|
if not os.path.exists(FIGURE_ID):
|
|
os.makedirs(FIGURE_ID)
|
|
|
|
if not os.path.exists(DATA_ID):
|
|
os.makedirs(DATA_ID)
|
|
|
|
def image_path(fig_id):
|
|
return os.path.join(FIGURE_ID, fig_id)
|
|
|
|
def data_path(dat_id):
|
|
return os.path.join(DATA_ID, dat_id)
|
|
|
|
def save_fig(fig_id):
|
|
plt.savefig(image_path(fig_id) + ".png", format='png')
|
|
|
|
infile = open(data_path("grades.csv"),'r')
|
|
|
|
# Read the experimental data with Pandas
|
|
from IPython.display import display
|
|
grades = pd.read_csv(infile,names = ('Trend','Sleep','Studied','Grade'))
|
|
grades = pd.DataFrame(grades)
|
|
|
|
# Features and targets
|
|
X = grades.loc[:, grades.columns != 'Grade'].values
|
|
y = grades.loc[:, grades.columns == 'Grade'].values
|
|
|
|
# Create the encoder.
|
|
encoder = OneHotEncoder(handle_unknown="ignore")
|
|
# Assume for simplicity all features are categorical.
|
|
encoder.fit(X)
|
|
# Apply the encoder.
|
|
X = encoder.transform(X)
|
|
print(X)
|
|
# Then do a Classification tree
|
|
tree_clf = DecisionTreeClassifier(max_depth=2)
|
|
tree_clf.fit(X, y)
|
|
print("Train set accuracy with Decision Tree: {:.2f}".format(tree_clf.score(X,y)))
|
|
#transfer to a decision tree graph
|
|
export_graphviz(
|
|
tree_clf,
|
|
out_file="DataFiles/grade.dot",
|
|
rounded=True,
|
|
filled=True
|
|
)
|
|
cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/grades.png'
|
|
os.system(cmd)
|
|
|