<jupyter_start><jupyter_text>Predicting Ideal Drug for each PatientClassified based on patient demographics and historical data on how they've responded to these drugs in the past. All patients were diagnosed with the same illness.<jupyter_code>import numpy as np 
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
#getting data
#!wget -O drug200.csv https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/ML0101ENv3/labs/drug200.csv
my_data = pd.read_csv("drug200.csv", delimiter=",")
my_data[0:5]
#size
my_data.size<jupyter_output><empty_output><jupyter_text>Pre-processing<jupyter_code># X = Feature Matrix
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:5]

# y = response vector
y = my_data["Drug"]
y[0:5]
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Now  train_test_split  will return 4 different parameters. We will name them:X_trainset, X_testset, y_trainset, y_testset  The  train_test_split  will need the parameters: X, y, test_size=0.3, and random_state=3.<jupyter_code>X_trainset, X_testset, y_trainset, y_testset = train_test_split(X, y, test_size=0.3, random_state=3)
#shapes
print (X_trainset.shape, y_trainset.shape)
print (X_testset.shape, y_testset.shape)
#modelling
drugTree = DecisionTreeClassifier(criterion="entropy", max_depth = 4)
drugTree # it shows the default parameters
drugTree.fit(X_trainset,y_trainset)
#predictions
predTree = drugTree.predict(X_testset)
print (predTree [0:10])
print (y_testset [0:10])
#evaluation
from sklearn import metrics
import matplotlib.pyplot as plt
print("DecisionTrees's Accuracy: ", metrics.accuracy_score(y_testset, predTree))
# Visualize
# pip3 install pydotplus
# pip3 install graphviz

'''
from sklearn.externals.six 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]
targetNames = my_data["Drug"].unique().tolist()
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.12754555198285103, 'fraction_numerical': 0.01893533404787424, 'mean_word_length': 7.459214501510574, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '592562', 'license': 'MIT', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1013, 'n_words': 217, 'path': 'MLwithPython_Coursera/Decision Tree.ipynb', 'repo_name': 'k8iechen/Machine-Learning-with-Python', 'size': '2799'}