<jupyter_start><jupyter_text>CE-121<jupyter_code>#Import scikit-learn dataset library
from sklearn import datasets
from sklearn import preprocessing
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#Load dataset
bcancer = datasets.load_breast_cancer()
# print the names of the 30 features
print("Features: ",bcancer.feature_names)

# print the label type of cancer(malignant, benign)
print("Labels: ",bcancer.target_names)

# print data(feature)shape
print("data shape: ",bcancer.data.shape)

#print target shape
print("target shape: ",bcancer.target.shape)
print(bcancer.keys())
dataset = pd.DataFrame(bcancer.data, columns=[bcancer.feature_names])
#dataset['Target'] = pd.Series(data=bcancer.target, index=dataset.index)
dataset['target']=bcancer.target
print(dataset.tail())
y_enc=dataset.iloc[:,30]
print(y_enc)
le=preprocessing.LabelEncoder()
y_enc=le.fit_transform(y_enc)
print(y_enc)
dataset=dataset.drop(['target'],axis=1)
print(dataset.tail())


ohe=preprocessing.OneHotEncoder(dtype=np.int)
x_enc=ohe.fit_transform(dataset)
print(x_enc)
print(ohe.get_feature_names(bcancer.feature_names))
print(len(ohe.get_feature_names(bcancer.feature_names)))
from pandas.core.common import random_state
#train test division(50%-50%)
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(x_enc,y_enc, test_size = 0.50,random_state=121)
print(X_train)
#create DecisionTree model
dtc=DecisionTreeClassifier(criterion="entropy",random_state=121,max_leaf_nodes=121)
dtc.fit(X_train,Y_train)
pred_op=dtc.predict(X_test)
print("predicted output: ",pred_op)
print("actual test output: ",Y_test)
from sklearn import metrics
print("Accuracy is :- ",metrics.accuracy_score(Y_test, pred_op))
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score

precision = precision_score(Y_test, pred_op)
recall = recall_score(Y_test, pred_op)

print("precision :- ",precision)
print("recall :- ",recall)
# from sklearn.tree import export_graphviz

# export_graphviz(dtc,out_file='tree_entropy.dot',
#               feature_names=ohe.get_feature_names(bcancer.feature_names),class_names=list(bcancer.target_names), 
#               filled=True,max_depth=122)

# #convert to png
# from subprocess import call
# call(['dot', '-Tpng', 'tree_entropy.dot', '-o', 'tree_entropy.png', '-Gdpi=600'])

# # Display in python
# import matplotlib.pyplot as plt
# plt.figure(figsize = (14, 18))
# plt.imshow(plt.imread('tree_entropy.png'))
# plt.axis('off');
# plt.show();
from sklearn.metrics import confusion_matrix

confusion_matrix(Y_test, pred_op)
disp = metrics.plot_confusion_matrix(dtc, X_test, Y_test)
disp.figure_.suptitle("Confusion Matrix")
print("Confusion matrix:\n",disp.confusion_matrix)

plt.show()<jupyter_output>Confusion matrix:
 [[  6  89]
 [  3 187]]
{'fraction_non_alphanumeric': 0.1269678302532512, 'fraction_numerical': 0.01403148528405202, 'mean_word_length': 6.732804232804233, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '100294', 'license': 'MIT', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1034, 'n_words': 193, 'path': 'Lab4/CE121_Lab4_02.ipynb', 'repo_name': 'jwalit21/121_Jwalit_Shah', 'size': '2922'}