Supervised Learning:Linear Regression - with Bias ( simple basic program) - : Predicating No of Pizza, based on one reservation parameters

Hi Friends
I was trying out linear regression in a basic way. As wanted to understand the blocks.

So am using a trial and error method to get the right iterations to train the system.

How do get the correct converge, ie. right iteration, tried various numbers already.

I am getting following error: -Couldn’t converge within 183 iterations

I have changed the different combination of iteration, still the error remains the same.

Am pasting the code below.

data:(pizza file)
Reservations Pizzas
13 33
2 16
14 32
23 51
13 27
1 16
18 34
10 17
26 29
3 15
3 15
21 32
7 22
22 37
2 13
27 44
6 16
10 21
18 37
15 30
9 26
26 34
8 23
15 39
10 27
21 37
5 17
6 18
13 25
13 23

#defining the predict function, loss function, train the system function.

def predict_b (X, w, b):
return X * w +b

def loss_b(X, Y, w, b):

return np.average((predict_b(X, w, b) - Y) ** 2)

def train_b (X, Y, iterations, lr):

w = b= 0
for i in range(iterations):
current_loss = loss_b(X, Y, w, b)
print(“Iteration%4d=> Loss: %.6f”% (i, current_loss))
if loss_b(X, Y, w + lr, b) < current_loss:
w += lr
elif loss_b(X, Y, w - lr,b) < current_loss:
w -= lr
elif loss_b(X, Y, w,b, + lr) < current_loss:
b += lr
elif loss_b(X, Y, w,b, - lr) < current_loss:
b -= lr
else:
return w,b
raise Exception(“Couldn’t converge within %d iterations” % iterations)

Import the datasets

X, Y = np.loadtxt(“pizza.txt”, skiprows=1, unpack=True)

Train the system

w, b = train_b(X, Y, iterations=183, lr=0.01)
print(“nw=%.3f, b=%.3f” % (w, b))

Predict the number of pizzas

print(“Prediction: x=%d => y=%.2f”% (20, predict(20, w, b)))

this is error output

Iteration 0=> Loss: 812.866667

Exception Traceback (most recent call last)
in
4
5 # Train the system
----> 6 w, b = train_b(X, Y, iterations=183, lr=0.01)
7 print(“nw=%.3f, b=%.3f” % (w, b))
8

in train_b(X, Y, iterations, lr)
14 else:
15 return w,b
—> 16 raise Exception(“Couldn’t converge within %d iterations” % iterations)

Exception: Couldn’t converge within 183 iterations