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

# # Importing Packges

# + colab={} colab_type="code" executionInfo={"elapsed": 3586, "status": "ok", "timestamp": 1598629644701, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="mcR2OGs_kRno"
# %matplotlib inline

import numpy as np

import joblib


import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, f1_score, recall_score,precision_score, classification_report, confusion_matrix
import collections
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import precision_recall_curve, roc_curve
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import label_binarize

np.random.seed(1337)  # for reproducibility



from sklearn.ensemble import VotingClassifier
# -

# # Loading data (CTTD features and Labels)

# + colab={} colab_type="code" executionInfo={"elapsed": 10269, "status": "ok", "timestamp": 1598629651412, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="dY-T9ZviojBz"
X_train = np.load('../data/train/X_train.npy') 

Y_train = np.load('../data/train/Y_train.npy')

X_test = np.load('../data/test/set1/X_test.npy')

Y_test = np.load('../data/test/set1/Y_test.npy')

X_test2 = np.load('../data/test/set2/X_test2.npy')

Y_test2 = np.load('../data/test/set2/Y_test2.npy')
# -

# # Scaling features

# + colab={} colab_type="code" executionInfo={"elapsed": 775, "status": "ok", "timestamp": 1598629657354, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="4cJXuCsmpz8D"
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)
X_test2 = scaler.fit_transform(X_test2)

# + colab={"base_uri": "https://localhost:8080/", "height": 119} colab_type="code" executionInfo={"elapsed": 1402, "status": "ok", "timestamp": 1598629663256, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="Ph-dUh8ZqEa9" outputId="7f878c8c-946d-4c82-8d1e-ac9a26edb5a1"
print(X_train.shape)
print(Y_train.shape)

print(X_test.shape)
print(Y_test.shape)


print(X_test2.shape)
print(Y_test2.shape)
# -

# # Loading models 

# + colab={"base_uri": "https://localhost:8080/", "height": 105} colab_type="code" executionInfo={"elapsed": 1314, "status": "ok", "timestamp": 1598629960623, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="RW_ws22wqLi0" outputId="2b428f9b-31fb-463e-e4c0-5b896489a255"
SVM_clf = joblib.load('../models/svm_emg_clf.pkl')

LR_clf = joblib.load('../models/LR_emg_clf.pkl')

RF_clf = joblib.load('../models/RF_emg_clf.pkl')

ET_clf = joblib.load('../models/ET_emg_clf.pkl')

# + colab={} colab_type="code" executionInfo={"elapsed": 1378, "status": "ok", "timestamp": 1598630015761, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="FUpO8EImsfiX"
estimators=[('LR',LR_clf),('SVM',SVM_clf),('RF',RF_clf),('ET',ET_clf)]

# + colab={} colab_type="code" executionInfo={"elapsed": 1674, "status": "ok", "timestamp": 1598630041632, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="IzeYCKYyvSGC"
voting_clf=VotingClassifier(estimators,voting='soft')

# + colab={"base_uri": "https://localhost:8080/", "height": 510} colab_type="code" executionInfo={"elapsed": 204096, "status": "ok", "timestamp": 1598630270403, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="1ZGKeLj0viwy" outputId="016f5444-f0e0-4983-b8c2-33297f1c7beb"
voting_clf.fit(X_train,Y_train)

# + colab={} colab_type="code" executionInfo={"elapsed": 3309, "status": "ok", "timestamp": 1598630288279, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="_XQ3s4NQvluy"
Y_predict = voting_clf.predict(X_test)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" executionInfo={"elapsed": 1223, "status": "ok", "timestamp": 1598630290667, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="qit8WlR1vtxI" outputId="ebd3c93e-00ab-43ac-987d-65270198b798"
accuracy_score(Y_test, Y_predict)
# -

# # SVC class don't compute probability by default, so we have to train the model by setting the parameter probability=True.

# + colab={} colab_type="code" executionInfo={"elapsed": 1876, "status": "ok", "timestamp": 1598630477437, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="aOIBSVovzIdT"
SVM_clf = SVC(kernel = 'rbf', C = 35.4, gamma= 'scale', class_weight = 'balanced',probability=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 85} colab_type="code" executionInfo={"elapsed": 15927, "status": "ok", "timestamp": 1598630494539, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="Pe9WoA-NzTVq" outputId="0f2e08c8-e731-4142-9cd7-5005c276e92b"
SVM_clf.fit(X_train, Y_train)
# -

# # Evaluation on Test set 1

# + colab={"base_uri": "https://localhost:8080/", "height": 102} colab_type="code" executionInfo={"elapsed": 5075, "status": "ok", "timestamp": 1598630574162, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="-_gcq3uXvu80" outputId="3c27be6c-f3bf-4738-fd17-d7eea3435a5c"
for clf in (LR_clf,SVM_clf,RF_clf,ET_clf,voting_clf):
    y_predict=clf.predict(X_test)
    print(clf.__class__.__name__,accuracy_score(Y_test, y_predict)) 
# -

# # Evaluation on Test set 2

# + colab={"base_uri": "https://localhost:8080/", "height": 102} colab_type="code" executionInfo={"elapsed": 3546, "status": "ok", "timestamp": 1598630634131, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "11638296981034036456"}, "user_tz": -330} id="qZR_2eNxzzxJ" outputId="24b0445f-bd6f-4742-e5e1-345ae5f29442"
for clf in (LR_clf,SVM_clf,RF_clf,ET_clf,voting_clf):
    y_predict=clf.predict(X_test2)
    print(clf.__class__.__name__,accuracy_score(Y_test2, y_predict)) 

StarcoderdataJupyterScriptsDedupFiltered