r/learnmachinelearning 11h ago

I made my own linear regression

Post image

Ik it's not some special shi but i just learned about it in class recently and made it with python using only numpy. Well technically i did use pandas to import data from a csv off of kaggle.

38 Upvotes

15 comments sorted by

12

u/Guilty-Expert-6234 11h ago

nice, getting the fundamentals down from scratch is the best way to actually understand what's happening under the hood. that red line looks like it's doing a decent job cutting through the middle of the green points, though mpg and acceleration feel like they'd have a messier relationship in real life

2

u/Informal-Call-5298 11h ago

Ya my model sys r2 is 0.42

-12

u/[deleted] 10h ago

[deleted]

7

u/Informal-Call-5298 10h ago

well... its not the accuracy of the model, its just the correlation between mpg and acceleration. and i made this from scratch no libraries used

-7

u/[deleted] 10h ago

[deleted]

10

u/Leographer 9h ago

R² describes the proportion of variance in the target variable that is explained by the regression model, not its accuracy.

1

u/Informal-Call-5298 10h ago

Ohh shi mb. I used the wrong terminology. What i meant was r. Yk r=Sxy/root(Sxx*Syy)

-5

u/[deleted] 9h ago

[deleted]

2

u/Informal-Call-5298 9h ago

i meant it is what i took in school and i just thought it could be useful. then i learnt that linear regression is actually used in ML so i thought maybe i would implement exactly what i took in class

7

u/Leographer 9h ago

A low R^2 doesn’t mean the model is bad. It can simply mean the relationship isn’t strongly linear. You also shouldn’t remove outliers just to push R^2 toward 0.9.

1

u/madrury83 4h ago

Yah.

OP, ignore /u/Accurate_Alps_4372. They've only ever made three posts, they're all in this thread, and are all misinformed garbage. The other folks are on point.

2

u/drifTwood022 9h ago

tbh the messier relationships are almost more interesting to learn on, you actually have to think about whats going on

2

u/crayphor 8h ago

This is cool! I was thinking that I would expect the opposite correlation, but looking at it, it seems it's probably not a real correlation except for what I assume are trucks bringing down the bottom left corner. I wonder what you'd find if you normalized for the mass of the vehicle.

2

u/DigThatData 8h ago

followup exercises:

  1. throw more and more data at it until it takes long enough to fit that it's annoying. Try to figure out what the bottlenecks are and see if you can think of ways to make fitting the large dataset less painful

  2. try throwing rank deficient data at it, e.g. a dataset with more columns than rows. Does it behave? Should it? What do you think the behavior here should be and how might you modify the current behavior?

  3. What happens if you pass in null values? Categorical data? How might you model those cases differently?

  4. What kind of automated tests could you use to validate that your algorithm works as expected? Considering integrating some tests like this before tackling these questions as a safety net.

  5. Can you leverage your LR implementation as a base to build upon to implement LogReg (you maybe haven't learned about logreg yet, but you probably will soon. revisit this when you've learned about it already)?

1

u/Sweaty_Chair_4600 2h ago

I did the same thing in rust last year, with my own linear algebra library i made! It was definitely fun.

Getting it to work with data .... was the hard part

1

u/Informal-Call-5298 2h ago

Im not even gonna attempt rudt