"""
## **Set-up and importing**
"""
import warnings
warnings.filterwarnings("ignore")
import os
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
import random
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import make_pipeline
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
"""
## **Initilizing seed and random values**
"""


seed_value = 44
os.environ['PYTHONHASHSEED']=str(seed_value)

# Set `python` built-in pseudo-random generator at a fixed value
random.seed(seed_value)

# Set `numpy` pseudo-random generator at a fixed value
np.random.seed(seed_value)
data_file ="../input/predictive-maintenance/ai4i2020.csv" 
data = pd.read_csv(data_file)
train_data,test_data = train_test_split(data, test_size = 0.33, random_state = seed_value)
train_data.to_csv('train.csv',index=0)
test_data.to_csv('test.csv',index=0)
data.head()
"""
## **Binary Classification** 
"""
X=train_data.iloc[:,3:8]
Y=train_data.iloc[:,8]
X_test=test_data.iloc[:,3:8]
Y_test=test_data.iloc[:,8]
print(X.shape)
print(Y.shape)
clf = RandomForestClassifier(max_depth=2, random_state=seed_value)
clf = clf.fit(X, Y)
prediction=clf.predict(X_test)
print(confusion_matrix(Y_test,prediction))
print("Random Forest model accuracy(in %):", metrics.accuracy_score(Y_test, prediction)*100)
classification_report(Y_test,prediction)
sgd = make_pipeline(SGDClassifier(max_iter=1000, tol=1e-3),)
sgd.fit(X, Y)
pre=sgd.predict(X_test)
print(confusion_matrix(Y_test,pre))
print("SGD model accuracy(in %):", metrics.accuracy_score(Y_test, pre)*100)
classification_report(Y_test,pre)
gnb = GaussianNB()
gnb.fit(X, Y) 
y_pred = gnb.predict(X_test)
from sklearn import metrics
print("Gaussian Naive Bayes model accuracy(in %):", metrics.accuracy_score(Y_test, y_pred)*100)
classification_report(Y_test,y_pred)
"""
## **Multi-classification**
* Classfiying Machine Failure Type 
"""
data["Type"]=data["Type"].map( {'S':0 , 'M':1, 'L':2} )
data['TWF'] = data['TWF'].replace(1, "TWF", regex=True)
data['TWF'] = data['TWF'].replace(0, "", regex=True)
data['HDF'] = data['HDF'].replace(1, "HDF", regex=True)
data['HDF'] = data['HDF'].replace(0, "", regex=True)
data["PWF"] = data['PWF'].replace(1,"PWF",regex=True)
data["PWF"] = data['PWF'].replace(0,"",regex=True)
data["OSF"] = data['OSF'].replace(1,"OSF",regex=True)
data["OSF"] = data['OSF'].replace(0,"",regex=True)
data["RNF"] = data['RNF'].replace(1,"RNF",regex=True)
data["RNF"] = data['RNF'].replace(0,"",regex=True)
'''data.loc[data["TWF"] == 1, ["TWF"]] = "TWF"
data.loc[data["HDF"] == 1, ["HDF"]] = "HDF"
data.loc[data["PWF"] == 1, ["PWF"]] = "PWF"
data.loc[data["OSF"] == 1, ["OSF"]] = "OSF"
data.loc[data["RNF"] == 1, ["RNF"]] = "RNF"
data["class"] = data["TWF"] + data["HDF"]+data["PWF"]+data["OSF"]+data["RNF"]'''
data["class"] = data["TWF"] + data["HDF"]+data["PWF"]+data["OSF"]+data["RNF"]
multi_class_data= data[data['class'].str.len()> 1]
multi_class_data=multi_class_data.drop(["TWF","HDF","PWF","OSF","RNF"],axis=1)

multi_class_data["class"].unique()
multi_class_data.head()
x=multi_class_data.iloc[:200,3:8]
y=multi_class_data.iloc[:200,9]
x_test=multi_class_data.iloc[200:,3:8]
y_test=multi_class_data.iloc[200:,9]
clf = RandomForestClassifier(max_depth=2, random_state=seed_value)
clf = clf.fit(x, y)
prediction=clf.predict(x_test)
print(confusion_matrix(y_test,prediction))
print("Multi-class : Random Forest model accuracy(in %):", metrics.accuracy_score(y_test, prediction)*100)
classification_report(y_test,prediction)
sgd = make_pipeline(SGDClassifier(max_iter=1000, tol=1e-3),)
sgd.fit(x, y)
pre=sgd.predict(x_test)
print("Multi-class : SGD model accuracy(in %):", metrics.accuracy_score(y_test, pre)*100)
classification_report(y_test,pre)
gnb = GaussianNB()
gnb.fit(x, y) 
y_pred = gnb.predict(x_test)
from sklearn import metrics
print("Multi-class : Gaussian Naive Bayes model accuracy(in %):", metrics.accuracy_score(y_test, y_pred)*100)
classification_report(y_test,y_pred)
from sklearn.datasets import make_blobs
from pandas import DataFrame
x, y = make_blobs(n_samples=200, centers=3, n_features=2,random_state=44)
classfication_data = DataFrame(dict(x1=x[:,0], x2=x[:,1], label=y))
classfication_data.head()
train_data,test_data = train_test_split(classfication_data, test_size = 0.33, random_state = seed_value)
x=train_data.iloc[:,:2]
y=train_data.iloc[:,2]
x_test=test_data.iloc[:,:2]
y_test=test_data.iloc[:,2]
clf = RandomForestClassifier(max_depth=2, random_state=seed_value)
clf = clf.fit(x, y)
prediction=clf.predict(x_test)
print(confusion_matrix(y_test,prediction))
print("Multi-class : Random Forest model accuracy(in %):", metrics.accuracy_score(y_test, prediction)*100)
classification_report(y_test,prediction)
#data.tail(50)
sgd = make_pipeline(SGDClassifier(max_iter=1000, tol=1e-3),)
sgd.fit(x, y)
pre=sgd.predict(x_test)
print("Multi-class : SGD model accuracy(in %):", metrics.accuracy_score(y_test, pre)*100)
classification_report(y_test,pre)

gnb = GaussianNB()
gnb.fit(x, y) 
y_pred = gnb.predict(x_test)
from sklearn import metrics
print("Multi-class : Gaussian Naive Bayes model accuracy(in %):", metrics.accuracy_score(y_test, y_pred)*100)
classification_report(y_test,y_pred)