Linear regression helps us find a straight line that best fits data points. It predicts one value from another using a simple formula.
0
0
Linear regression with scikit-learn in ML Python
Introduction
Predicting house prices based on size
Estimating sales from advertising budget
Forecasting temperature based on time of day
Understanding relationship between study hours and exam scores
Syntax
ML Python
from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X_train, y_train) predictions = model.predict(X_test)
X_train should be a 2D array with features (like size, age).
y_train is a 1D array with target values (like price).
Examples
Simple example: predicting y = 2 * x.
ML Python
from sklearn.linear_model import LinearRegression model = LinearRegression() X = [[1], [2], [3], [4]] y = [2, 4, 6, 8] model.fit(X, y) pred = model.predict([[5]])
Example without intercept (line forced through zero).
ML Python
model = LinearRegression(fit_intercept=False) model.fit(X, y) pred = model.predict([[5]])
Sample Program
This program trains a linear regression model to predict exam scores based on hours studied. It prints the slope, intercept, prediction for 6 hours, and training accuracy.
ML Python
from sklearn.linear_model import LinearRegression import numpy as np # Sample data: hours studied vs exam score X = np.array([[1], [2], [3], [4], [5]]) y = np.array([50, 60, 65, 70, 80]) # Create and train model model = LinearRegression() model.fit(X, y) # Predict score for 6 hours studied predicted_score = model.predict([[6]]) # Print coefficients and prediction print(f"Coefficient (slope): {model.coef_[0]:.2f}") print(f"Intercept: {model.intercept_:.2f}") print(f"Predicted score for 6 hours: {predicted_score[0]:.2f}") # Check training accuracy score = model.score(X, y) print(f"Training R^2 score: {score:.2f}")
OutputSuccess
Important Notes
Linear regression assumes a straight-line relationship between input and output.
Input features must be numeric and shaped correctly (2D array).
R^2 score shows how well the model fits the data (1.0 is perfect).
Summary
Linear regression finds a line to predict one value from another.
Use scikit-learn's LinearRegression to train and predict easily.
Check model quality with R^2 score and understand slope/intercept.