简体   繁体   中英

How to plot Decision trees on a line graph using sklearn

I'm new to data analytics and I'm currently dealing with decision tree. If I wanted to represent the example below into a line graph how would I go about it?

 from sklearn.model_selection import train_test_split
from sklearn import tree

import pandas as pd

df  = pd.read_csv("titanic.csv",encoding = "ISO-8859-1")
dict = {'female': 1, 'male':2}
df['Sex'] = df['Sex'] .map(dict)

flt = df  [['Survived', 'Pclass', 'Age', 'Sex']]
flt = flt.dropna()

X = (flt[['Sex', 'Age']])
y = flt[['Survived']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

clf = tree.DecisionTreeClassifier()
clf.fit(X_train, y_train)

print(clf.score(X_train, y_train))
print(clf.score(X_test, y_test))

Just add this line

tree.plot_tree(clf)

You can check the documentation on sklearn's website for details, but for a basic chart, this should suffice.

The question is simple. Welcome to this crazy world of data analytics. To represent your example with a line graph, just use tree.plot_tree(clf) and for view tree.plt.show()

from sklearn.model_selection import train_test_split
from sklearn import tree
import pandas as pd

import matplotlib.pyplot as plt #update 
from sklearn.datasets import load_iris #update

df  = pd.read_csv("titanic.csv",encoding = "ISO-8859-1")
dict = {'female': 1, 'male':2}
df['Sex'] = df['Sex'] .map(dict)

flt = df  [['Survived', 'Pclass', 'Age', 'Sex']]
flt = flt.dropna()

X = (flt[['Sex', 'Age']])
y = flt[['Survived']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

clf = tree.DecisionTreeClassifier()
clf.fit(X_train, y_train)
   
print(clf.score(X_train, y_train))
print(clf.score(X_test, y_test))

#SOLUTION
tree.plot_tree(clf) #update
tree.plt.show() #update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM