Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.tree import DecisionTreeClassifier
  4.  
  5. my_data = pd.read_csv("")
  6. my_data.head()
  7. #we give X the values to train
  8. X = my_data[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].values
  9. X[0:5]
  10.  
  11.  
  12. #Change the values of categorical data with dummies
  13.  
  14. from sklearn import preprocessing
  15. le_sex = preprocessing.LabelEncoder()
  16. le_sex.fit(['F','M'])
  17. X[:,1] = le_sex.transform(X[:,1])
  18.  
  19. le_BP = preprocessing.LabelEncoder()
  20. le_BP.fit([ 'LOW', 'NORMAL', 'HIGH'])
  21. X[:,2] = le_BP.transform(X[:,2])
  22.  
  23.  
  24. le_Chol = preprocessing.LabelEncoder()
  25. le_Chol.fit([ 'NORMAL', 'HIGH'])
  26. X[:,3] = le_Chol.transform(X[:,3])
  27.  
  28. X[0:5]
  29.  
  30.  
  31. y = my_data["Drug"]
  32. y[0:5]
  33.  
  34.  
  35. #We use Train test split
  36. from sklearn.model_selection import train_test_split
  37. X_trainset, X_testset, y_trainset, y_testset = train_test_split(X, y, test_size=0.3, random_state=3)
  38.  
  39.  
  40. #We create our model Decision Tree
  41. drugTree = DecisionTreeClassifier(criterion="entropy", max_depth = 4)
  42. drugTree # it shows the default parameters
  43.  
  44.  
  45. #We fit the Model Normally
  46. drugTree.fit(X_trainset,y_trainset)
  47.  
  48. #We use prediction to predict the output
  49. predTree = drugTree.predict(X_testset)
  50. #print the prediction vs the real values
  51. print (predTree [0:5])
  52. print (y_testset [0:5])
  53.  
  54. #Check the accuracy
  55. from sklearn import metrics
  56. import matplotlib.pyplot as plt
  57. print("DecisionTrees's Accuracy: ", metrics.accuracy_score(y_testset, predTree))
  58.  
  59. #Tree visualization
  60.  
  61. from sklearn.externals.six import StringIO
  62. import pydotplus
  63. import matplotlib.image as mpimg
  64. from sklearn import tree
  65. %matplotlib inline
  66.  
  67. dot_data = StringIO()
  68. filename = "drugtree.png"
  69. featureNames = my_data.columns[0:5]
  70. targetNames = my_data["Drug"].unique().tolist()
  71. 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)  
  72. graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
  73. graph.write_png(filename)
  74. img = mpimg.imread(filename)
  75. plt.figure(figsize=(100, 200))
  76. plt.imshow(img,interpolation='nearest')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement