<jupyter_start><jupyter_text>A Simple Classification Problem with Python_Fruit Classification<jupyter_code># import the packages

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import tree
# import the dataset
fruits = pd.read_csv('Data/fruit_with_colors.csv')
print(fruits.info())
# show the first five rows
fruits.head()
# fruit type distribution
print(fruits['fruit_name'].value_counts())
# plot the distribution
sns.countplot(fruits['fruit_name'],label="Count")
plt.show()
feature_names = ['mass', 'width', 'height', 'color_score']
X = fruits[feature_names]
y = fruits['fruit_label']
from sklearn.tree import DecisionTreeClassifier, export_graphviz

clftree = DecisionTreeClassifier(max_depth=3, random_state=24)
clftree.fit(X, y)<jupyter_output><empty_output><jupyter_text>Print decision rules<jupyter_code>from sklearn.tree.export import export_text

r = export_text(clftree, feature_names=feature_names)
print(r)<jupyter_output>|--- height <= 7.95
|   |--- mass <= 128.00
|   |   |--- height <= 6.10
|   |   |   |--- class: 2
|   |   |--- height >  6.10
|   |   |   |--- class: 4
|   |--- mass >  128.00
|   |   |--- width <= 7.25
|   |   |   |--- class: 3
|   |   |--- width >  7.25
|   |   |   |--- class: 1
|--- height >  7.95
|   |--- width <= 7.40
|   |   |--- class: 4
|   |--- width >  7.40
|   |   |--- class: 3<jupyter_text>Print tree image  Approach 1<jupyter_code># pip install pydotplus

# pip install graphviz
# conda install graphviz
# add the location of 'gvedit.exe' file to the user's environment variable

import pydotplus as pdp
from IPython.display import Image
from io import StringIO


# This function creates images of tree models using pydotplus
def print_tree(estimator, features, class_names=None, filled=True):
    tree = estimator
    names = features
    color = filled
    classn = class_names
    
    dot_data = StringIO()
    export_graphviz(estimator, out_file=dot_data, feature_names=features, class_names=classn, filled=filled)
    graph = pdp.graph_from_dot_data(dot_data.getvalue())
    return(graph)
graph3 = print_tree(clftree, features=X.columns, class_names=['apple', 'mandarin','orange','lemon'])
Image(graph3.create_png())<jupyter_output><empty_output><jupyter_text>Approach 2If you cannot print using the first approach, as a workaround, you can print the image from a web link<jupyter_code>with open("fruit_classifier.txt", "w") as f:
    f = tree.export_graphviz(clftree, out_file=f, feature_names=feature_names, class_names=['apple', 'mandarin','orange','lemon'], filled=True)

# copy the content in "fruit_classifier.txt" to http://webgraphviz.com/, to display the tree image<jupyter_output><empty_output><jupyter_text>Variable Importance<jupyter_code># View a list of the features and their importance scores
print(list(zip(X, clftree.feature_importances_)))

# plot the important features
feat_importances = pd.Series(clftree.feature_importances_, index=X.columns)
feat_importances.nlargest(5).plot(kind='barh')<jupyter_output>[('mass', 0.19478841180144613), ('width', 0.45180048218827407), ('height', 0.3534111060102798), ('color_score', 0.0)]
{'fraction_non_alphanumeric': 0.1335020563112939, 'fraction_numerical': 0.032584625118633345, 'mean_word_length': 4.877323420074349, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '270556', 'license': 'MIT', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1090, 'n_words': 255, 'path': 'Notebooks/DecisionTree_FruitsClassification.ipynb', 'repo_name': 'EthanRao/BUAL-5610', 'size': '3161'}