Tugas Perceptron Dan BP Ann - A - f1d019055 - M. Arfriyandri

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Dataset

In [2]: x = [[1, 0, 0, 0, 1, 0],


[0, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0],
[0, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 1, 0, 0],
[1, 0, 0, 1, 0, 1],
[0, 1, 1, 1, 1, 1]]

y = ['A','B','A','A','A','A','B','B','B','B']
weights = [0,0,0,0,0,0]

#mengubah target(y) menjadi numerik A=1 dan B=-1


import numpy as np
y = np.array(y)
t = np.arange(len(y))
t[:] = 1
t[np.where(y == 'B')] = -1
t = t.tolist()
t

Out[2]: [1, -1, 1, 1, 1, 1, -1, -1, -1, -1]

Perceptron
In [3]: #activation function dengan fungsi aktivasi bipolar step function bipolar
def actFunction(dt):
return 1.0 if dt >= 0.0 else -1.0

# Make a prediction with weights


def predict(row, weights):
activation = weights[0] #Bias
for i in range(len(row)-1):
activation += weights[i + 1] * row[i] #activation = bias + (w1 * X1) + (w2 *
y=actFunction(activation)
return y

def train_weights(train, l_rate, n_epoch):


weights = [0.0 for i in range(len(train[0]))]
for epoch in range(n_epoch):
sum_error = 0.0
for row in train:
prediction = predict(row, weights)
error = row[-1] - prediction
sum_error += error**2
weights[0] = weights[0] + l_rate * error
for i in range(len(row)-1):
weights[i + 1] = weights[i + 1] + l_rate * error * row[i]
print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))
return weights

def testing (data):


for row in data:
prediction = predict(row, weights)
print("Expected=%d, Predicted=%d" % (row[-1], prediction))
In [8]: l_rate = 0.8
n_epoch = 10
weights = train_weights(x, l_rate, n_epoch)
print(weights)

>epoch=0, lrate=0.800, error=13.000


>epoch=1, lrate=0.800, error=17.000
>epoch=2, lrate=0.800, error=5.000
>epoch=3, lrate=0.800, error=13.000
>epoch=4, lrate=0.800, error=9.000
>epoch=5, lrate=0.800, error=9.000
>epoch=6, lrate=0.800, error=9.000
>epoch=7, lrate=0.800, error=9.000
>epoch=8, lrate=0.800, error=5.000
>epoch=9, lrate=0.800, error=13.000
[1.6000000000000003, 3.2, 3.2, 3.2, -2.220446049250313e-16, -1.6]

In [7]: testing(x)

Expected=0, Predicted=1
Expected=0, Predicted=1
Expected=1, Predicted=1
Expected=0, Predicted=1
Expected=1, Predicted=1
Expected=1, Predicted=1
Expected=0, Predicted=1
Expected=0, Predicted=1
Expected=1, Predicted=1
Expected=1, Predicted=1

Back Propagation
In [5]: #mengimport library
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

In [6]: dataset = train_test_split(x,t,test_size = 0.2,stratify = t)


trainX, testX, trainY, testY = dataset

In [7]: #membuat ANN dengan fungsi aktivasi logistic function


BP = MLPClassifier(learning_rate_init=0.5, activation='logistic',hidden_layer_sizes=

#trainning data
BP.fit(trainX, trainY)

Out[7]: MLPClassifier(activation='logistic', hidden_layer_sizes=(10, 6),


learning_rate_init=0.5, max_iter=100, random_state=1,
solver='lbfgs', verbose=0)

In [8]: def generateClassificationReport(y_test,y_pred):


print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
print('accuracy is ',accuracy_score(y_test,y_pred))

#Prediction Training
print("\n--- PREDICTION TRAINING ---" )
pred_y = BP.predict(trainX)
generateClassificationReport(trainY,pred_y)

--- PREDICTION TRAINING ---


precision recall f1-score support
-1 1.00 1.00 1.00 4
1 1.00 1.00 1.00 4

accuracy 1.00 8
macro avg 1.00 1.00 1.00 8
weighted avg 1.00 1.00 1.00 8

[[4 0]
[0 4]]
accuracy is 1.0

In [ ]:

You might also like