<jupyter_start><jupyter_text>1.Code for question 2c<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import graphviz
from sklearn import preprocessing

df = pd.read_csv('Q2.csv')
X = df[['A', 'B', 'C']].values
Y = df['Y'].values

clf = DecisionTreeClassifier()
clf.set_params(criterion='entropy')
print(clf.get_params())
clf.fit(X, Y)
dot_data = tree.export_graphviz(clf, out_file=None) 
graph = graphviz.Source(dot_data) 
graph.render("Q2") 
graph<jupyter_output><empty_output><jupyter_text>2.Code for question 3 First read the data from the file wdbc.data, then check on the data content and statistical properties.<jupyter_code>df=pd.read_csv("wdbc.data")
df.isnull().any()
df.dtypes
print(df.head(10))<jupyter_output>ID Diagnosis  Radius(Mean)  Texture  Perimeter    Area  Smoothness  \
0    842302         M         17.99    10.38     122.80  1001.0     0.11840   
1    842517         M         20.57    17.77     132.90  1326.0     0.08474   
2  84300903         M         19.69    21.25     130.00  1203.0     0.10960   
3  84348301         M         11.42    20.38      77.58   386.1     0.14250   
4  84358402         M         20.29    14.34     135.10  1297.0     0.10030   
5    843786         M         12.45    15.70      82.57   477.1     0.12780   
6    844359         M         18.25    19.98     119.60  1040.0     0.09463   
7  84458202         M         13.71    20.83      90.20   577.9     0.11890   
8    844981         M         13.00    21.82      87.50   519.8     0.12730   
9  84501001         M         12.46    24.04      83.97   475.9     0.11860   

   Compactness  Concavity  Concave points  ...  Radius(Mean of 3 largest)  \
0      0.27760    0.30010         0.14710  ...       [...]<jupyter_text>Next,import sklearn.metrics for the evaluation of DT models<jupyter_code>import sklearn.metrics as skm<jupyter_output><empty_output><jupyter_text>(1)Evaluation for DT1<jupyter_code>X = df[['Radius(Mean)','Texture','Perimeter','Area','Smoothness','Compactness','Concavity','Concave points','Symmetry','Fractal Dimension','Radius(SE)','Texture','Perimeter','Area','Smoothness','Compactness','Concavity','Concave points','Symmetry','Fractal Dimension','Radius(Mean of 3 largest)','Texture','Perimeter','Area','Smoothness','Compactness','Concavity','Concave points','Symmetry','Fractal Dimension']].values
Y = df['Diagnosis'].values
scores_train=np.zeros(20)
scores_test=np.zeros(20)
precision_B=np.zeros(20)
precision_M=np.zeros(20)
recall_B=np.zeros(20)
recall_M=np.zeros(20)
depth=np.zeros(20)
numleaf=np.zeros(20)
clf = DecisionTreeClassifier()
clf.set_params(criterion='entropy')
for i in range(20):
    (X_train,X_test,Y_train,Y_test)=train_test_split(X, Y, test_size=0.7)
    clf.fit(X_train, Y_train)
    leaves=clf.predict(X_test)
    scores_train[i]=clf.score(X_train,Y_train)
    scores_test[i]=clf.score(X_test, Y_test)
    precision=skm.precision_score(Y_test,leaves,average=None)
    recall=skm.recall_score(Y_test,leaves,average=None)
    depth[i]=clf.get_depth()
    numleaf[i]=clf.get_n_leaves()
    precision_B[i]=precision[0]
    precision_M[i]=precision[1]
    recall_B[i]=recall[0]
    recall_M[i]=recall[1]
print("Accuracy_train\n",scores_train)
print("Accuracy_test\n",scores_test)
print("Precision_B\n",precision_B)
print("Precision_M\n",precision_M)
print("Recall_B\n",recall_B)
print("Recall_M\n",recall_M)
print("Average accuracy for training data\n",scores_train.sum()/20)
print("Average accuracy for test data\n",scores_test.sum()/20)
print("Average precision for benign cancer\n",precision_B.sum()/20)
print("Average precision for malignant cancer\n",precision_M.sum()/20)
print("Average recall for benign cancer\n",recall_B.sum()/20)
print("Average recall for malignant cancer\n",recall_M.sum()/20)<jupyter_output>Accuracy_train
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Accuracy_test
 [0.9197995  0.90977444 0.9122807  0.92481203 0.87719298 0.93984962
 0.9122807  0.90726817 0.89473684 0.9273183  0.9273183  0.9047619
 0.90977444 0.94235589 0.93483709 0.93233083 0.92230576 0.9197995
 0.9273183  0.91729323]
Precision_B
 [0.93723849 0.91698113 0.94779116 0.92519685 0.96956522 0.93536122
 0.96566524 0.91902834 0.94583333 0.94190871 0.91353383 0.94957983
 0.93650794 0.9496124  0.93774319 0.95238095 0.95798319 0.92720307
 0.94901961 0.924     ]
Precision_M
 [0.89375    0.89552239 0.85333333 0.92413793 0.75147929 0.94852941
 0.8373494  0.88815789 0.81761006 0.90506329 0.95488722 0.83850932
 0.86394558 0.92907801 0.92957746 0.89795918 0.86956522 0.9057971
 0.88888889 0.90604027]
Recall_B
 [0.92946058 0.94552529 0.91472868 0.95528455 0.84150943 0.97233202
 0.89285714 0.93032787 0.88671875 0.93801653 0.97590361 0.8968254
 0.921875   0.96078431 0.96015936 0.94117647 0.91566265 0.94901961[...]<jupyter_text>(2)Evaluation for DT2<jupyter_code>#get the average depth and leaves of DT1
print(depth.sum()/20)
print(numleaf.sum()/20)
clf = DecisionTreeClassifier()
clf.set_params(criterion='entropy',max_depth=4,max_leaf_nodes=7)
for i in range(20):
    (X_train,X_test,Y_train,Y_test)=train_test_split(X, Y, test_size=0.7)
    clf.fit(X_train, Y_train)
    leaves=clf.predict(X_test)
    scores_train[i]=clf.score(X_train,Y_train)
    scores_test[i]=clf.score(X_test, Y_test)
    precision=skm.precision_score(Y_test,leaves,average=None)
    recall=skm.recall_score(Y_test,leaves,average=None)
    precision_B[i]=precision[0]
    precision_M[i]=precision[1]
    recall_B[i]=recall[0]
    recall_M[i]=recall[1]
print("Accuracy_train\n",scores_train)
print("Accuracy_test\n",scores_test)
print("Precision_B\n",precision_B)
print("Precision_M\n",precision_M)
print("Recall_B\n",recall_B)
print("Recall_M\n",recall_M)
print("Average accuracy for training data\n",scores_train.sum()/20)
print("Average accuracy for test data\n",scores_test.sum()/20)
print("Average precision for benign cancer\n",precision_B.sum()/20)
print("Average precision for malignant cancer\n",precision_M.sum()/20)
print("Average recall for benign cancer\n",recall_B.sum()/20)
print("Average recall for malignant cancer\n",recall_M.sum()/20)<jupyter_output>Accuracy_train
 [1.         0.98823529 1.         0.98823529 0.98823529 0.99411765
 0.99411765 0.97647059 0.95882353 0.99411765 0.96470588 0.98235294
 0.99411765 0.97058824 0.97058824 0.99411765 0.98823529 0.97058824
 0.98823529 0.98823529]
Accuracy_test
 [0.9197995  0.93984962 0.9122807  0.92481203 0.91478697 0.93483709
 0.92230576 0.87719298 0.88972431 0.91478697 0.91478697 0.92481203
 0.87969925 0.9273183  0.90225564 0.87468672 0.93483709 0.93233083
 0.93984962 0.92481203]
Precision_B
 [0.94715447 0.96341463 0.91764706 0.92996109 0.90225564 0.956
 0.93700787 0.94347826 0.92436975 0.95416667 0.89259259 0.95180723
 0.91176471 0.95850622 0.87636364 0.94570136 0.94422311 0.90566038
 0.93014706 0.91732283]
Precision_M
 [0.87581699 0.90196078 0.90277778 0.91549296 0.93984962 0.89932886
 0.89655172 0.78698225 0.83850932 0.85534591 0.96124031 0.88
 0.83229814 0.87974684 0.95967742 0.78651685 0.91891892 0.98507463
 0.96062992 0.93793103]
Recall_B
 [0.92460317 0.94047619 0.94354839 0.95219124[...]
{'fraction_non_alphanumeric': 0.12697763229678125, 'fraction_numerical': 0.24986361156573922, 'mean_word_length': 4.0260452364633315, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '618677', 'license': 'MIT', 'n_tokens_mistral': 4173, 'n_tokens_neox': 3045, 'n_words': 535, 'path': 'H2.ipynb', 'repo_name': 'GO-Loc-GO/NUS-Machine-Learning-Codes', 'size': '7332'}