Example 1: Linear Regression
PREDITING PRICE BASES ON AREA
Example 2: Linear Regression (Study Hours vs Marks)
👉 Problem Statement
Predict student marks based on study hours.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Data
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) # Study hours
Y = np.array([35, 40, 50, 60, 70]) # Marks
# Model
model = LinearRegression()
model.fit(X, Y)
# Slope and intercept
m = model.coef_[0]
b = model.intercept_
# Prediction
hours = 6
predicted_marks = model.predict([[hours]])
# Plot
X_line = np.linspace(1, 6, 100).reshape(-1, 1)
Y_line = model.predict(X_line)
plt.scatter(X, Y)
plt.plot(X_line, Y_line)
plt.scatter(hours, predicted_marks)
plt.xlabel("Study Hours")
plt.ylabel("Marks")
plt.title("Linear Regression: Study Hours vs Marks")
plt.show()
# Output
print("Slope (m):", m)
print("Intercept (b):", b)
print("Equation: Y =", m, "* X +", b)
print("Predicted Marks for", hours, "hours:", predicted_marks[0])
No comments:
Post a Comment