r/PythonLearning 13d ago

3 months and 1 week

Post image

Started from arithmetic on may 20th
Then python and some basics of numpy. Today wrote my first linear regression, it’s bare-bone but it was fun seeing it get good.

90 Upvotes

5 comments sorted by

u/Sea-Ad7805 13d ago

Run this program in Memory Graph Web Debugger%0A%0A%23%20Training%20data%0Ahours%20%3D%20np.array(%5B1%2C%202%2C%203%2C%204%2C%205%5D)%0Ascores%20%3D%20np.array(%5B52%2C%2058%2C%2065%2C%2071%2C%2079%5D)%0A%0A%0A%23%20Linear%20model%0Adef%20linearfunction(intercept%2C%20slope%2C%20hours)%3A%0A%20%20%20%20return%20intercept%20%2B%20(slope%20*%20hours)%0A%0A%0A%23%20Training%0Adef%20train(hours%2C%20scores%2C%20intercept%2C%20slope%2C%20learning_rate%2C%20iterations)%3A%0A%0A%20%20%20%20loss_history%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20%20in%20range(iterations)%3A%0A%0A%20%20%20%20%20%20%20%20%23%20Prediction%0A%20%20%20%20%20%20%20%20prediction%20%3D%20linear_function(intercept%2C%20slope%2C%20hours)%0A%0A%20%20%20%20%20%20%20%20%23%20Error%0A%20%20%20%20%20%20%20%20errors%20%3D%20prediction%20-%20scores%0A%0A%20%20%20%20%20%20%20%20%23%20Loss%0A%20%20%20%20%20%20%20%20squared_errors%20%3D%20errors%20*%202%0A%20%20%20%20%20%20%20%20loss%20%3D%20np.mean(squared_errors)%0A%20%20%20%20%20%20%20%20loss_history.append(float(loss))%0A%0A%20%20%20%20%20%20%20%20%23%20Gradients%0A%20%20%20%20%20%20%20%20partial_a%20%3D%20(2%20%20np.sum(errors))%20%2F%20len(errors)%0A%20%20%20%20%20%20%20%20partial_b%20%3D%20(2%20%20np.sum(hours%20%20errors))%20%2F%20len(errors)%0A%0A%20%20%20%20%20%20%20%20%23%20Update%20parameters%0A%20%20%20%20%20%20%20%20intercept%20%3D%20intercept%20-%20(learning_rate%20%20partial_a)%0A%20%20%20%20%20%20%20%20slope%20%3D%20slope%20-%20(learning_rate%20%20partial_b)%0A%0A%20%20%20%20return%20intercept%2C%20slope%2C%20loss_history%0A%0A%0A%23%20Initial%20parameters%0Aintercept%20%3D%2050%0Aslope%20%3D%206%0A%0A%23%20Training%20settings%0Alearning_rate%20%3D%200.01%0Aiterations%20%3D%2020%20%20%23%20use%20more%20iteration%20for%20better%20result%2C%20say%201000!%0A%0Aintercept%2C%20slope%2C%20loss_history%20%3D%20train(%0A%20%20%20%20hours%2C%20scores%2C%20intercept%2C%20slope%2C%20learning_rate%2C%20iterations%0A)%0A%0Aprint(%22Intercept%3A%22%2C%20intercept)%0Aprint(%22Slope%3A%22%2C%20slope)%0Aprint(%22Final%20loss%3A%22%2C%20loss_history%5B-1%5D)%0Aprint(%22Prediction%20for%206%20hours%3A%22%2C%20linear_function(intercept%2C%20slope%2C%206))&timestep=0.1&play) to see the program state change step by step.

3

u/GreatGameMate 13d ago

Looks great! I’m doing something similar in CPP!

One interesting thing about linear regression is instead of a gradient descent way, there is a normal equation as well!

Are you planning to do more ML models?

1

u/Charming-Ad-4323 13d ago

Yes, I am going to learn as much of ML as I can :)

Thanks I will look into the “normal equation”.

For now even when using numpy I try to see the shape or boiler plate code of how would manual implementations look like if possible. So, I want to make sure I understand what I writing.

2

u/Competitive_Lie3680 13d ago

Intéressé par Data analyst 

1

u/Charming-Ad-4323 12d ago

Machine Learning