LFD Book Forum Chapter 1 Problem 1.5
 Register FAQ Calendar Mark Forums Read

#1
09-20-2018, 03:20 PM
 venkatesh-devale Junior Member Join Date: Sep 2018 Posts: 5
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?

 Tags problem 1.5, variation of adaline

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home General     General Discussion of Machine Learning     Free Additional Material         Dynamic e-Chapters         Dynamic e-Appendices Course Discussions     Online LFD course         General comments on the course         Homework 1         Homework 2         Homework 3         Homework 4         Homework 5         Homework 6         Homework 7         Homework 8         The Final         Create New Homework Problems Book Feedback - Learning From Data     General comments on the book     Chapter 1 - The Learning Problem     Chapter 2 - Training versus Testing     Chapter 3 - The Linear Model     Chapter 4 - Overfitting     Chapter 5 - Three Learning Principles     e-Chapter 6 - Similarity Based Methods     e-Chapter 7 - Neural Networks     e-Chapter 8 - Support Vector Machines     e-Chapter 9 - Learning Aides     Appendix and Notation     e-Appendices

All times are GMT -7. The time now is 02:14 AM.