# -*- coding: utf-8 -*-
"""NRF01_DT.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1XCBo45vkxctbR0y0wQCpVBIh7Kv8FN0Y
"""

from sklearn.datasets import load_iris # iris資料集
from sklearn.tree import DecisionTreeClassifier # 決策樹分類器
from sklearn.model_selection import train_test_split # split訓練及測試資料

# Load data
iris = load_iris()

# features and target
X = iris.data
y = iris.target

# split dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0) # 依2:1切割出training/test sets

# fit data
clf = DecisionTreeClassifier(max_depth=3) # max_depth: 決策樹的深度
clf.fit(X_train, y_train)

# outside prediction
y_pred_test = clf.predict(X_test)

# inside prediction
y_pred_train = clf.predict(X_train)

# calculate performance
acc_outside = sum((y_test - y_pred_test)==0) / len(y_test) * 100
acc_inside = sum((y_train - y_pred_train)==0) / len(y_train) * 100

print("inside accuracy: {:.2f}%".format(acc_inside))
print("outside accuracy: {:.2f}%".format(acc_outside))