<jupyter_start><jupyter_text>* @author: <EMAIL>  * @site: e-smartdata.org  P<NAME><jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from IPython.display import Image
from sklearn.tree import DecisionTreeClassifier
sns.set()<jupyter_output><empty_output><jupyter_text>Załadowanie danych<jupyter_code>iris = load_iris()
data = iris.data
targets = iris.target
feature_names = [name.replace(' ', '_')[:-5] for name in iris.feature_names]
class_names = iris.target_names

print('Liczba próbek:', len(data))
print('Kształt danych', data.shape)
print('Nazwy zmiennych objaśniających:', feature_names)
print('Nazwy kategorii kosaćca:', class_names)<jupyter_output>Liczba próbek: 150
Kształt danych (150, 4)
Nazwy zmiennych objaśniających: ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
Nazwy kategorii kosaćca: ['setosa' 'versicolor' 'virginica']<jupyter_text>Iris Setosa<jupyter_code>Image(url='https://ml-repository-krakers.s3-eu-west-1.amazonaws.com/decision_tree_course/graphs/Iris_setosa.jpg', width=300)<jupyter_output><empty_output><jupyter_text>Iris Versicolor<jupyter_code>Image(url='https://ml-repository-krakers.s3-eu-west-1.amazonaws.com/decision_tree_course/graphs/Iris_versicolor.jpg', width=300)<jupyter_output><empty_output><jupyter_text>Iris Virginica<jupyter_code>Image(url='https://ml-repository-krakers.s3-eu-west-1.amazonaws.com/decision_tree_course/graphs/Iris_virginica.jpg', width=300)<jupyter_output><empty_output><jupyter_text>Poznaj swoje dane<jupyter_code>print(iris.DESCR)
data_targets = np.c_[data, targets]
df = pd.DataFrame(data_targets, columns=feature_names + ['class'])
df.head()
df.info()
df.describe().apply(lambda x: round(x, 2))
plt.figure(figsize=(9, 7))
sns.scatterplot('sepal_length', 'sepal_width', hue='class', data=df, legend='full')
plt.figure(figsize=(9, 7))
sns.scatterplot('petal_length', 'petal_width', hue='class', data=df, legend='full')
df['class'].value_counts()<jupyter_output><empty_output><jupyter_text>Przygotowanie danych do modelu<jupyter_code>X = df.copy()
X = X[['petal_length', 'petal_width', 'class']]
y = X.pop('class')
X.head()
y.head()<jupyter_output><empty_output><jupyter_text>Konwersja DataFrame do tablic numpy<jupyter_code>X = X.values
y = y.values.astype('int16')<jupyter_output><empty_output><jupyter_text>Budowa klasyfikatora drzewa decyzyjnego- max_depth=1<jupyter_code>tree_clf = DecisionTreeClassifier(max_depth=1, random_state=30)
tree_clf.fit(X, y)<jupyter_output><empty_output><jupyter_text>Wykreślenie granic decyzyjnych<jupyter_code>from mlxtend.plotting import plot_decision_regions

colors='#f1865b,#31c30f,#64647F,#d62728,#9467bd,#8c564b,#e377c2,#7f7f7f,#bcbd22,#17becf'

plt.figure(figsize=(12, 10))
plot_decision_regions(X, y, tree_clf, legend=2, colors=colors)
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)')
plt.title('Drzewo decyzyjne: max_depth=1')
print('Dokładność modelu: ' + str(round(tree_clf.score(X, y), 4) * 100) + '%')<jupyter_output>Dokładność modelu: 66.67%<jupyter_text>Graf drzewa decyzyjnego- max_depth = 1<jupyter_code>from sklearn.tree import export_graphviz

export_graphviz(tree_clf,
               out_file='iris_tree.dot',
               feature_names=iris.feature_names[2:],
               class_names=iris.target_names,
               rounded=True,
               filled=True)<jupyter_output><empty_output><jupyter_text>Przekonwertowanie pliku .dot na plik .png<jupyter_code>%%bash
dot -Tpng iris_tree.dot -o iris_tree_1.png<jupyter_output><empty_output><jupyter_text>Bardziej przyjazny dla Python'a sposób<jupyter_code>from sklearn.externals.six import StringIO
from IPython.display import Image
import pydotplus

dot_data = StringIO()
export_graphviz(tree_clf,
               out_file=dot_data,
               feature_names=feature_names[2:],
               class_names=class_names,
               special_characters=True,
               rounded=True,
               filled=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('graph.png')
Image(graph.create_png())<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/externals/six.py:31: DeprecationWarning: The module is deprecated in version 0.21 and will be removed in version 0.23 since we've dropped support for Python 2.7. Please rely on the official version of six (https://pypi.org/project/six/).
  "(https://pypi.org/project/six/).", DeprecationWarning)<jupyter_text>Budowa prostej funkcji dla modelu drzewa decyzyjnego<jupyter_code>def make_decision_tree(max_depth=1):
    # train model
    tree_clf = DecisionTreeClassifier(max_depth=max_depth, random_state=30)
    tree_clf.fit(X, y)

    # export graph
    dot_data = StringIO()
    export_graphviz(tree_clf,
                   out_file=dot_data,
                   feature_names=feature_names[2:],
                   class_names=class_names,
                   special_characters=True,
                   rounded=True,
                   filled=True)
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    graph.write_png('graph.png')
    
    
    # plot decision boundries
    colors='#f1865b,#31c30f,#64647F,#d62728,#9467bd,#8c564b,#e377c2,#7f7f7f,#bcbd22,#17becf'
    plt.figure(figsize=(12, 10))
    ax = plot_decision_regions(X, y, tree_clf, legend=0, colors=colors)
    handles, labels = ax.get_legend_handles_labels()
    ax.legend(handles, ['setosa', 'versicolor', 'virginica'], framealpha=0.3)
    plt.xlabel('petal length (cm)')
    plt.ylabel('petal width (cm)')
    plt.title('Drzewo decyzyjne: max_depth={}'.format(max_depth))
    
    # accuracy
    print('Dokładność modelu: ' + str(round(tree_clf.score(X, y) * 100, 2)) + '%')
    return Image(graph.create_png())<jupyter_output><empty_output><jupyter_text>Budowa klasyfikatora drzewa decyzyjnego- max_depth=2<jupyter_code>make_decision_tree(max_depth=2)<jupyter_output>Dokładność modelu: 96.0%<jupyter_text>Budowa klasyfikatora drzewa decyzyjnego- max_depth=3<jupyter_code>make_decision_tree(max_depth=3)<jupyter_output>Dokładność modelu: 97.33%<jupyter_text>Budowa klasyfikatora drzewa decyzyjnego- max_depth=4<jupyter_code>make_decision_tree(max_depth=4)<jupyter_output>Dokładność modelu: 98.67%<jupyter_text>Budowa klasyfikatora drzewa decyzyjnego- max_depth=5<jupyter_code>make_decision_tree(max_depth=5)<jupyter_output>Dokładność modelu: 99.33%
{'fraction_non_alphanumeric': 0.12918809412498053, 'fraction_numerical': 0.027427146641732895, 'mean_word_length': 5.945887445887446, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 68, 'https://': 5, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '656409', 'license': 'MIT', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2506, 'n_words': 348, 'path': 'decision_trees/classification/01_decision_tree_iris.ipynb', 'repo_name': 'krakowiakpawel9/decision_tree_course', 'size': '6453'}