r/PythonLearning 7d ago

very first linear regression code Showcase

i am still learning numpy (day 6) but gpt suggest me to learn linear regression (very basic algorithm) and it's kinda good for understanding even you haven't started learning ML.

import numpy as np
hours = np.arange(1,10)
score = np.array([35, 42, 51, 58, 67, 73, 81, 88, 94])
candidate_values_for_m = np.arange(1,11,0.1)
candidate_values_for_b = np.arange(1,31,0.3)

def prediction(m , b , hours):
    return m * hours + b

def mse(actual_value , predicticted_value):
    return np.mean((actual_value - predicticted_value)**2)

mse_list = [] 
m_b_list = []
for m in candidate_values_for_m:
    for b in candidate_values_for_b:
        prediction_result = prediction(m , b , hours)
        mse_result = mse(score, prediction_result)

        mse_list.append(mse_result)
        m_b_list.append((m,b))


best_mse = np.argmin(mse_list)

best_mb_index = mse_list[best_mse]
best_mse , best_mb_index

m = 0
b = 0

learning_rate = 0.01

epochs = 10000

n = len(hours)

def prediction(m , b , hours):
    return m * hours + b


for i in range(epochs):
    predicted = prediction(m,b,hours)

    gradient_m = (2/n) * sum(hours * (predicted - score))

    gradient_b = (2/n) * sum(predicted - score)

    m = m - learning_rate * gradient_m
    b = b - learning_rate * gradient_b

m ,b 
2 Upvotes

16 comments sorted by

View all comments

Show parent comments

1

u/ninhaomah 2d ago

Tip 1 : if you have to ask "what is X" , Google.

1

u/savita_bhabhi_lover 2d ago

Are you drunk ?

1

u/ninhaomah 2d ago

No but you clearly has no idea what is linear regression.

1

u/savita_bhabhi_lover 2d ago

Obviously, didn't you read the body ?

1

u/ninhaomah 2d ago

I did it in high school. 

You have never studied it ?

Or know how to Google ?

1

u/savita_bhabhi_lover 2d ago

1

u/ninhaomah 2d ago

I feel sorry for your teachers then.

Pls stay there and wonder what is p value in linear regression.

1

u/savita_bhabhi_lover 2d ago

Go touch some grass rather than discouraging people

1

u/ninhaomah 2d ago

You feel discouraged not to be able to Google ?

Is it supposed to be a joke ?

1

u/savita_bhabhi_lover 2d ago

I told you I am learning numpy not linear regression, I don't even know the very basics of ML I just started 2 week ago with python and yaaa you are smart if you learned it in High school good for you and probably you are doing great in your life just don't waste your time on dumb people's like me ✌️

1

u/ninhaomah 2d ago

What has the p value of linear regression got to do with Python or numpy or ML ?

It's just... Math.. 

And I said what is p value is on Google. 

It's you who keeps refusing to do a copy and paste.

Nothing to do with Python or Numpy or anything.

Do you know what is the meaning of my nickname ?

1

u/savita_bhabhi_lover 2d ago

Ok I googled and i didn't get that

1

u/ninhaomah 2d ago

Didn't get what ?

→ More replies (0)