import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc #for model evaluation
from sklearn.metrics import classification_report #for model evaluation
from sklearn.metrics import confusion_matrix #for model evaluation
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn import preprocessing
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score, precision_score, recall_score, precision_recall_curve, \
average_precision_score
from inspect import signature
masterprojekt_daten = pd.read_csv("./Messergebnisse_merged.csv")
masterprojekt_daten = masterprojekt_daten.drop(columns=['Unnamed: 0', 'timeStamp'], axis=0)
#print(masterprojekt_daten.columns, masterprojekt_daten.dtypes)
#print(masterprojekt_daten.dtypes)
masterprojekt_daten
x = masterprojekt_daten.drop(columns=['Klassifizierung nach Messschraube'])
y = masterprojekt_daten['Klassifizierung nach Messschraube']
x.shape, y.shape
X_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, random_state=1432542)
correlation = masterprojekt_daten.corr()
from sklearn.linear_model import LogisticRegression
regressor = LogisticRegression(random_state=0, solver='lbfgs', max_iter=5000, multi_class='multinomial', C=0.15, verbose=1)
regressor.fit(X_train, y_train)
regressor.score(X_train, y_train)
y_pred = regressor.predict(x_test)
y_probs = regressor.predict_proba(x_test)
print('Cross validation of the Data set: ', cross_val_score(regressor, x, y, verbose=3))
y_test.shape, y_probs.shape
print('AUC Curve of the Data set: ', cross_val_score(regressor, x, y, verbose=3, scoring='roc_auc'))
probabilities = y_probs[:,1]
fpr, tpr, thresholds = roc_curve(y_test, probabilities)
roc_auc = auc(fpr, tpr)
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'y', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
precision, recall, threshold = precision_recall_curve(y_test, y_pred)
average_precision = average_precision_score(y_test, y_pred)
step_kwargs = ({'step': 'post'} if 'step' in signature(plt.fill_between).parameters else {})
plt.step(recall, precision, color='r', alpha=0.2, where='post')
plt.fill_between(recall, precision, alpha=0.2, color='r', **step_kwargs)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.0])
plt.xlim([0.0, 1.0])
plt.title('2-class Precision-Recall curve: AP={0:0.2f}'.format(average_precision))
import sklearn
matrix = confusion_matrix(y_test, y_pred)
class_names = masterprojekt_daten.columns
ax = plt.subplot()
# plt.figure(figsize = (10,7))
sns.heatmap(matrix, annot=True, fmt='d', ax = ax, annot_kws={"size": 16})
ax.set_xlabel('Predicted labels')
ax.set_ylabel('True labels')
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(['i.O.', 'n.I.O.']); ax.yaxis.set_ticklabels(['i.O.', 'n.I.O.'])
#ax.yaxis.set_major_locator(ticker.IndexLocator(base=1, offset=0.5))
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Precision:", precision_score(y_test, y_pred))
print("Recall:", recall_score(y_test, y_pred))
cnf_matrix = confusion_matrix(y_test, y_pred)
# class_names=[0,1]
# fig, ax = plt.subplots()
# tick_marks = np.arange(len(class_names))
# plt.xticks(tick_marks, class_names)
# plt.yticks(tick_marks, class_names)
# # create heatmap
# sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu" ,fmt='g')
# ax.xaxis.set_label_position("top")
# plt.tight_layout()
# plt.title('Confusion matrix', y=1.1)
# plt.ylabel('Actual label')
# plt.xlabel('Predicted label')
# plt.show()
sns.heatmap(cnf_matrix, annot=True, fmt="d")
# loss = expit(x_test * regressor.coef_ + regressor.intercept_).ravel()
# plt.plot(x_test, loss, color='red', linewidth=3)
df_example = pd.DataFrame({'x': x_test.iloc[:,0], 'y': y_test})
df_example = df_example.sort_values(by='x')
from scipy.special import expit
sigmoid_function = expit(df_example['x'] * regressor.coef_[0][0] + regressor.intercept_[0]).ravel()
plt.plot(df_example['x'], sigmoid_function)
plt.scatter(df_example['x'], df_example['y'], c=df_example['y'], cmap='rainbow', edgecolors='b')