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 :: make the program title a name python 
Python :: pandas rows count 
Python :: error handling flask 
Python :: python unzip a zip 
Python :: yahoo finance api python 
Python :: basic pygame window 
Python :: how to use enumerate in python 
Python :: hex python add 0 
Python :: Export a Pandas dataframe as a table image 
Python :: Python NumPy swapaxis Function Syntax 
Python :: How to perform Bubble sort in Python? 
Python :: nn.dropout 
Python :: pandas divide one column by another 
Python :: how to use elif in python 
Python :: how to check libraries in python 
Python :: cmd check if python is installed 
Python :: pythonwrite to file 
Python :: std python 
Python :: python print variables and string 
Python :: NumPy unique Example Get the counts of each unique value 
Python :: thread with args python 
Python :: plot using matplotlib 
Python :: how to use static files in django 
Python :: How to split a text column into two separate columns? 
Python :: requests.packages.urllib3.util.retry could not be resolved from source 
Python :: np.random.normal 
Python :: create a blank image 
Python :: count list python 
Python :: beautiful soup 4 
Python :: python location 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =