-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fall_Detection_rev_GitHub.py
153 lines (84 loc) · 2.87 KB
/
Fall_Detection_rev_GitHub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# coding: utf-8
# In[61]:
import pandas as pd
import numpy as np
import pickle
import os
from sklearn import svm
from matplotlib import pyplot as plt
from sklearn import metrics
# In[62]:
accxy = pd.read_csv('AccXYLatest.csv')
accxy
# In[63]:
X = accxy.drop(['Label'], axis=1)
Y = accxy.Label
# In[64]:
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2)
# In[65]:
print(X_test)
# In[66]:
from sklearn.svm import SVC
model = SVC(C = 1, gamma = 1, kernel = 'rbf')
# In[67]:
model.fit(X_train, Y_train)
# In[68]:
model.score(X_test, Y_test)
# In[69]:
with open('svmfazarev.pkl', 'wb') as f:
pickle.dump(model, f)
# In[70]:
with open('svmfazarev.pkl', 'rb') as f:
model = pickle.load(f)
# In[71]:
# buat meshgrid
h = 0.01 # step size in the mesh
x_min, x_max = X.iloc[:, 0].min() - 1, X.iloc[:, 0].max() + 1
y_min, y_max = X.iloc[:, 1].min() - 1, X.iloc[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Z berfungsi untuk melakukan predict terhadap model yang sudah dibuat
Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) #Ravel itu mengubah array nxn menjadi 1xn
Z = Z.reshape(xx.shape) #mengubah shape (pake fucntion ravel) Z jadi sama dengan shape xx
# Plot decision boundary dengan data poin
plt.contourf(xx, yy, Z, alpha=1) #plot contour
plt.scatter(X.iloc[:, 0], X.iloc[:, 1], c=Y, cmap=plt.cm.Paired) #plot scatter data kolom 1 dan 2, dengan tiap data memiliki wanra kontur yg sama
# Set batas plot dan label
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xlabel('Accelerometer X')
plt.ylabel('Accelerometer Y')
plt.title("Plot Scatter Dengan Decision Boundary RBF")
# plot scatter grafik
plt.show()
# In[75]:
# from sklearn.model_selection import GridSearchCV
# #cari parameter terbaik
# parameter_grid = {'C': [0.1, 1, 10, 100, 1000],
# 'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
# 'kernel': ['rbf']
# }
# grid = GridSearchCV(SVC(), parameter_grid, refit = True, verbose = 3)
# grid.fit(X_train, Y_train)
# In[76]:
# print(grid.best_params_)
# print(grid.best_estimator_)
# In[77]:
from matplotlib import pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
data0 = data[data.Label==0]
data1 = data[data.Label==1]
plt.xlabel('Accelerometer X')
plt.ylabel('Accelerometer Y')
plt.scatter(data0['Accelerometer X'], data0['Accelerometer Y'], color = 'blue', marker = 'x')
plt.scatter(data1['Accelerometer X'], data1['Accelerometer Y'], color = 'red', marker = '.')
plt.title('Grafik yang dihasilkan')
# In[78]:
import pickle
# Load the trained model from the pickle file
with open('svmfazarev.pkl', 'rb') as file:
model = pickle.load(file)
# Use the model for prediction or other tasks
result = model.predict(data)
print(result)