import pandas
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import plot_tree
from matplotlib import pyplot
df = pandas.read_csv("diabetes.csv")
df_features = df.drop(labels=["Outcome"], axis=1)
df_target = df.filter(items=["Outcome"])
X_train, X_test, y_train, y_test = train_test_split(df_features, df_target["Outcome"], test_size=0.2, shuffle=True, random_state=1)
classifier = DecisionTreeClassifier(random_state=1, max_depth=3)
classifier.fit(X_train, y_train)
y_test_pred = classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_test_pred)
print("Accuracy Score: ", accuracy)
plot_tree(classifier, feature_names=df_features.columns, filled=True, fontsize=10)
pyplot.show()
Please note that the feature_names parameter provides the name of the features. The filled=True parameter indicates that we want to print various information, such as the impurity of each node, etc. And the font size parameter adjusts the font size.
The output plot will look like the following:

We can use similar Python code to plot a regression tree. I would leave it to the reader to try out the same.








































0 Comments