<jupyter_start><jupyter_text>Decision Trees<jupyter_code>import numpy as np 
import pandas as pd
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>About the dataset    Imagine that you are a medical researcher compiling data for a study. You have collected data about a set of patients, all of whom suffered from the same illness. During their course of treatment, each patient responded to one of 5 medications, Drug A, Drug B, Drug c, Drug x and y.             Part of your job is to build a model to find out which drug might be appropriate for a future patient with the same illness. The features of this dataset are Age, Sex, Blood Pressure, and the Cholesterol of the patients, and the target is the drug that each patient responded to.            It is a sample of multiclass classifier, and you can use the training part of the dataset     to build a decision tree, and then use it to predict the class of an unknown patient, or to prescribe a drug to a new patient.<jupyter_code>!wget -O drug200.csv https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-ML0101EN-SkillsNetwork/labs/Module%203/data/drug200.csv
my_data = pd.read_csv("drug200.csv", delimiter=",")
my_data[0:5]
# write your code here
my_data.shape<jupyter_output><empty_output><jupyter_text>Pre-processing<jupyter_code>X = my_data[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].values
X[0:5]
from sklearn import preprocessing
le_sex = preprocessing.LabelEncoder()
le_sex.fit(['F','M'])
X[:,1] = le_sex.transform(X[:,1]) 


le_BP = preprocessing.LabelEncoder()
le_BP.fit([ 'LOW', 'NORMAL', 'HIGH'])
X[:,2] = le_BP.transform(X[:,2])


le_Chol = preprocessing.LabelEncoder()
le_Chol.fit([ 'NORMAL', 'HIGH'])
X[:,3] = le_Chol.transform(X[:,3]) 

X[0:10]

y = my_data["Drug"]
y[0:5]<jupyter_output><empty_output><jupyter_text>Setting up the Decision Tree    We will be using train/test split on our decision tree. Let's import train_test_split from sklearn.cross_validation.<jupyter_code>from sklearn.model_selection import train_test_split
X_trainset, X_testset, y_trainset, y_testset = train_test_split(X, y, test_size=0.3, random_state=3)<jupyter_output><empty_output><jupyter_text>Modeling    We will first create an instance of the DecisionTreeClassifier called drugTree.    Inside of the classifier, specify  criterion="entropy"  so we can see the information gain of each node.<jupyter_code>drugTree = DecisionTreeClassifier(criterion="entropy", max_depth = 4)
drugTree # it shows the default parameters
drugTree.fit(X_trainset,y_trainset)<jupyter_output><empty_output><jupyter_text>Prediction    Let's make some predictions on the testing dataset and store it into a variable called predTree.<jupyter_code>predTree = drugTree.predict(X_testset)
print (predTree [0:5])
print (y_testset [0:5])<jupyter_output>['drugY' 'drugX' 'drugX' 'drugX' 'drugX']
40     drugY
51     drugX
139    drugX
197    drugX
170    drugX
Name: Drug, dtype: object<jupyter_text>Evaluation    Next, let's import metrics from sklearn and check the accuracy of our model.<jupyter_code>from sklearn import metrics
import matplotlib.pyplot as plt
print("DecisionTrees's Accuracy: ", metrics.accuracy_score(y_testset, predTree))<jupyter_output>DecisionTrees's Accuracy:  0.9833333333333333<jupyter_text>VisualizationLet's visualize the tree<jupyter_code># Notice: You might need to uncomment and install the pydotplus and graphviz libraries if you have not installed these before
!conda install -c conda-forge pydotplus -y
!conda install -c conda-forge python-graphviz -y
from  io import StringIO
import pydotplus
import matplotlib.image as mpimg
from sklearn import tree
%matplotlib inline 
dot_data = StringIO()
filename = "drugtree.png"
featureNames = my_data.columns[0:5]
out=tree.export_graphviz(drugTree,feature_names=featureNames, out_file=dot_data, class_names= np.unique(y_trainset), filled=True,  special_characters=True,rotate=False)  
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png(filename)
img = mpimg.imread(filename)
plt.figure(figsize=(100, 200))
plt.imshow(img,interpolation='nearest')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09501074242062545, 'fraction_numerical': 0.01885891620911912, 'mean_word_length': 6.101694915254237, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '360006', 'license': 'MIT', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1354, 'n_words': 413, 'path': 'ML-models/12_Machine_Learning-Classification-Decision-Trees-drug.ipynb', 'repo_name': 'sbeniwal19/Data-Science-Projects', 'size': '4189'}