Memuat
Memuat
Want to go deeper?
Join one of our courses or ask us directly on WhatsApp.
Picked from the software, topic, and level of “Machine Learning Python Class #5: Linear Regression & Polynomial Regression: Sales Prediction”.
Other ways we can help with Python.
Learn with a mentor
Scheduled AI & Machine Learning courses with live guidance and a certificate.

Multi-Objective Optimization Course: NSGA-II, MOPSO & Hybrid Algorithms (Python)

ECG Arrhythmia Classification Course with Wavelet & Deep Learning (PyTorch)

ANFIS + PSO/GA Course for Non-Linear Time Series Forecasting (Python)
Have it built for you
No time to learn Python? Our team can do the project for you.

Python Machine Learning — Prediction Models, Customer Segmentation, Fraud Detection & AI Apps

AI Image Creation with Stable Diffusion — Unlimited Custom Visual Production
★ 4.9

AI Image & Video Creation with Leonardo AI — All-in-One Visuals for Business
★ 4.9
Get notified about new Python classes
At most one email a week. Unfollow anytime with one click.
The full narration of this video. The current sentence is highlighted while the video plays; click any sentence to jump there.
Here is the final result, three graphs explaining one of the most important machine learning concepts: underfitting, good fit, and overfitting. Previously, we also built a regression model that predicts sales from TV, radio, and newspaper ad costs, complete with how to read coefficients and evaluate the model. Let's get started. Hello, welcome to episode five of the machine learning with Python series. We're entering the first part of supervised learning, regression, which is predicting numerical values such as price, temperature, or sales. We start with simple linear regression, move to multiple regression, learn to evaluate models with MAE, RMSE, and R², and then handle curved relationships with polynomial regression. We create the file ad.csv.
The data consists of 200 stores with TV, radio, and newspaper ad costs, along with their sales, in millions of rupiah. We also save it as a CSV so it can be used by other files. The question is, if ad costs are increased, how much will sales increase?
Simple linear regression finds the best-fitting straight line, Y hat equals W times X plus B. We train a linear regression model with one feature, TV ads. Note that X is written with double brackets because scikit-learn requires X to be 2-dimensional. The coefficient is around 0.5. Every additional 1 million rupiah in TV ads increases sales by about half a million rupiah. The graph shows a red line splitting the cloud of data points.
Sales are usually influenced by many factors, so we use multiple regression. Create the file multiple.py. We use all three features, split the data into training and test sets, and then train the model. Look at the coefficients: TV is around 0.45, radio is almost 2, and newspaper is close to 0. This means that per rupiah, radio ads are the most effective, while newspaper ads have no impact. The business recommendation is clear: shift the newspaper ad budget to radio.
The model must be evaluated on the test data. MAE is the average of absolute errors, RMSE punishes large errors more heavily, Both in million rupiah units. R squared indicates how much of the sales variation is explained by the model, ranging from 0 to 1. Always compare with the baseline, which is just guessing the average. The baseline has an R squared around 0, even slightly negative, while our model is 0.94.
We check the residuals, which is the difference between the actual and predicted values. The left graph compares the actual and predicted values, with points closely clustered around the diagonal line. The right graph shows residuals randomly spread around 0 without a pattern. This indicates that the linear regression assumptions are satisfied.
What if the relationship is curved? Create a file named polynomial.py We generate 20 data points from a sine curve. The trick of polynomial regression is adding power features, such as x squared, x cubed, and so on, using polynomial features, and then still using linear regression. We compare three degrees, 1, 4, and 15, and calculate R squared on both the training and test data. This is today's most important lesson.
Degree 1 is too simple, a straight line can't follow the curve, underfitting. Degree 4 fits the original pattern well, good fit. Degree 15 wiggles through almost all training points, training score is nearly perfect, but the test score plummets, overfitting, like a student who memorizes practice questions without understanding the concepts.
Done. We have learned simple and multiple linear regression, reading coefficients, evaluating with MAI, RMSI, and R squared, checking residuals, as well as polynomial regression and the danger of overfitting. In the next episode, we will move into classification with logistic regression, KNN, and naive Bayes. Want to master machine learning and data science? Cerdas Inovasi Digital offers Python, machine learning, and AI courses from the basics, Prediction model creation service, data analysis, and AI application for business or research, as well as selling the complete source code of this tutorial. Want to master all series of this, from machine learning introduction to deploying models to the web? All 18 episodes, complete with step-by-step PDF guides, are available for premium tutorial subscribers. Subscribe starting at Rp69.000 per month, save 28% with the 12-month package, or one-time payment of Rp999.000 for lifetime access. Want a single class only? Buy single class with lifetime access. Subscribe now at the premium page cerdasinovasidigital.com Contact us via WhatsApp, or visit cerdasinovasidigital.com Thank you for watching, see you in the next episode.
Type a question or keyword; we find the part of this episode that explains it and jump the video there.
Notes are saved with the current video time and are only visible to you.
Stuck on a step? Ask here; the instructor and other learners can answer.
No questions yet.

Cerdas Inovasi Digital Team
Software developers, BIM & 3D engineers, and AI practitioners
We build software, BIM models, games and AI automations for clients every day, and turn that hands-on work into step-by-step classes: every step is shown on screen and comes with a PDF guide you can keep.
See our clients →