-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification_of_hand_posture_analysis_using_a_v_elm.py
154 lines (120 loc) · 4.01 KB
/
classification_of_hand_posture_analysis_using_a_v_elm.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
153
154
# -*- coding: utf-8 -*-
"""Classification of Hand Posture Analysis Using a V-ELM
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1RFgNfrhY0Y0k-81JhEDaOIacl07RsNE1
<h1>1. Import Dataset</h1>
"""
!wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=1JVZLOn1BJ7oKwMTaAgk4aM8tEAZsqpWI' -O MoCapHandPostures.csv
import pandas as pd
import numpy as np
import statistics
data = pd.read_csv('MoCapHandPostures.csv')
data = data.iloc[1: , :]
data
"""<h1>2. Imputasi Missing Value</h1>"""
data = data.replace('?',np.nan)
data
import random
def imputasi(df_input):
list_columns = df_input.columns[2:]
class_columns = df_input.columns[0]
df_input.dropna(thresh=11, axis=1)
for column in list_columns:
df_input[column] = pd.to_numeric(df_input[column])
val = df_input.groupby(class_columns)[column]
df_input[column] = df_input[column].fillna(value=val.transform('mean'))
if df_input.isnull().any().any():
for column in list_columns:
val = df_input.groupby('User')[column]
df_input[column] = df_input[column].fillna(value=val.transform('mean'))
return df_input
data_impu = imputasi(data)
data_impu
data_impu = data_impu.drop(columns=['User'])
def minmax(df_input):
list_fitur = df_input.columns[1:]
for fitur in list_fitur:
grouper = df_input.groupby('Class')[fitur]
maxes = grouper.transform('max')
mins = grouper.transform('min')
df_input = df_input.assign(fitur=(df_input[fitur] - mins)/(maxes - mins))
#df_input = df_input.groupby('Class').transform(lambda x: (x - x.min()) / x.max()- x.min())
#for fitur in list_fitur:
#max = df_input.groupby('Class')[fitur].max()
#min = df_input.groupby('Class')[fitur].min()
#df_input[fitur] = (df_input[fitur]-min)/(max-min)
return df_input
"""<h1>3. Pemisahan Data Latih</h1>"""
from sklearn.model_selection import train_test_split
train70, test30 = train_test_split(data_impu, test_size=0.3)
train80, test20 = train_test_split(data_impu, test_size=0.2)
trNom70 = minmax(train70)
trNom80 = minmax(train80)
teNom30 = minmax(test30)
teNom20 = minmax(test20)
from scipy import stats as s
class voteELM:
W = []
beta = []
mape = []
def __fitELM(self,train,h,b1,b2):
t = train['Class']
t = np.stack(t.values)
train = train.drop(columns=['Class'])
d = len(train.columns)
X = train.values
W = np.random.uniform(b1,b2, (h,d))
Hinit = X @ W.T
H = 1/(1+np.exp(-1*Hinit))
Hplus = np.linalg.inv(H.T @ H) @ H.T
beta = Hplus @ t
y = H @ beta
return W, beta, t, y
def fit(self,train,k=5,h=4,b1=-0.5,b2=0.5):
for i in range(k):
wf, bf, tf, yf = self.__fitELM(train,h,b1,b2)
self.W.append(wf)
self.beta.append(bf)
MAPE = sum(abs(yf-tf)/yf)*1/len(tf)
self.mape.append(MAPE)
def test(self,test):
t = test['Class']
t = np.stack(t.values)
y = []
yt = []
vote = []
Xt = test.drop(columns=['Class'])
Xt = Xt.values
for idx,e in enumerate(self.W):
Hinit = Xt @ e.T
H = 1/(1+np.exp(-1*Hinit))
yt = H @ self.beta[idx]
yt = np.around(yt)
y.append(yt)
yt = list(zip(*y))
pred = []
for x in yt:
mode = int(s.mode(x)[0])
pred.append(mode)
return pred
import time
model = voteELM()
model1 = voteELM()
st1 = time.time()
model.fit(trNom80,5,500)
et1 = time.time()
st2 = time.time()
model1.fit(trNom70,5,500)
et2 = time.time()
pred = model.test(teNom20)
pred1 = model1.test(teNom30)
from sklearn.metrics import accuracy_score
y_test = teNom20['Class'].tolist()
y_test1 = teNom30['Class'].tolist()
print("Akurasi V-ELM dengan rasio pelatihan 80:20 : ",accuracy_score(y_test,pred))
print("Akurasi V-ELM dengan rasio pelatihan 70:30 : ",accuracy_score(y_test1,pred1))
print("Waktu Pelatihan V-ELM dengan rasio pelatihan 80:20 : ",et1-st1)
print("Waktu Pelatihan V-ELM dengan rasio pelatihan 70:30 : ",et2-st2)
print("Nilai MAPE pada Pelatihan 80:20 : ", model.mape[:5])
print("Nilai MAPE pada Pelatihan 70:30 : ", model1.mape[5:])