Code Monkey home page Code Monkey logo

linearregression_python's Introduction

LinearRegression_Python

Header

Hello, World! I am the DataByte. This repo hosts my scripts (Python) that I walk through in my linear regression series on YouTube.

Check out my video Linear Regression with Excel, Python & R to hear more about these topics:

Overview

Mathematics

In my YouTube video, I give a short mathematical explanation of how linear regression trendlines are calculated with the least squares method.

R2 value

The r2 value (the coefficient of determination) helps you determine if your data is exhibiting linear behavior or not. R2 values range between 0 and 1, with 1 indicating a perfect linear trend. Values beteween 0.7 and 1 indicate that your data has a linear trend, the closer to 1, the better. You probably don't want to be using any linear trendlines with r2 values less than 0.7 to make predictions.

Code

Linear regression in Python is easy using the OLS() function in the statsmodels library.

model = sm.OLS(y, x).fit()

Make sure you add a constant to your dataset with x = sm.add_constant(x) if you want the line of best fit to not just automatically assume that the y-intercept is equal to zero. If you want the point (x = 0, y = 0) to be included on your line of best fit, then you don't need to add this function.

Calling summary() will give you a description of your model.

Linear regression can help you capture the general trend of your data, but if the r2 value is less than 0.7, it's not going to be very much good for making predictions.

Graph_2022_YTD

Sometimes, you may be able to find a subset in your dataset that may still exhibit strongly linear characteristics, like Tesla stock's closing prices between March 14th and April 1st, 2022. It's an interesting observation, but if you told anyone you were trading off a linear model, they'd probably think you were crazy!

LM_Summary

Graph_2022_YTD

Dependencies

None

References

It is easy to find sources online to learn more about linear regression, here are some great references to what I used.

Python:

stasmodels.regression.linear_model.OLS

matplotlib.pyplot.plot

Mathematical background:

Miller, I., Freund, J. E., & Johnson, R. A. (2005). Miller and Freund's Probability and statistics for engineers. Upper Saddle River, NJ: Prentice Hall.

Sigma summation

More?

If you want to hear more from me, check out my Hello, World! video on YouTube and subscribe! I'm constantly working on new content :)

linearregression_python's People

Contributors

thedatabyte avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.