Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

decision tree algorithm python

import pandas
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt

df = pandas.read_csv("data.csv")

d = {'UK': 0, 'USA': 1, 'N': 2}
df['Nationality'] = df['Nationality'].map(d)
d = {'YES': 1, 'NO': 0}
df['Go'] = df['Go'].map(d)

features = ['Age', 'Experience', 'Rank', 'Nationality']

X = df[features]
y = df['Go']

dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)

tree.plot_tree(dtree, feature_names=features)
Comment

python decision tree

# Create Decision Tree classifer object
clf = DecisionTreeClassifier(criterion="entropy", max_depth=3)

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)

#Predict the response for test dataset
y_pred = clf.predict(X_test)

# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Comment

PREVIOUS NEXT
Code Example
Python :: create an empty array numpy 
Python :: python why call super(class).__init__() 
Python :: max value pandas 
Python :: Python RegEx Searching for an occurrence of the pattern 
Python :: django check if get parameter exists 
Python :: python bool 
Python :: convert int to float python 
Python :: data frame 
Python :: listing of django model types 
Python :: python tableau 
Python :: run python script inside bash script 
Python :: numpy round to nearest 5 
Python :: python program to display fibonacci sequence using recursion 
Python :: how to make a grid in python 
Python :: how to find unique sublist in list in python 
Python :: install python ubuntu 
Python :: how to get data after last slash in python 
Python :: knuth morris pratt algorithm 
Python :: cosine similarity python 
Python :: Math Module degrees() Function in python 
Python :: python save picture in folder 
Python :: text color python tkinter 
Python :: python print() 
Python :: sum values in django models and insert value in model field 
Python :: boolien in python 
Python :: How do I schedule an email to send at a certain time using cron and smtp, in python 
Python :: max element in dictionary python 
Python :: python select file in folder given extension 
Python :: put cropped image in original image name folder python 
Python :: Using emoji Modules in Python 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =