Regression helps us find numbers that can be any value, not just fixed choices. It predicts things like prices or temperatures that change smoothly.
Why regression predicts continuous values in ML Python
model = RegressionModel() model.fit(training_data, target_values) predictions = model.predict(new_data)
The model learns from data with continuous target values.
Predictions are numbers that can take any value, not just categories.
from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X_train, y_train) predictions = model.predict(X_test)
from sklearn.tree import DecisionTreeRegressor model = DecisionTreeRegressor() model.fit(X_train, y_train) predictions = model.predict(X_test)
This program trains a simple linear regression model to predict exam scores based on hours studied. It then predicts the score for 6 hours.
from sklearn.linear_model import LinearRegression import numpy as np # Training data: hours studied vs exam score X_train = np.array([[1], [2], [3], [4], [5]]) y_train = np.array([50, 55, 65, 70, 75]) # Create and train the model model = LinearRegression() model.fit(X_train, y_train) # Predict score for 6 hours studied X_test = np.array([[6]]) prediction = model.predict(X_test) print(f"Predicted score for 6 hours studied: {prediction[0]:.2f}")
Regression models output continuous numbers, unlike classification which outputs categories.
Choosing the right regression model depends on the data shape and problem.
Regression predicts numbers that can be any value, not just fixed groups.
It is useful when the result is a quantity like price, temperature, or score.
Models learn from examples with continuous target values to make predictions.
Practice
Solution
Step 1: Understand regression's purpose
Regression is designed to predict numbers that can vary continuously, like prices or temperatures.Step 2: Compare with classification
Classification predicts fixed groups or categories, not continuous values.Final Answer:
Because it estimates quantities that can take any value within a range -> Option DQuick Check:
Regression predicts continuous values = Because it estimates quantities [OK]
- Confusing regression with classification
- Thinking regression predicts categories
- Assuming regression only works with discrete values
Solution
Step 1: Identify regression target type
Regression targets are continuous numbers, like 3.5 or 7.2.Step 2: Check options for continuous values
Only target = [3.5, 7.2, 1.8] contains numbers suitable for regression.Final Answer:
target = [3.5, 7.2, 1.8] -> Option AQuick Check:
Regression targets are numbers = target with floats [OK]
- Using categorical strings as regression targets
- Confusing classification labels with regression targets
- Using boolean or text values for regression
Solution
Step 1: Identify target variable type
House prices are numbers that can take many values, not fixed groups.Step 2: Match problem to regression
Regression predicts continuous numeric values, perfect for price prediction.Final Answer:
Because house prices are continuous values that can vary widely -> Option BQuick Check:
Price prediction = continuous value regression [OK]
- Treating prices as categories
- Choosing classification instead of regression
- Confusing regression with yes/no prediction
from sklearn.linear_model import LinearRegression X = [[1], [2], [3], [4]] y = [2.0, 4.1, 6.0, 8.1] model = LinearRegression() model.fit(X, y) pred = model.predict([[5]]) print(round(pred[0], 1))
What will be the printed output?
Solution
Step 1: Understand the data and model
The data roughly follows y = 2 * x with small noise. The model fits a line close to y=2x.Step 2: Predict for x=5
Prediction is about 2 * 5 = 10, but slight noise shifts it to around 10.1.Final Answer:
10.1 -> Option AQuick Check:
Linear fit predicts ~10.1 for input 5 [OK]
- Assuming exact multiples without noise
- Confusing classification output with regression output
- Rounding incorrectly
from sklearn.linear_model import LinearRegression X = [1, 2, 3, 4] y = [2.0, 4.1, 6.0, 8.1] model = LinearRegression() model.fit(X, y)
What is the error and how to fix it?
Solution
Step 1: Identify input shape error
scikit-learn expects X as 2D array (list of lists), but X is 1D list.Step 2: Fix input shape
Wrap each value in a list: X = [[1], [2], [3], [4]] to make it 2D.Final Answer:
X should be a 2D array, fix with X = [[1], [2], [3], [4]] -> Option CQuick Check:
Input X must be 2D array for regression [OK]
- Passing 1D list instead of 2D array for X
- Changing y to strings incorrectly
- Removing required fit arguments
Ready to prove it?
Pass the test and this pattern moves unknown
