How to easily code a Linear Regression model using TensorFlow and python! (Pt1)
TensorFlow is one of the most used machine learning libraries in the world. But it doesn't have to be that hard. Get started with Linear Regression:
With the advent of artificial intelligence many libraries were developed to help us make use of the many algorithms you can find in the world of machine learning. Unfortunately, most of those algorithms may appear to be overly difficult, full of math and boring stuff, which can be intimidating for beginners. As I'm a fan of friendly approaches for difficult topics, I decided to write an article about one of the most important algorithms in the world of machine learning, the linear regression.
What's Linear Regression anyway?
Linear Regression is one of the most used algorithms in the world of machine learning. It's basically used to predict values based on a given input. For example, marketers could use it to predict in which ad they should spend their money with, biologists could use it to find correlations between an illness and its pathological agent. You basically could use it whenever there's a linear relationship between the data ingested as the independent variable (represented as X) and the dependent variable (represented as Y). For example, if you spend 1 hour playing an online game and it makes you gain 2 levels, and you spend 2 hours playing the same game and it makes you gain 4 levels, it'd make sense to assume that if you play this game for 3 hours you'd get to the level 6 , right? That's exactly how linear regression works, you use it to predict a value based on past input and the outcome it generated.
How is it represented?
If you studied linear functions in high school you'll have no problems understanding its plotting. Its represented as a line that crosses many dots. These dots are the data ingested, and their position in Y is the result of their value in X. Among these dots you cross a line, and that line is basically an average of all these dots. So if you input a new data as X in the future, you'll just have to look at the line to "guess" which value in Y it approximately will take.
How is this line calculated?
Just like most of the machine learning algorithms, linear regression is calculated in 4 easy steps. First you need to ingest data. Second, you need to choose the model that will work best in your data. In our case of course, we chose linear regression. Third, you need an objective function. Those functions work by calculating the error between the value you predicted and the real value. In our case for linear regression models, the mean squared error function works really well. Lastly, you need an optimization algorithm. This function works by reducing the error that you got using the objective function. As the error is the difference between the value you predicted and the real value, the most you reduce this error, the better your predictions will be. In our case, we're going to use the gradient descent function.
Memorize well this 4 steps, as they will be used in most algorithms in machine learning. Of course for different data you'll have to choose different algorithms, but the steps remain the same.
data => model => objective function => optimization algorithm
Of course to really understand these function well you need to get deeper in the world of mathematics. But gladly, unless you are a machine learning engineer, designing functions from scratch is not required, and you can just use functions that other people already designed for you.
Conclusion
In this tutorial we talked about the linear regression model and its use. In the next tutorial, well learn a little bit more about the linear model, the mean squared error function and the gradient descent optimization algorithm. See you on the next tutorial.
FML
6yDear Romullo, thanks for your explanation that makes it much easier to understand.
Technology Consulting Senior Manager - SAP Intelligent Enterprise at PwC
6yGreat block ...Keep posting .!!
Economista. Econometria. Análise de dados. Economist. Data analytics. Econometrics.
6yCongrats for your explanations, Romullo! They were very didatic and very useful.
Software Engineer
6yRomullo Bernardo that is awesome. I have never thought such easy way to understand the linear regression. I'm really glad because I know that I can use it easy in my projects. Perfect.
IT Specialist, PhD in Information Science
6yRomullo Bernardo so many companies are already using machine learning algorithms in order to make predictions about next sales, next customer step and next market behaviors. This is not magic, this is science! Congratulations. Thanks for giving us a friendly approache. I have already memorized: data => model => objective function => optimization algorithm.