# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''base'': conda)'
#     name: python385jvsc74a57bd0e660af3098993ea5160fd9270f74e6db286e749e474b6bbf115c5a1892218496
# ---

# Daten importieren aus Excel
# ------

# +
import pandas as pd

data_input = pd.read_excel('../data/raw/U bung kNN Klassifizierung Ecoli.xlsx', sheet_name=0)
data_output = pd.read_excel('../data/raw/U bung kNN Klassifizierung Ecoli.xlsx', sheet_name=1)
# -

data_output

# Train und Test Datensätze erstellen, normalisieren und kNN-Classifier trainieren
# ---------

# +
X = data_input.loc[:,data_input.columns != "Target"]
y = data_input["Target"]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20) #, random_state=0)
# -

data_input

# +
#Normalisieren der Daten
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(X_train)

X_train_norm = scaler.transform(X_train)
X_test_norm = scaler.transform(X_test)
# -

from sklearn.neighbors import KNeighborsClassifier
knnclf = KNeighborsClassifier(n_neighbors=7)
knnclf.fit(X_train_norm, y_train)

# Test Datensätze erstellen und kNN-Classifier verwenden
# ---------

X_output = data_output

y_pred = knnclf.predict(X_output)

#Normalisieren der Daten
X_output_norm = scaler.transform(X_output)
y_pred_norm = knnclf.predict(X_output_norm)

print(X_output)
print(y_pred)
print(X_output_norm)
print(y_pred_norm)

# Akkuranz bestimmen
# ----------------

score = knnclf.score(X_test,y_test)
print('knn-Classifier scores with {}% accuracy'.format(score*100))

# Optimalen k-wert bestimmen
# ---------------

# y_pred = knnclf.predict(X_test)
#
# from sklearn.metrics import classification_report, confusion_matrix
# print(confusion_matrix(y_test, y_pred))
# print(classification_report(y_test, y_pred))

# +
import matplotlib.pyplot as plt
import numpy as np
error = []

# Calculating error for K values between 1 and 40
for i in range(1, 40):
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train_norm, y_train)
    pred_i = knn.predict(X_test_norm)
    error.append(np.mean(pred_i != y_test))
# -

plt.figure(figsize=(12, 6))
plt.plot(range(1, 40), error, color='red', linestyle='dashed', marker='o',
         markerfacecolor='blue', markersize=10)
plt.title('Error Rate K Value')
plt.xlabel('K Value')
plt.ylabel('Mean Error')

# Decision Tree
# ----------

# +
from sklearn.tree import DecisionTreeClassifier

dt = DecisionTreeClassifier(max_depth=3, criterion="entropy", min_samples_split=2)
dt.fit(X_train,y_train)
from sklearn.tree import DecisionTreeClassifier

score = dt.score(X_test,y_test)
print('Decision Tree scores with {}% accuracy'.format(score*100))

# +
from sklearn.model_selection import GridSearchCV

#tree parameters which shall be tested
tree_para = {'criterion':['gini','entropy'],'max_depth':[i for i in range(1,20)], 'min_samples_split':[i for i in range (2,20)]}

#GridSearchCV object
grd_clf = GridSearchCV(dt, tree_para, cv=5)

#creates differnt trees with all the differnet parameters out of our data
grd_clf.fit(X_train, y_train)

#best paramters that were found
best_parameters = grd_clf.best_params_  
print(best_parameters) 
# -



StarcoderdataJupyterScriptsDedupFiltered