Advertisement
ShrekOP

Assg6/7

Dec 14th, 2022 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3.  
  4. data = pd.read_csv("Dataset/sales.csv")
  5. data
  6.  
  7. data.describe()
  8.  
  9. #data.apply(pd.value_counts)
  10. data['Buys'].value_counts()
  11.  
  12. from sklearn.preprocessing import LabelEncoder
  13. le=LabelEncoder();
  14. #data=data.apply(le.fit_transform)
  15. x=data.iloc[:,:-1] #-1 means don't take last column
  16. x=x.apply(le.fit_transform)
  17. #find label with their encoded value
  18. print("Age with encodd value :",list( zip(data.iloc[:,0], x.iloc[:,0])))
  19. print("\nIncome with encoded value :",list( zip(data.iloc[:,1], x.iloc[:,1])))
  20. print("\nGender with encoded value :",list( zip(data.iloc[:,2], x.iloc[:,2])))
  21. print("\nmaritialStatus with encoded value :",list( zip(data.iloc[:,3], x.iloc[:,3])))
  22.  
  23. y=data.iloc[:,-1]
  24.  
  25. from sklearn.tree import DecisionTreeClassifier
  26. classifier=DecisionTreeClassifier(criterion='entropy')
  27. classifier.fit(x,y)
  28.  
  29. #Predict value for the given Expression
  30. #[Age < 21, Income = Low,Gender = Female, Marital Status = Married]
  31. test_x=np.array([2,2,0,0])
  32. pred_y=classifier.predict([test_x])
  33. print("Predicted class for input [Age > 35, Income = Medium,Gender = Female, Marital Status = Married]\n", test_x," is ",pred_y[0])
  34.  
  35. #method to generate graph p.s. needs dot utility installed in os
  36. from sklearn.tree import export_graphviz
  37. from IPython.display import Image
  38. export_graphviz(classifier,out_file="6th7th.dot",feature_names=x.columns,class_names=["No","Yes"])
  39. #you need to install graphviz in fedora(IN LAB) for running below dor command
  40. #yum install graphviz
  41. #then go to terminal and cd to directory where you are saving jupyter notebook
  42. # and execute below command
  43. # dot -Tpng data.dot -o tree.png
  44. # !dot -Tpng data.dot -o tree.png
  45. # Image("tree.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement