LFD Book Forum  

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

Reply
 
Thread Tools Display Modes
  #1  
Old 09-20-2018, 03:20 PM
venkatesh-devale venkatesh-devale is offline
Junior Member
 
Join Date: Sep 2018
Posts: 4
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):
train_X1.append(random.uniform(0,1))
train_Y1.append(random.uniform(0,1))
train_X2.append(random.uniform(-1,0))
train_Y2.append(random.uniform(-1,0))

#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]
#print(self.W)
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
  #2  
Old 09-22-2018, 01:59 PM
htlin's Avatar
htlin htlin is offline
NTU
 
Join Date: Aug 2009
Location: Taipei, Taiwan
Posts: 580
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
  #3  
Old 10-09-2018, 11:34 AM
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
Reply

Tags
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:57 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2018, 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.