<jupyter_start><jupyter_text>Breast Test<jupyter_code>cancer_df = pd.read_csv("breast-w.csv", na_values = '?')
cancer_df = cancer_df.replace({'Class': {'benign': 0, 'malignant': 1}})
cancer_df = cancer_df.dropna()
cancer_df.describe()<jupyter_output><empty_output><jupyter_text>One-level decision tree (max_depth = 1)<jupyter_code>model = DecisionTreeClassifier(criterion="entropy", max_depth=1)

cols = list(cancer_df.columns)
x = cancer_df[cols[:-1]]
y = cancer_df[cols[-1]]
model.fit(x, y)
scores_one = cross_val_score(model, x, y, cv = 10)
scores_one = np.mean(scores_one)
scores_one
dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = cols[:-1],
                class_names=['benign','malignant'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('pic/breast_1.png')
# Image(graph.create_png())
Image(graph.create_png())<jupyter_output><empty_output><jupyter_text>Multi-level decision tree (max_depth = none)<jupyter_code>model = DecisionTreeClassifier(criterion="entropy", max_depth=None)

cols = list(cancer_df.columns)
x = cancer_df[cols[:-1]]
y = cancer_df[cols[-1]]
list_scores_multi = []
for i in range(100):
    model.fit(x, y)
    scores_multi = cross_val_score(model, x, y, cv = 10)
    scores_multi = np.mean(scores_multi)
    list_scores_multi.append(scores_multi)
    
sns.distplot(list_scores_multi)
# The rate increase by multi-level decision tree
rate = np.mean(list_scores_multi) - scores_one
rate
dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = cols[:-1],
                class_names=['benign','malignant'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('pic/breast_2.png')
# Image(graph.create_png())
Image(graph.create_png())
min_size = min(cancer_df['Class'].value_counts()[0], cancer_df['Class'].value_counts()[1])
cols = list(cancer_df.columns)
x = cancer_df[cols[:-1]]
y = cancer_df[cols[-1]]

x_axis = []
list_cv_accuracy = []
list_train_acc = []
for i in range(1, 100 , 1):
    model = DecisionTreeClassifier(criterion="entropy", max_depth=None, min_samples_leaf = i)
    model.fit(x, y)
    scores = cross_val_score(model, x, y, cv = 20, scoring='accuracy')
    list_cv_accuracy.append(scores.mean())
    
    y_pred = model.predict(x)
    training_acc = metrics.accuracy_score(y, y_pred)
    list_train_acc.append(training_acc)
    x_axis.append(i)
plt.figure(figsize=(10,5))  
plt.plot(x_axis, list_cv_accuracy, label='10 CV')
plt.plot(x_axis, list_train_acc, label='Training Set')
plt.title("The accuracy of Breast Test with variation of min_samples_leaf ")
plt.xlabel("Num. of min_samples_leaf")
plt.ylabel("Accuracy")
plt.legend()<jupyter_output><empty_output><jupyter_text>From above pictures we can tell that once x over 200, the model becomes underfitting.   Diabetes Test<jupyter_code>diabetes_df = pd.read_csv("diabetes.csv", na_values = '?')
diabetes_df = diabetes_df.replace({'Class': {'tested_positive': 0, 'tested_negative': 1}})
diabetes_df = diabetes_df.dropna()
diabetes_df.describe()<jupyter_output><empty_output><jupyter_text>One-level decision tree (max_depth = 1)<jupyter_code>model = DecisionTreeClassifier(criterion="entropy", max_depth=1)

cols = list(diabetes_df.columns)
x = diabetes_df[cols[:-1]]
y = diabetes_df[cols[-1]]
model.fit(x, y)
scores_one = cross_val_score(model, x, y, cv = 10)
scores_one = np.mean(scores_one)
scores_one<jupyter_output><empty_output><jupyter_text>Multi-level decision tree (max_depth = none)<jupyter_code>model = DecisionTreeClassifier(criterion="entropy", max_depth=None)

list_scores_multi = []
for i in range(100):
    model.fit(x, y)
    scores_multi = cross_val_score(model, x, y, cv = 10)
    scores_multi = np.mean(scores_multi)
    list_scores_multi.append(scores_multi)
    
# The rate increase by multi-level decision tree
rate = np.mean(list_scores_multi) - scores_one
rate
min_size = min(diabetes_df['class'].value_counts()[0], diabetes_df['class'].value_counts()[1])
list_accuracy = []
list_train_acc = []
x_axis = []
for i in range(1, min_size , 3):
    model = DecisionTreeClassifier(criterion="entropy", max_depth=None, min_samples_leaf = i)
    model.fit(x, y)
    scores = cross_val_score(model, x, y, cv = 10)
    scores = np.mean(scores)
    list_accuracy.append(scores)
    y_pred = model.predict(x)
    training_acc = metrics.accuracy_score(y, y_pred)    
    list_train_acc.append(training_acc)
    x_axis.append(i)
plt.figure(figsize=(10,5))    
plt.plot(x_axis, list_accuracy, label='10 CV')
plt.plot(x_axis, list_train_acc, label='Training Set')
plt.title("The accuracy of Diabete Test with variation of min_samples_leaf")
plt.xlabel("Num. of min_samples_leaf")
plt.ylabel("Accuracy")
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13969991889699918, 'fraction_numerical': 0.012570965125709651, 'mean_word_length': 5.748290013679891, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '242328', 'license': 'MIT', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1790, 'n_words': 310, 'path': 'Lab1/DecisionTree_Lab1.ipynb', 'repo_name': 'A2Zntu/ML_HW', 'size': '4932'}