LFD Book Forum  

Go Back   LFD Book Forum > Book Feedback - Learning From Data > Chapter 1 - The Learning Problem

Thread Tools Display Modes
Old 09-20-2018, 04:20 PM
venkatesh-devale venkatesh-devale is offline
Junior Member
Join Date: Sep 2018
Posts: 5
Default 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?
Reply With Quote
Old 09-22-2018, 02:59 PM
htlin's Avatar
htlin htlin is offline
Join Date: Aug 2009
Location: Taipei, Taiwan
Posts: 610
Default 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.
When one teaches, two learn.
Reply With Quote
Old 10-09-2018, 12:34 PM
nickarab nickarab is offline
Junior Member
Join Date: Oct 2018
Posts: 1
Default 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.
Reply With Quote

problem 1.5, variation of adaline

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 10:56 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.