How to Use Linear Regression with sklearn in Python
To use
LinearRegression from sklearn.linear_model in Python, first import it, create a model instance, then fit it with training data using fit(X, y). After training, use predict(X_new) to get predictions.Syntax
The basic steps to use linear regression in sklearn are:
- Import: Bring in
LinearRegressionfromsklearn.linear_model. - Create model: Make an instance with
model = LinearRegression(). - Fit model: Train with data using
model.fit(X, y), whereXis input features andyis target values. - Predict: Use
model.predict(X_new)to get output for new inputs.
python
from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X, y) predictions = model.predict(X_new)
Example
This example shows how to train a linear regression model on simple data and predict new values.
python
from sklearn.linear_model import LinearRegression import numpy as np # Sample data: X is feature, y is target X = np.array([[1], [2], [3], [4], [5]]) y = np.array([3, 5, 7, 9, 11]) # Create and train model model = LinearRegression() model.fit(X, y) # Predict new values X_new = np.array([[6], [7]]) predictions = model.predict(X_new) print("Predictions:", predictions) print("Model coefficient:", model.coef_) print("Model intercept:", model.intercept_)
Output
Predictions: [13. 15.]
Model coefficient: [2.]
Model intercept: 1.0
Common Pitfalls
Common mistakes when using sklearn linear regression include:
- Not reshaping input
Xto 2D array (e.g., shape (n_samples, n_features)). - Mixing up
Xandyinfit. - Using non-numeric data without preprocessing.
- Forgetting to import necessary modules.
Always check your data shape and types before fitting.
python
import numpy as np from sklearn.linear_model import LinearRegression # Wrong: X is 1D array X_wrong = np.array([1, 2, 3, 4, 5]) y = np.array([3, 5, 7, 9, 11]) model = LinearRegression() # This will raise an error # model.fit(X_wrong, y) # Correct: reshape X to 2D X_correct = X_wrong.reshape(-1, 1) model.fit(X_correct, y) print("Model trained successfully with correct input shape.")
Output
Model trained successfully with correct input shape.
Quick Reference
Summary tips for using sklearn LinearRegression:
- Input
Xmust be 2D array: shape (samples, features). - Target
ycan be 1D array. - Use
fit()to train,predict()to get predictions. - Access
coef_for slope(s) andintercept_for bias. - Works best with numeric, clean data.
Key Takeaways
Always reshape input features X to 2D array before fitting the model.
Use model.fit(X, y) to train and model.predict(X_new) to get predictions.
Check model.coef_ and model.intercept_ to understand the learned line.
Ensure your data is numeric and clean for best results.
Import LinearRegression from sklearn.linear_model before use.