Train Your First Model
Intermediate · Write the code · from Build Your First ML Model: Linear Regression with scikit-learn
From the lesson: Build Your First ML Model: Linear Regression with scikit-learn
Imagine you're a real estate agent. A client asks, "How much is my house worth?" You look at the square footage, the number of bedrooms, and the neighborhood - then you give an estimate. You just did regression in your head.
Read the lessonCreate a LinearRegression model, fit it on the given X_train and y_train, then print the R-squared score on X_test, y_test rounded to 4 decimal places. Print only the number.
The data is already created for you.
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
X, y = make_regression(n_samples=100, n_features=1,
noise=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
# Create a LinearRegression model, fit it, and print the R2 score
More Linear Regression exercises
- Read the CoefficientsIntermediate
- Fix the Scaling BugIntermediate
- Multi-Feature RegressionIntermediate
- Polynomial vs LinearIntermediate
- Complete Regression PipelineIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz