What is regression?
- Machine Learning course
- Beginner
- Lesson 41/112
- 5 min
Regression is a supervised learning task that predicts a continuous output value.
What is regression?
Regression is supervised learning that predicts a continuous number: a house price, tomorrow's temperature, a product's sales count. The model learns a function from inputs to the output.
# square metres -> price (thousand TL)
X = [[60], [80], [100], [120]]
y = [1200, 1600, 1950, 2400]The difference from classification is the output: classification asks "which group?", regression asks "how much?".
Regression models
In scikit-learn many regression models share the same interface; class names usually end in Regressor:
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
for m in [LinearRegression(), DecisionTreeRegressor(), KNeighborsRegressor(2)]:
m.fit(X, y)
print(m.predict([[90]]))The simplest and most interpretable is linear regression; we'll look at it in detail in this group.
Practice First regression
Train a KNeighborsRegressor(n_neighbors=2) model and print the prediction for 90 m².
Starter code
from sklearn.neighbors import KNeighborsRegressor
X = [[60], [80], [100], [120]]
y = [1200, 1600, 1950, 2400]
# buraya yazSample solution
from sklearn.neighbors import KNeighborsRegressor
X = [[60], [80], [100], [120]]
y = [1200, 1600, 1950, 2400]
m = KNeighborsRegressor(n_neighbors=2)
m.fit(X, y)
print(m.predict([[90]]))Expected output
[1775.]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is a regression problem?
- Predicting a car's fuel consumption Correct answer
- Whether an e-mail is spam
- The species of an animal in a photo
- Grouping customers
Explanation: Fuel consumption is a continuous number; the others are class or cluster questions.
2 Create the decision tree regression model.
m = DecisionTree____()
m = DecisionTreeRegressor()
Explanation: Regression versions end in Regressor, classification versions in Classifier.
3 Match the outputs with the problem type.
| Term | Meaning |
|---|---|
Price: 1950 |
Regression |
Class: spam |
Classification |
Cluster: 2 |
Clustering |
Temperature: 21.4 |
Regression (continuous) |