Supervised Learning
1. Simple linear regression
Slope = Change in Y/Change in X
=Delta (Y) / Delta(X)
#Find the slope of the graph where the lower point on the line is represented as (−3, −2) and the
higher point on the line is represented as (2, 2).
Types of slopes
1. positive slope
Linear positive slope
Curve linear positive slope
2. negative slope
Linear negative slope
Curve linear negative slope
Error in simple regression
identifying the exact match of values for a and b is not always possible. There will be
some error value (ɛ) associated with it. This error is called marginal or residual error.
Y = (a + bX) + ε
FIG. 8.9 Residual error
Residual is the distance between the predicted point (on the regression line) and the
actual point .
The Sum of the Squares of the Errors (SSE):
SSE is least when b takes the value
The corresponding value of ‘a’ calculated using the above value of ‘b’ is
MExt = 19.04 + 1.89 × MInt
slope = 1.89 implies that the average value of the external examination marks increases by 1.89 for
each additional 1 mark in the internal examination.
Intercept=19.04 implies that indicates that 19.05 is the portion of the external examination marks
not explained by the internal examination marks. (Even if internal marks were zero (hypothetically),
the model predicts ~19 marks in external.)
Note:
Measures the total error between actual values (yi) and predicted values
R-squared (R²):
where,
where:
SST (Total Sum of Squares) = total variation in data
o Value ranges: 1 : perfect fit, 0 : model explains nothing, < 0: worse than mean
prediction
import pandas as pd
import numpy as np
import [Link] as plt
%matplotlib inline
dataset = pd.read_csv('student_scores.csv')
[Link]
[Link]()
[Link](x=’Internal Marks’, y=’External Marks’, style='o')
[Link](‘Internal vs External’)
[Link](’Internal’)
[Link](‘External’)
[Link]()
X = [Link][:, :-1].values
y = [Link][:, 1].values
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
comparison = [Link]({'Actual': y_test, 'Predicted': y_pred})
print(comparison)
import pandas as pd
import numpy as np
import [Link] as plt
%matplotlib inline
# Load dataset
dataset = pd.read_csv('student_scores.csv')
[Link]
[Link]()
# Visualize data
[Link](x=’Internal Marks’, y=’External Marks’, style='o')
[Link](‘Internal vs External’)
[Link](’Internal’)
[Link](‘External’)
[Link]()
# Feature and target split
X = [Link][:, :-1].values
y = [Link][:, 1].values
print(X)
# Train-test split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Train the model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
[Link](X_train, y_train)
# Predict on test set
y_pred = [Link](X_test)
# Compare actual vs predicted
comparison = [Link]({'Actual': y_test, 'Predicted': y_pred})
print(comparison)
# Predict for a custom input
custom_pred = [Link](6.5)
print(f"\nPredicted external marks: {custom_pred[0]:.2f}")
# Model accuracy
from [Link] import r2_score
sse = [Link]((y_test - y_pred) ** 2)
r2 = r2_score(y_test, y_pred)
print("\n── Model Performance ──")
print(f"SSE : {sse:.2f}")
print(f"R² : {r2:.2f}")
# Plot regression line
[Link](X_test, y_test, color='red', label='Actual')
[Link](X_test, y_pred, color='blue', label='Predicted Line')
[Link](‘Internal vs External (Test Set)')
[Link](‘Internal Marks’)
[Link](‘External Marks’)
[Link]()
[Link]()