58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
import os
|
|
from sklearn.datasets import load_breast_cancer
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.metrics import confusion_matrix
|
|
from sklearn.tree import export_graphviz
|
|
|
|
from IPython.display import Image
|
|
from pydot import graph_from_dot_data
|
|
import pandas as pd
|
|
import numpy as np
|
|
|
|
|
|
# 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')
|
|
|
|
|
|
cancer = load_breast_cancer()
|
|
X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
|
print(X)
|
|
y = pd.Categorical.from_codes(cancer.target, cancer.target_names)
|
|
y = pd.get_dummies(y)
|
|
print(y)
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
|
|
tree_clf = DecisionTreeClassifier(max_depth=5)
|
|
tree_clf.fit(X_train, y_train)
|
|
|
|
export_graphviz(
|
|
tree_clf,
|
|
out_file="DataFiles/cancer.dot",
|
|
feature_names=cancer.feature_names,
|
|
class_names=cancer.target_names,
|
|
rounded=True,
|
|
filled=True
|
|
)
|
|
cmd = 'dot -Tpng DataFiles/cancer.dot -o DataFiles/cancer.png'
|
|
os.system(cmd)
|