#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 16 21:18:38 2021
@author: batuhandaz
"""
import numpy as np
import matplotlib.pyplot as plt 
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap

#veri setini alıyoruz
data = pd.read_csv("/home/batuhan/Masaüstü/decision tree/decision_tree.csv")
#başlıkları bastırıyoruz
print(data.head())

#ilk 4 veri setini ve verinin parametrelerini aldık
X = data.iloc[:,[2,3]].values
y = data.iloc[:,4].values

#eğitim ve test veri setlerini oluşturduk
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.25, random_state= 0)
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

#kara ağacı fonksiyonumuzu çağıralım
classifier = DecisionTreeClassifier()
classifier = classifier.fit(X_train,y_train)

#doğruluk payımızın ne kadar olduğunu kontrol edelim
y_pred = classifier.predict(X_test)
print('Doğruluk payı', metrics.accuracy_score(y_test,y_pred))
cm = confusion_matrix(y_test, y_pred)
print(cm)

#Veri setlerimiziden oluşturulan modeli görselleştirelim
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:,0].min()-1, stop= X_set[:,0].max()+1, step = 0.01),np.arange(start = X_set[:,1].min()-1, stop= X_set[:,1].max()+1, step = 0.01))
plt.contourf(X1,X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha=0.75, cmap = ListedColormap(("white","black")))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i,j in enumerate(np.unique(y_set)):
 plt.scatter(X_set[y_set==j,0],X_set[y_set==j,1], c = ListedColormap(("gray","red"))(i),label = j)
plt.title("Karar Ağacı")
plt.xlabel("Tahmini Yaş")
plt.ylabel("Maaş")
plt.legend()
plt.show()