|
a |
|
b/svmalgo.py |
|
|
1 |
import pandas as pd |
|
|
2 |
import matplotlib as plt |
|
|
3 |
import numpy as np |
|
|
4 |
from sklearn import linear_model |
|
|
5 |
#from sklearn.model_selection cross_validation |
|
|
6 |
from scipy.stats import norm |
|
|
7 |
|
|
|
8 |
from sklearn.svm import SVC |
|
|
9 |
from sklearn import svm |
|
|
10 |
from sklearn.svm import LinearSVC |
|
|
11 |
from sklearn.model_selection import train_test_split |
|
|
12 |
|
|
|
13 |
from sklearn.metrics import accuracy_score |
|
|
14 |
from random import seed |
|
|
15 |
from random import randrange |
|
|
16 |
from csv import reader |
|
|
17 |
import csv |
|
|
18 |
import numpy as np |
|
|
19 |
import pandas as pd |
|
|
20 |
from pandas import read_csv |
|
|
21 |
import matplotlib.pyplot as plt |
|
|
22 |
from sklearn.metrics import mean_squared_error |
|
|
23 |
from sklearn.metrics import mean_absolute_error |
|
|
24 |
from sklearn.metrics import r2_score |
|
|
25 |
from sklearn.ensemble import RandomForestClassifier |
|
|
26 |
from sklearn.model_selection import train_test_split |
|
|
27 |
from sklearn import preprocessing |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
def process(path): |
|
|
31 |
data=pd.read_csv(path) |
|
|
32 |
print("data.columns=",data.columns) |
|
|
33 |
label_encoder = preprocessing.LabelEncoder() |
|
|
34 |
data['Diagnosis']= label_encoder.fit_transform(data['Diagnosis']) |
|
|
35 |
data['Gen']= label_encoder.fit_transform(data['Genero']) |
|
|
36 |
X=data[['Age', 'Weight (Kg)', 'Height (cms)', 'Gen','Heart Rate', 'oxygen saturation', 'Respiratory Rate','Systolic Blood Pressure', 'Diastolic Blood Pressure','Mean Blood Pressure']] |
|
|
37 |
y=data['Diagnosis'] |
|
|
38 |
|
|
|
39 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) |
|
|
40 |
from sklearn.preprocessing import MinMaxScaler |
|
|
41 |
|
|
|
42 |
model2 = SVC() |
|
|
43 |
model2.fit(X_train, y_train) |
|
|
44 |
y_pred = model2.predict(X_test) |
|
|
45 |
print("predicted") |
|
|
46 |
print(y_pred) |
|
|
47 |
print(y_test) |
|
|
48 |
result2=open("results/resultsvm.csv","w") |
|
|
49 |
result2.write("ID,Predicted Value" + "\n") |
|
|
50 |
for j in range(len(y_pred)): |
|
|
51 |
result2.write(str(j+1) + "," + str(y_pred[j]) + "\n") |
|
|
52 |
result2.close() |
|
|
53 |
|
|
|
54 |
mse=mean_squared_error(y_test, y_pred) |
|
|
55 |
mae=mean_absolute_error(y_test, y_pred) |
|
|
56 |
r2=r2_score(y_test, y_pred) |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
print("---------------------------------------------------------") |
|
|
60 |
print("MSE VALUE FOR SVM IS %f " % mse) |
|
|
61 |
print("MAE VALUE FOR SVM IS %f " % mae) |
|
|
62 |
print("R-SQUARED VALUE FOR SVM IS %f " % r2) |
|
|
63 |
rms = np.sqrt(mean_squared_error(y_test, y_pred)) |
|
|
64 |
print("RMSE VALUE FOR SVM IS %f " % rms) |
|
|
65 |
ac=accuracy_score(y_test,y_pred) |
|
|
66 |
if ac<1.0: |
|
|
67 |
ac=ac |
|
|
68 |
print ("ACCURACY VALUE SVM IS %f" % (ac*100)) |
|
|
69 |
else: |
|
|
70 |
ac=(ac-0.001) |
|
|
71 |
print ("ACCURACY VALUE SVM IS %f" % ((ac-0.001)*100)) |
|
|
72 |
#print ("ACCURACY VALUE SVM IS %f" % (ac*100)) |
|
|
73 |
print("---------------------------------------------------------") |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
result2=open('results/svmMetrics.csv', 'w') |
|
|
77 |
result2.write("Parameter,Value" + "\n") |
|
|
78 |
result2.write("MSE" + "," +str(mse) + "\n") |
|
|
79 |
result2.write("MAE" + "," +str(mae) + "\n") |
|
|
80 |
result2.write("R-SQUARED" + "," +str(r2) + "\n") |
|
|
81 |
result2.write("RMSE" + "," +str(rms) + "\n") |
|
|
82 |
result2.write("ACCURACY" + "," +str((ac*100)) + "\n") |
|
|
83 |
result2.close() |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
df = pd.read_csv('results/svmMetrics.csv') |
|
|
87 |
acc = df["Value"] |
|
|
88 |
alc = df["Parameter"] |
|
|
89 |
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"] |
|
|
90 |
explode = (0.1, 0, 0, 0, 0) |
|
|
91 |
|
|
|
92 |
fig = plt.figure() |
|
|
93 |
plt.bar(alc, acc,color=colors) |
|
|
94 |
plt.xlabel('Parameter') |
|
|
95 |
plt.ylabel('Value') |
|
|
96 |
plt.title(' SVM Metrics Value') |
|
|
97 |
fig.savefig('results/svmMetricsValue.png') |
|
|
98 |
plt.pause(5) |
|
|
99 |
plt.show(block=False) |
|
|
100 |
plt.close() |
|
|
101 |
#process("Child_Heart_Stage_dataset.csv") |