a b/users/utility/MyClassifier.py
1
import pandas as pd
2
from matplotlib import pyplot as plt
3
from sklearn import preprocessing
4
from sklearn.model_selection import train_test_split
5
from sklearn.naive_bayes import GaussianNB
6
from sklearn.neighbors import KNeighborsClassifier
7
from sklearn import metrics
8
from django.conf import settings
9
filepath = settings.MEDIA_ROOT + "\\" + 'stress_data.xlsx'
10
df = pd.read_excel(filepath, header=None)
11
12
df.columns=['Target', 'ECG(mV)', 'EMG(mV)','Foot GSR(mV)','Hand GSR(mV)', 'HR(bpm)','RESP(mV)']
13
X_train, X_test, y_train, y_test = train_test_split(df[['ECG(mV)', 'EMG(mV)','Foot GSR(mV)','Hand GSR(mV)', 'HR(bpm)','RESP(mV)']], df['Target'],
14
    test_size=0.30, random_state=12345)
15
16
# Min-Max Scaling
17
18
minmax_scale = preprocessing.MinMaxScaler().fit(df[['ECG(mV)', 'EMG(mV)','Foot GSR(mV)','Hand GSR(mV)', 'HR(bpm)','RESP(mV)']])
19
df_minmax = minmax_scale.transform(df[['ECG(mV)', 'EMG(mV)','Foot GSR(mV)','Hand GSR(mV)', 'HR(bpm)','RESP(mV)']])
20
X_train_norm, X_test_norm, y_train_norm, y_test_norm = train_test_split(df_minmax, df['Target'],
21
    test_size=0.30, random_state=12345)
22
def plot():
23
    plt.figure(figsize=(8,6))
24
25
    plt.scatter(df['Hand GSR(mV)'], df['HR(bpm)'],
26
            color='green', label='input scale', alpha=0.5)
27
28
    plt.scatter(df_minmax[:,0], df_minmax[:,1],
29
            color='blue', label='min-max scaled [min=0, max=1]', alpha=0.3)
30
31
    plt.title('Hand GSR and HR content of the physiological dataset')
32
    plt.xlabel('Hand GSR')
33
    plt.ylabel('HR')
34
    plt.legend(loc='upper left')
35
    plt.grid()
36
37
    plt.tight_layout()
38
39
class KNNclassifier:
40
    def getKnnResults(self):
41
        # filepath = settings.MEDIA_ROOT + "\\" + 'stress_data.xlsx'
42
        print("Started works")
43
        knn = KNeighborsClassifier(n_neighbors=5)
44
        fit = knn.fit(X_train, y_train)
45
46
        # on normalized data
47
        knn_norm = KNeighborsClassifier(n_neighbors=5)
48
        fit_norm = knn_norm.fit(X_train_norm, y_train)
49
50
        pred_train = knn.predict(X_train)
51
        pred_test = knn.predict(X_test)
52
53
        # Accuracy measure for datasets
54
55
        print('Accuracy measure for dataset:- ', '{:.2%}\n'.format(metrics.accuracy_score(y_test, pred_test)))
56
57
        pred_test_norm = knn_norm.predict(X_test_norm)
58
        print('Accuracy measure for normalized dataset:- ',
59
              '{:.2%}\n'.format(metrics.accuracy_score(y_test, pred_test_norm)))
60
61
        # comparing the true and predicted responses
62
63
        print('True target values: ', y_test.values[0:25])
64
        print('Predicted target values: ', pred_test_norm[0:25])
65
66
        # Confusion Matrix
67
        print(metrics.confusion_matrix(y_test, pred_test_norm))
68
        print('True target values: ', y_test.values[0:25])
69
        print('Predicted target values: ', pred_test_norm[0:25])
70
        print()
71
        confusion = metrics.confusion_matrix(y_test, pred_test_norm)
72
        TP = confusion[1, 1]
73
        TN = confusion[0, 0]
74
        FP = confusion[0, 1]
75
        FN = confusion[1, 0]
76
77
        # Metrics calclulation using confusion matrix
78
79
        print()
80
        # Classsification accuracy:- how often is the classifier correct
81
        accuracy = metrics.accuracy_score(y_test, pred_test_norm)
82
        print('Classification Accuracy:- ', accuracy)
83
84
        # Classification error/Misclassification rate:- how often is the classifier is incorrect
85
        classificationerror = 1 - metrics.accuracy_score(y_test, pred_test_norm)
86
        print('Classification Error:- ',classificationerror )
87
88
        # Sensitivity :- when the actual value is positive , how often is the prediction correct?
89
        sensitivity = metrics.recall_score(y_test, pred_test_norm)
90
        print('Sensitivity:- ',sensitivity )
91
92
        # Specificity:- when the actual value is negative ,how often the prediction is the correct?
93
        Specificity =  TN / float(TN + FP)
94
        print('Specificity:- ',Specificity )
95
96
        # False positive rate:- when the actual value is negative ,how often the prediction is the incorrect?
97
        fsp =  FP / float(TN + FP)
98
        print('False positive rate:- ', fsp)
99
100
        # Precision:- when a positive value is predicted , how often is the prediction correct?
101
        precision = metrics.precision_score(y_test, pred_test_norm)
102
        print('Precision:- ', precision)
103
104
        # Prediction of stress/no stress class on new dataset
105
        print()
106
        pred_data_norm = minmax_scale.transform([[-0.005, 0.49, 8.257, 5.853, 66.142, 45.998]])
107
        pred = knn_norm.predict(pred_data_norm)
108
        print('Predicted class for dataset [-0.005,0.49,8.257,5.853,66.142,45.998]:- ', pred)
109
110
        pred_data_norm = minmax_scale.transform([[0.001, 0.931, 5.91, 19.773, 99.065, 35.59]])
111
        pred = knn_norm.predict(pred_data_norm)
112
        print('Predicted class for dataset [0.001,0.931,5.91,19.773,99.065,35.59]:- ', pred)
113
114
        return df,accuracy,classificationerror,sensitivity,Specificity,fsp,precision