LFD Book Forum

LFD Book Forum (http://book.caltech.edu/bookforum/index.php)
-   Chapter 1 - The Learning Problem (http://book.caltech.edu/bookforum/forumdisplay.php?f=108)
-   -   Chapter 1 Problem 1.5 (http://book.caltech.edu/bookforum/showthread.php?t=4857)

venkatesh-devale 09-20-2018 04:20 PM

Chapter 1 Problem 1.5
Hello All,

I am simply generating data points in first and third quadrant with below code:
import random
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.unicode_minus'] = False

#generate a data set of 100.
#for simplicity, 50 in the first quadrant, another 50 in the third quadrant
train_X1 = []
train_Y1 = []
train_X2 = []
train_Y2 = []

for i in range(50):

#label the data
train_data1 = [np.array([1,train_X1[i],train_Y1[i],1]) for i in range(50)]
train_data2 = [np.array([1,train_X2[i],train_Y2[i],-1]) for i in range(50)]
train_data = train_data1 + train_data2

I have my perceptron algorithm as below:

#Problem 1.5
class Perceptron(object):
def __init__(self, data):
self.W = np.zeros(len(data[0:3]))
self.update = 0
self.learning_rate = 0.01

def predict(self, x):
activation = np.dot(self.W.T,x)
return np.sign(activation)

def fit(self, data):
count = 0
X = np.array(data)[:,0:3]
d = np.array(data)[:, 3:4]
while self.update < 1000:
#self.update = 0
for i in range(len(data)):
predicted_value_y = self.predict(X[i])
expected_value = d[i]
if expected_value * predicted_value_y <=1:
self.W = self.W + self.learning_rate*(expected_value -
predicted_value_y) * X[i]
self.update += 1

print("Number of iterations for converging:",count)

For me it seems correct according to mention of update rule in problem 1.5 in book.

But some how even if my learning rate changes my target function 'g' does not change and classifies some data points incorrectly.

Is this expected?

htlin 09-22-2018 02:59 PM

Re: Chapter 1 Problem 1.5
You should perhaps considering picking random points within each iteration instead of following the original (data) order of points. Hope this helps.

nickarab 10-09-2018 12:34 PM

Re: Chapter 1 Problem 1.5
Thanks I got it, Thank you so much! It's great to have you guys around. There will be a lot of questions from me on this book as I am going through it and have no background.

All times are GMT -7. The time now is 10:28 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.