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3.9,4.8,4,4.9,4.7,4.3,4.4,4.8,5,4.5,3.5,3.8,3.7,3.9,5.1,4.5,4.5,4.7,4.4,4.1,4,4.4,4.6,4,3.3,4.2,4.2,4.2,4.3,3,4.1]
D = [0.2,0.2,0.2,0.2,0.2,0.4,0.3,0.2,0.2,0.1,0.2,0.2,0.1,0.1,0.2,0.4,0.4,0.3,0.3,0.3,0.2,0.4,0.2,0.5,0.2,0.2,0.4,0.2,0.2,0.2,0.2,0.4,0.1,0.2,0.2,0.2,0.2,0.1,0.2,0.2,0.3,0.3,0.2,0.6,0.4,0.3,0.2,0.2,0.2,0.2,1.4,1.5,1.5,1.3,1.5,1.3,1.6,1,1.3,1.4,1,1.5,1,1.4,1.3,1.4,1.5,1,1.5,1.1,1.8,1.3,1.5,1.2,1.3,1.4,1.4,1.7,1.5,1,1.1,1,1.2,1.6,1.5,1.6,1.5,1.3,1.3,1.3,1.2,1.4,1.2,1,1.3,1.2,1.3,1.3,1.1,1.3]
A, B, C, D = [float(i) for i in A], [float(i) for i in B], [float(i) for i in C], [float(i) for i in D]
x1, x2, x3, x4 = A, B, C, D
featureLength = len(A)
t = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
w1 = float(0.8)
w2 = float(0.8)
w3 = float(0.8)
w4 = float(0.8)
alpha = float(0.4)
a = []
b = []
error_A, error_B, error_C, error_D = [], [], [], []
avgErrorA, avgErrorB, avgErrorC, avgErrorD = 0, 0, 0, 0
totalError = []
avgError = float(0)
w1Delta, w2Delta, w3Delta, w4Delta = float(0.0), float(0.0), float(0.0), float(0.0)
EPOCH = 1
FINAL_EPOCH = 0
ITER_ = 1
for iter_ in range(ITER_):
for i in range(featureLength):
if EPOCH == 1:
# mencari a / summation
a.append(x1[i]*w1 + x2[i]*w2 + x3[i]*w3 + x4[i]*w4)
# mencari b / activation function (sigmoid)
b.append(1/(1+exp(-a[i])))
# mencari masing-masing feature error
error_A.append((b[i]-t[i])*A[i])
error_B.append((b[i]-t[i])*B[i])
error_C.append((b[i]-t[i])*C[i])
error_D.append((b[i]-t[i])*D[i])
# mencari a /summation
a[i] = x1[i]*w1 + x2[i]*w2 + x3[i]*w3 + x4[i]*w4
# mencari b / activation function (sigmoid)
b[i] = 1/(1+exp(-a[i]))
# mencari masing-masing feature error
error_A[i] = (b[i]-t[i])*A[i]
error_B[i] = (b[i]-t[i])*B[i]
error_C[i] = (b[i]-t[i])*C[i]
error_D[i] = (b[i]-t[i])*D[i]
# mencari rata-rata error pada masing-masing feature
avgErrorA = sum(error_A)/featureLength
avgErrorB = sum(error_B)/featureLength
avgErrorC = sum(error_C)/featureLength
avgErrorD = sum(error_D)/featureLength
if EPOCH == 1:
#mencari total error untuk masing-masing feature
totalError.append(-t[i]*log(b[i])+(1-t[i])*log(1-b[i]))
#totalError[i] = -t[i]*log(b[i])+(1-t[i])*log(1-b[i])
avgError = sum(totalError)/featureLength
for i in range(featureLength):
# mencari nilai wDelta
w1Delta = w1-alpha*error_A[i]
w2Delta = w2-alpha*error_B[i]
w3Delta = w3-alpha*error_C[i]
w4Delta = w4-alpha*error_D[i]
# mengupdate nilai w
w1, w2, w3, w4 = w1Delta, w2Delta, w3Delta, w4Delta
if avgError <= 0.1:
FINAL_EPOCH = EPOCH
break
print("Epoch ke - ", FINAL_EPOCH, \
"\nError = ", avgError, \
"\nw1 = {0}\nw2 = {1}\nw3 = {2}\nw4 = {3}\n\n".format(w1, w2, w3, w4))
import math
import numpy as np
import time
totalFeatures = 4
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4,1.4],
[5.6,3.0,4.5,1.5],
[5.8,2.7,4.1,1.0],
[6.2,2.2,4.5,1.5],
[5.6,2.5,3.9,1.1],
[5.9,3.2,4.8,1.8],
[6.1,2.8,4.0,1.3],
[6.3,2.5,4.9,1.5],
[6.1,2.8,4.7,1.2],
[6.4,2.9,4.3,1.3],
[6.6,3.0,4.4,1.4],
[6.8,2.8,4.8,1.4],
[6.7,3.0,5.0,1.7],
[6.0,2.9,4.5,1.5],
[5.7,2.6,3.5,1.0],
[5.5,2.4,3.8,1.1],
[5.5,2.4,3.7,1.0],
[5.8,2.7,3.9,1.2],
[6.0,2.7,5.1,1.6],
[5.4,3.0,4.5,1.5],
[6.0,3.4,4.5,1.6],
[6.7,3.1,4.7,1.5],
[6.3,2.3,4.4,1.3],
[5.6,3.0,4.1,1.3],
[5.5,2.5,4.0,1.3]], np.float32)
t = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1], np.float32)
# inisialisasi bobot & bias
W = [0.8, 0.8, 0.8, 0.8]
b = np.zeros(1, np.float32)
for i in range(iteration):
# dot product / summation
a = np.dot(x, W) + b
# activation function (sigmoid)
a = 1.0 / (1.0 + np.exp(-a))
# error
err = t - a
# menghitung delta w dan update w
w_delta = alpha * np.dot(np.transpose(x) , err)
delta_b = alpha * np.sum(err)
W = W + w_delta
b = b + delta_b
if np.abs(np.sum(err) / np.sum(t)) <= 0.1:
break
print ("Epoch ke - " + str(i), "\nError: " + str(np.abs(np.sum(err) / np.sum(t))))
print("\nw1 = ", W[0], "\nw2 = ", W[1], "\nw3 = ", W[2], "\nw4 = ", W[3])