import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
from scipy import spatial

# import data
iris = load_iris()
test_idx = [0, 50, 100]
# print(iris.feature_names)
# print(iris.target_names)
# for i in range(len(iris.target)):
#     print('Example %d: label %s, features %s' % (i, iris.target[i], iris.data[i]))

# training data
train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx, axis=0)

# testing data
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]

clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)

# predict label for new flower
print(test_target)
print(clf.predict(test_data))

# visualize the tree
from sklearn.externals.six import StringIO
import pydotplus

dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
                    feature_names=iris.feature_names,
                    class_names=iris.target_names,
                    filled=True, rounded=True,
                    special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('iris.png')


print(test_data[1], test_target[1])
print(iris.feature_names, iris.target_names)

##############################################################################
##############################################################################

# import data
iris = load_iris()
train_data, test_data, train_target, test_target = \
    train_test_split(iris.data, iris.target, test_size=0.2, random_state=60)

# training data
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)

# testing data
test_result = clf.predict(test_data)
accuracy = 1 - spatial.distance.cosine(test_target, test_result)
print(test_target)
print(test_result)
print(accuracy)
