# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Supervised Binary Classification with parametric Algorithm
# Modeling of Data as Supervised Learning Problem as a binary classification. The ten minutes leading up to the crash will be assigned with target = 1. Logistic Regression will be used, as I want to see calibrated Probabilites for Failure and change Thresholds. 
#
# Approach: Same as before, but with some Feature Selection done

# +
# Data Manipulation and Visualization
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('Solarize_Light2')
plt.rcParams['font.size'] = 18
from warnings import filterwarnings
filterwarnings('ignore')

# Data Preparation
from sklearn.model_selection import train_test_split
import collections
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import StandardScaler

# Machine learning
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
# -

# ## Import and Check Data

df = pd.read_csv('transform_one.csv')

df.head()

# ## Train-Test-Split, SMOTE, Scaling

X = df.drop('broken', axis=1)
y = df.broken

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, stratify=y, random_state=44)
X_train.shape, X_test.shape, y_train.shape, y_test.shape

# +
# normally smote needs 6 neighbors, that is why i had to define number
sm = SMOTE(k_neighbors=2, random_state = 12)
X_trains, y_trains = sm.fit_resample(X_train, y_train)

print(y_train.value_counts())
print(collections.Counter(y_trains))

# +
# Scaler Object
sc = StandardScaler()

# Fitting on train
sc.fit(X_trains)

# Transformation of train and test
X_trains = sc.transform(X_trains)
X_tests = sc.transform(X_test)

# +
# Ohne Oversampling
sc = StandardScaler()
sc.fit(X_train)

X_train = sc.transform(X_train)
X_test = sc.transform(X_test)
# -

# ## Feature Importance
# Find most important Features with the Random Forest Classifier. Due to multicollinearity in the data set, the results must be viewed carefully.

clf = RandomForestClassifier(n_estimators=100)

clf.fit(X_trains, y_trains)

y_pred_clf = clf.predict(X_test)

fi = pd.Series(data=clf.feature_importances_, index=df.columns[:-1]).sort_values(ascending=False)
plt.figure(figsize=(15,20))
plt.title("Feature Importances", fontsize = 20)
ax = sns.barplot(y=fi.index, x=fi.values, palette="Blues_d", orient='h')

fi.index.tolist()[:8]

# ## Assessment of Feature Importance
# The results seem to, at least partially, coincide with what the manual analysis reading the plots showed. (Sensor 0, 4, 10, 11, 12)

StarcoderdataJupyterScriptsDedupFiltered