0
0
Data Analysis Pythondata~5 mins

Linear regression basics in Data Analysis Python

Choose your learning style9 modes available
Introduction

Linear regression helps us find a straight line that best fits data points. It shows how one thing changes when another changes.

Predicting house prices based on size
Estimating sales based on advertising budget
Understanding how temperature affects ice cream sales
Finding the relationship between study hours and test scores
Syntax
Data Analysis Python
from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(X, y)
predictions = model.predict(X_new)

X is the input data (features), usually a 2D array.

y is the target data (labels), usually a 1D array.

Examples
This example fits a line to points where y is twice x, then predicts y for x=5.
Data Analysis Python
from sklearn.linear_model import LinearRegression

X = [[1], [2], [3], [4]]
y = [2, 4, 6, 8]
model = LinearRegression()
model.fit(X, y)
pred = model.predict([[5]])
This shows how to get the slope (coefficient) and intercept of the fitted line.
Data Analysis Python
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[10], [20], [30]])
y = np.array([15, 25, 35])
model = LinearRegression()
model.fit(X, y)
print(model.coef_, model.intercept_)
Sample Program

This program fits a line to study hours and test scores, then predicts the score for 6 hours of study.

Data Analysis Python
import numpy as np
from sklearn.linear_model import LinearRegression

# Data: hours studied vs test score
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([50, 55, 65, 70, 75])

# Create and train the model
model = LinearRegression()
model.fit(X, y)

# Predict score for 6 hours studied
predicted_score = model.predict([[6]])

# Print slope, intercept, and prediction
print(f"Slope (coefficient): {model.coef_[0]:.2f}")
print(f"Intercept: {model.intercept_:.2f}")
print(f"Predicted score for 6 hours: {predicted_score[0]:.2f}")
OutputSuccess
Important Notes

Linear regression assumes a straight-line relationship between variables.

Input features X must be 2D (even if one feature).

Model learns slope and intercept to make predictions.

Summary

Linear regression finds a straight line to predict one value from another.

Use it when you want to see or predict how one thing changes with another.

Fit the model with data, then use it to predict new values.