Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- data = pd.read_csv("Dataset/sales.csv")
- data
- data.describe()
- #data.apply(pd.value_counts)
- data['Buys'].value_counts()
- from sklearn.preprocessing import LabelEncoder
- le=LabelEncoder();
- #data=data.apply(le.fit_transform)
- x=data.iloc[:,:-1] #-1 means don't take last column
- x=x.apply(le.fit_transform)
- #find label with their encoded value
- print("Age with encodd value :",list( zip(data.iloc[:,0], x.iloc[:,0])))
- print("\nIncome with encoded value :",list( zip(data.iloc[:,1], x.iloc[:,1])))
- print("\nGender with encoded value :",list( zip(data.iloc[:,2], x.iloc[:,2])))
- print("\nmaritialStatus with encoded value :",list( zip(data.iloc[:,3], x.iloc[:,3])))
- y=data.iloc[:,-1]
- from sklearn.tree import DecisionTreeClassifier
- classifier=DecisionTreeClassifier(criterion='entropy')
- classifier.fit(x,y)
- #Predict value for the given Expression
- #[Age < 21, Income = Low,Gender = Female, Marital Status = Married]
- test_x=np.array([2,2,0,0])
- pred_y=classifier.predict([test_x])
- print("Predicted class for input [Age > 35, Income = Medium,Gender = Female, Marital Status = Married]\n", test_x," is ",pred_y[0])
- #method to generate graph p.s. needs dot utility installed in os
- from sklearn.tree import export_graphviz
- from IPython.display import Image
- export_graphviz(classifier,out_file="6th7th.dot",feature_names=x.columns,class_names=["No","Yes"])
- #you need to install graphviz in fedora(IN LAB) for running below dor command
- #yum install graphviz
- #then go to terminal and cd to directory where you are saving jupyter notebook
- # and execute below command
- # dot -Tpng data.dot -o tree.png
- # !dot -Tpng data.dot -o tree.png
- # Image("tree.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement