Assignment 4
Supervised Machine Learning Models for Regression
(Linear Regression, Polynomial Regression, Logistic Regression)
Lab Assignment
SET A
1. Apply Linear Regression on a YouTube Video dataset to predict video views using relevant video attributes. Visualize the relationship between the predictor and target variable using a scatter plot and regression line.
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# Create YouTube video dataset
data = {
"Video": [
"Video 1", "Video 2", "Video 3", "Video 4", "Video 5",
"Video 6", "Video 7", "Video 8", "Video 9", "Video 10",
"Video 11", "Video 12", "Video 13", "Video 14", "Video 15"
],
"Likes": [
1200, 2500, 3200, 4500, 5200,
6800, 7500, 8200, 9500, 11000,
12500, 14000, 15500, 17000, 19000
],
"Views": [
15000, 28000, 35000, 50000, 62000,
75000, 82000, 95000, 110000, 125000,
145000, 160000, 180000, 195000, 220000
]
}
df = pd.DataFrame(data)
print("YouTube Dataset:")
print(df)
# Save dataset
df.to_csv("youtube_videos.csv", index=False)
# Independent variable (Predictor)
X = df[["Likes"]]
# Dependent variable (Target)
y = df["Views"]
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create Linear Regression model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Predict views
y_pred = model.predict(X_test)
# Display model parameters
print("\nLinear Regression Equation:")
print("Views =", model.intercept_, "+", model.coef_[0], "* Likes")
# Display predictions
result = pd.DataFrame({
"Actual Views": y_test,
"Predicted Views": y_pred
})
print("\nActual vs Predicted Views:")
print(result)
# Model evaluation
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("\nMean Squared Error:", mse)
print("R2 Score:", r2)
# Predict views for a new video
new_likes = [[10000]]
predicted_views = model.predict(new_likes)
print("\nPredicted views for 10,000 likes:",
round(predicted_views[0], 2))
# Scatter plot with regression line
plt.figure(figsize=(9, 6))
plt.scatter(
df["Likes"],
df["Views"],
label="Actual Data"
)
plt.plot(
df["Likes"],
model.predict(df[["Likes"]]),
linewidth=2,
label="Regression Line"
)
plt.xlabel("Number of Likes")
plt.ylabel("Number of Views")
plt.title("YouTube Likes vs Video Views - Linear Regression")
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
2. Apply Logistic Regression on an Online Gaming dataset to classify players as Casual or Professional gamers based on features such as daily play time, achievements unlocked, in-game purchases, and gaming level. Evaluate the model using Accuracy, Precision, Recall, F1-Score, and Confusion Matrix.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report
)
# --------------------------------------------------
# Step 1: Create Online Gaming Dataset
# --------------------------------------------------
data = {
"Player": [
"P1", "P2", "P3", "P4", "P5",
"P6", "P7", "P8", "P9", "P10",
"P11", "P12", "P13", "P14", "P15",
"P16", "P17", "P18", "P19", "P20"
],
"Daily_Play_Time": [
1.0, 1.5, 2.0, 2.5, 3.0,
3.5, 4.0, 4.5, 5.0, 5.5,
6.0, 6.5, 7.0, 7.5, 8.0,
8.5, 9.0, 9.5, 10.0, 10.5
],
"Achievements_Unlocked": [
5, 8, 12, 15, 20,
25, 30, 35, 40, 45,
50, 55, 60, 65, 70,
75, 80, 85, 90, 95
],
"In_Game_Purchases": [
200, 300, 500, 600, 700,
900, 1000, 1200, 1500, 1700,
2000, 2200, 2500, 2800, 3000,
3200, 3500, 3800, 4000, 4500
],
"Gaming_Level": [
3, 5, 7, 9, 12,
15, 18, 21, 24, 27,
30, 33, 36, 39, 42,
45, 48, 51, 54, 57
],
# 0 = Casual, 1 = Professional
"Player_Type": [
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1
]
}
df = pd.DataFrame(data)
print("Online Gaming Dataset:")
print(df)
# Save dataset
df.to_csv("online_gaming.csv", index=False)
# --------------------------------------------------
# Step 2: Select Features and Target
# --------------------------------------------------
features = [
"Daily_Play_Time",
"Achievements_Unlocked",
"In_Game_Purchases",
"Gaming_Level"
]
X = df[features]
y = df["Player_Type"]
# --------------------------------------------------
# Step 3: Split Dataset
# --------------------------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.30,
random_state=42,
stratify=y
)
# --------------------------------------------------
# Step 4: Create and Train Logistic Regression Model
# --------------------------------------------------
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
# --------------------------------------------------
# Step 5: Make Predictions
# --------------------------------------------------
y_pred = model.predict(X_test)
print("\nActual Values:")
print(y_test.values)
print("\nPredicted Values:")
print(y_pred)
# --------------------------------------------------
# Step 6: Evaluate Model
# --------------------------------------------------
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(
y_test,
y_pred,
zero_division=0
)
recall = recall_score(
y_test,
y_pred,
zero_division=0
)
f1 = f1_score(
y_test,
y_pred,
zero_division=0
)
print("\n----- Model Evaluation -----")
print("Accuracy :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall :", round(recall, 4))
print("F1-Score :", round(f1, 4))
# --------------------------------------------------
# Step 7: Classification Report
# --------------------------------------------------
print("\nClassification Report:")
print(
classification_report(
y_test,
y_pred,
target_names=["Casual", "Professional"],
zero_division=0
)
)
# --------------------------------------------------
# Step 8: Confusion Matrix
# --------------------------------------------------
cm = confusion_matrix(y_test, y_pred)
print("\nConfusion Matrix:")
print(cm)
plt.figure(figsize=(6, 5))
sns.heatmap(
cm,
annot=True,
fmt="d",
xticklabels=["Casual", "Professional"],
yticklabels=["Casual", "Professional"]
)
plt.xlabel("Predicted Class")
plt.ylabel("Actual Class")
plt.title("Confusion Matrix - Online Gaming Classification")
plt.tight_layout()
plt.show()
# --------------------------------------------------
# Step 9: Predict a New Player
# --------------------------------------------------
new_player = [[6.5, 55, 2300, 34]]
prediction = model.predict(new_player)
if prediction[0] == 0:
print("\nNew Player Classification: Casual Gamer")
else:
print("\nNew Player Classification: Professional Gamer")
3. Download the Salary Dataset. Write a Python program to read the dataset and display its information. Preprocess the data if required and split it into training and testing sets. Apply Simple Linear Regression to predict salary based on years of experience. Plot the regression line and evaluate the model using appropriate metrics such as Mean Squared Error.
Apply Polynomial Regression to predict song popularity using audio features such as danceability, energy, and tempo. Evaluate the model using MAE, RMSE, and R² Score, and visualize the fitted curve.
pip install pandas matplotlib scikit-learn
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# --------------------------------------------------
# Step 1: Read the Salary Dataset
# --------------------------------------------------
df = pd.read_csv("Salary.csv")
print("First 5 records:")
print(df.head())
# --------------------------------------------------
# Step 2: Display Dataset Information
# --------------------------------------------------
print("\nDataset Information:")
print(df.info())
print("\nDataset Description:")
print(df.describe())
print("\nMissing Values:")
print(df.isnull().sum())
# --------------------------------------------------
# Step 3: Preprocess the Data
# --------------------------------------------------
# Remove missing values
df = df.dropna()
# Convert columns to numeric if required
df["YearsExperience"] = pd.to_numeric(
df["YearsExperience"], errors="coerce"
)
df["Salary"] = pd.to_numeric(
df["Salary"], errors="coerce"
)
# Remove rows containing invalid values
df = df.dropna()
print("\nCleaned Dataset:")
print(df.head())
# --------------------------------------------------
# Step 4: Select Independent and Dependent Variables
# --------------------------------------------------
X = df[["YearsExperience"]]
y = df["Salary"]
# --------------------------------------------------
# Step 5: Split Dataset into Training and Testing Sets
# --------------------------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))
# --------------------------------------------------
# Step 6: Create Linear Regression Model
# --------------------------------------------------
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# --------------------------------------------------
# Step 7: Predict Salary
# --------------------------------------------------
y_pred = model.predict(X_test)
print("\nActual and Predicted Salaries:")
result = pd.DataFrame({
"YearsExperience": X_test["YearsExperience"].values,
"Actual Salary": y_test.values,
"Predicted Salary": y_pred
})
print(result)
# --------------------------------------------------
# Step 8: Display Regression Equation
# --------------------------------------------------
print("\nRegression Equation:")
print(
"Salary =",
round(model.intercept_, 2),
"+",
round(model.coef_[0], 2),
"* YearsExperience"
)
# --------------------------------------------------
# Step 9: Evaluate the Model
# --------------------------------------------------
mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("\n----- Model Evaluation -----")
print("Mean Squared Error (MSE):", round(mse, 2))
print("Mean Absolute Error (MAE):", round(mae, 2))
print("R2 Score:", round(r2, 4))
# --------------------------------------------------
# Step 10: Plot Regression Line
# --------------------------------------------------
plt.figure(figsize=(9, 6))
# Actual data
plt.scatter(
X,
y,
label="Actual Salary"
)
# Regression line
plt.plot(
X,
model.predict(X),
linewidth=2,
label="Regression Line"
)
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.title("Salary Prediction using Simple Linear Regression")
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
# --------------------------------------------------
# Step 11: Predict Salary for New Experience
# --------------------------------------------------
years = [[7]]
predicted_salary = model.predict(years)
print(
"\nPredicted salary for 7 years of experience:",
round(predicted_salary[0], 2)
)
Part B: Polynomial Regression for Song Popularity
Part B: Polynomial Regression for Song Popularity
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
# --------------------------------------------------
# Step 1: Create / Read Song Dataset
# --------------------------------------------------
data = {
"Song": [
"Song 1", "Song 2", "Song 3", "Song 4", "Song 5",
"Song 6", "Song 7", "Song 8", "Song 9", "Song 10",
"Song 11", "Song 12", "Song 13", "Song 14", "Song 15",
"Song 16", "Song 17", "Song 18", "Song 19", "Song 20"
],
"Danceability": [
0.40, 0.45, 0.50, 0.55, 0.60,
0.62, 0.65, 0.68, 0.70, 0.72,
0.74, 0.76, 0.78, 0.80, 0.82,
0.84, 0.86, 0.88, 0.90, 0.92
],
"Energy": [
0.30, 0.35, 0.40, 0.42, 0.45,
0.50, 0.52, 0.55, 0.58, 0.60,
0.62, 0.65, 0.68, 0.70, 0.72,
0.75, 0.78, 0.80, 0.83, 0.85
],
"Tempo": [
80, 85, 90, 95, 100,
105, 110, 115, 120, 125,
128, 130, 135, 140, 145,
150, 155, 160, 165, 170
],
"Popularity": [
35, 38, 42, 45, 48,
52, 55, 58, 61, 64,
66, 69, 72, 75, 78,
80, 83, 86, 89, 92
]
}
df = pd.DataFrame(data)
print("Song Dataset:")
print(df)
# Save dataset
df.to_csv("song_popularity.csv", index=False)
# --------------------------------------------------
# Step 2: Check Dataset Information
# --------------------------------------------------
print("\nDataset Information:")
df.info()
print("\nMissing Values:")
print(df.isnull().sum())
# --------------------------------------------------
# Step 3: Preprocess Dataset
# --------------------------------------------------
features = [
"Danceability",
"Energy",
"Tempo"
]
target = "Popularity"
# Convert features to numeric
for column in features + [target]:
df[column] = pd.to_numeric(
df[column],
errors="coerce"
)
# Remove missing values
df = df.dropna()
# --------------------------------------------------
# Step 4: Select Features and Target
# --------------------------------------------------
X = df[features]
y = df[target]
# --------------------------------------------------
# Step 5: Split Dataset
# --------------------------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
# --------------------------------------------------
# Step 6: Create Polynomial Regression Model
# --------------------------------------------------
degree = 2
model = make_pipeline(
PolynomialFeatures(degree=degree),
LinearRegression()
)
# Train model
model.fit(X_train, y_train)
# --------------------------------------------------
# Step 7: Predict Popularity
# --------------------------------------------------
y_pred = model.predict(X_test)
result = pd.DataFrame({
"Actual Popularity": y_test.values,
"Predicted Popularity": y_pred
})
print("\nActual vs Predicted Popularity:")
print(result)
# --------------------------------------------------
# Step 8: Evaluate Model
# --------------------------------------------------
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print("\n----- Polynomial Regression Evaluation -----")
print("Mean Absolute Error (MAE):", round(mae, 2))
print("Root Mean Squared Error (RMSE):", round(rmse, 2))
print("R2 Score:", round(r2, 4))
# --------------------------------------------------
# Step 9: Visualize Fitted Polynomial Curve
# --------------------------------------------------
# For visualization, use Danceability as the
# main predictor while keeping the other features
# fixed at their mean values.
danceability_range = np.linspace(
df["Danceability"].min(),
df["Danceability"].max(),
100
)
visual_data = pd.DataFrame({
"Danceability": danceability_range,
"Energy": df["Energy"].mean(),
"Tempo": df["Tempo"].mean()
})
predicted_curve = model.predict(visual_data)
plt.figure(figsize=(9, 6))
# Actual observations
plt.scatter(
df["Danceability"],
df["Popularity"],
label="Actual Songs"
)
# Polynomial fitted curve
plt.plot(
danceability_range,
predicted_curve,
linewidth=2,
label="Polynomial Regression Curve"
)
plt.xlabel("Danceability")
plt.ylabel("Song Popularity")
plt.title(
"Polynomial Regression: Danceability vs Song Popularity"
)
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
SET B
1. Apply Logistic Regression to classify smartphones into different price categories using specifications such as RAM, storage, battery capacity, and camera resolution. Assess the model using Accuracy, F1-Score, and Confusion Matrix.
pip install pandas matplotlib seaborn scikit-learn
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, classification_report
# ---------------------------------------------------------
# Step 1: Create Smartphone Dataset
# ---------------------------------------------------------
data = {
"Smartphone": [
"Phone1", "Phone2", "Phone3", "Phone4", "Phone5",
"Phone6", "Phone7", "Phone8", "Phone9", "Phone10",
"Phone11", "Phone12", "Phone13", "Phone14", "Phone15",
"Phone16", "Phone17", "Phone18", "Phone19", "Phone20",
"Phone21", "Phone22", "Phone23", "Phone24", "Phone25",
"Phone26", "Phone27", "Phone28", "Phone29", "Phone30"
],
"RAM_GB": [
2, 3, 3, 4, 4, 4, 6, 6, 6, 8,
8, 8, 8, 12, 12, 12, 12, 16, 16, 16,
2, 3, 4, 6, 8, 12, 16, 4, 8, 12
],
"Storage_GB": [
32, 32, 64, 64, 128, 128, 128, 128, 256, 256,
256, 128, 256, 256, 256, 512, 512, 512, 512, 512,
32, 64, 64, 128, 256, 256, 512, 128, 256, 512
],
"Battery_mAh": [
3000, 3500, 4000, 4000, 4500, 4500, 5000, 5000, 4800, 5000,
5000, 4500, 5000, 5000, 5200, 5000, 5500, 5000, 5200, 5500,
3200, 3800, 4200, 4500, 5000, 5200, 5500, 4500, 5000, 5200
],
"Camera_MP": [
8, 12, 13, 16, 16, 20, 24, 32, 32, 48,
48, 50, 50, 64, 64, 64, 108, 108, 200, 200,
12, 16, 20, 32, 48, 64, 108, 32, 50, 108
],
# 0 = Budget, 1 = Mid-Range, 2 = Premium
"Price_Category": [
0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
0, 0, 1, 1, 1, 2, 2, 1, 1, 2
]
}
df = pd.DataFrame(data)
print("----- Smartphone Dataset -----")
print(df)
# Save dataset
df.to_csv("smartphone_price.csv", index=False)
# ---------------------------------------------------------
# Step 2: Display Dataset Information
# ---------------------------------------------------------
print("\n----- Dataset Information -----")
df.info()
print("\n----- Missing Values -----")
print(df.isnull().sum())
# ---------------------------------------------------------
# Step 3: Prepare Features and Target
# ---------------------------------------------------------
features = [
"RAM_GB",
"Storage_GB",
"Battery_mAh",
"Camera_MP"
]
X = df[features]
y = df["Price_Category"]
# ---------------------------------------------------------
# Step 4: Split Data into Training and Testing Sets
# ---------------------------------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.30,
random_state=42,
stratify=y
)
print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))
# ---------------------------------------------------------
# Step 5: Apply Logistic Regression
# ---------------------------------------------------------
model = LogisticRegression(
max_iter=1000,
multi_class="auto"
)
model.fit(X_train, y_train)
# Predict test data
y_pred = model.predict(X_test)
print("\nActual Price Categories:")
print(y_test.values)
print("\nPredicted Price Categories:")
print(y_pred)
# ---------------------------------------------------------
# Step 6: Evaluate the Model
# ---------------------------------------------------------
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(
y_test,
y_pred,
average="weighted"
)
print("\n----- Model Evaluation -----")
print("Accuracy :", round(accuracy, 4))
print("F1-Score :", round(f1, 4))
print("\n----- Classification Report -----")
print(
classification_report(
y_test,
y_pred,
target_names=["Budget", "Mid-Range", "Premium"],
zero_division=0
)
)
# ---------------------------------------------------------
# Step 7: Confusion Matrix
# ---------------------------------------------------------
cm = confusion_matrix(y_test, y_pred)
print("\n----- Confusion Matrix -----")
print(cm)
plt.figure(figsize=(7, 5))
sns.heatmap(
cm,
annot=True,
fmt="d",
xticklabels=["Budget", "Mid-Range", "Premium"],
yticklabels=["Budget", "Mid-Range", "Premium"]
)
plt.xlabel("Predicted Category")
plt.ylabel("Actual Category")
plt.title("Confusion Matrix - Smartphone Price Classification")
plt.tight_layout()
plt.show()
# ---------------------------------------------------------
# Step 8: Predict Price Category for a New Smartphone
# ---------------------------------------------------------
new_phone = [[
8, # RAM in GB
256, # Storage in GB
5000, # Battery in mAh
64 # Camera in MP
]]
prediction = model.predict(new_phone)
categories = {
0: "Budget",
1: "Mid-Range",
2: "Premium"
}
print("\n----- New Smartphone Prediction -----")
print("RAM :", new_phone[0][0], "GB")
print("Storage :", new_phone[0][1], "GB")
print("Battery :", new_phone[0][2], "mAh")
print("Camera :", new_phone[0][3], "MP")
print("Predicted Price Category:", categories[prediction[0]])
2. Use a Gaming Dataset containing gameplay hours, achievements unlocked, in-game purchases, and friend count. Develop a Multiple Linear Regression model to predict player engagement score. Evaluate the model using MAE, MSE, RMSE, and R² Score.
pip install pandas numpy matplotlib scikit-learn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score
)
# ---------------------------------------------------------
# Step 1: Create Gaming Dataset
# ---------------------------------------------------------
data = {
"Player": [
"P1", "P2", "P3", "P4", "P5",
"P6", "P7", "P8", "P9", "P10",
"P11", "P12", "P13", "P14", "P15",
"P16", "P17", "P18", "P19", "P20",
"P21", "P22", "P23", "P24", "P25"
],
"Gameplay_Hours": [
5, 8, 10, 12, 15,
18, 20, 22, 25, 28,
30, 32, 35, 38, 40,
42, 45, 48, 50, 52,
55, 58, 60, 65, 70
],
"Achievements_Unlocked": [
5, 8, 12, 15, 20,
25, 28, 32, 36, 40,
45, 48, 52, 55, 60,
65, 68, 72, 76, 80,
84, 88, 92, 96, 100
],
"In_Game_Purchases": [
100, 200, 300, 250, 500,
600, 700, 800, 900, 1000,
1200, 1300, 1500, 1600, 1800,
2000, 2200, 2400, 2600, 2800,
3000, 3200, 3500, 3800, 4000
],
"Friend_Count": [
5, 8, 10, 12, 15,
18, 20, 22, 25, 28,
30, 32, 35, 38, 40,
42, 45, 48, 50, 55,
58, 60, 65, 70, 75
],
"Engagement_Score": [
18, 24, 29, 33, 38,
44, 47, 51, 56, 60,
64, 67, 72, 76, 79,
82, 85, 88, 91, 94,
96, 97, 98, 99, 100
]
}
df = pd.DataFrame(data)
print("----- Gaming Dataset -----")
print(df)
# Save dataset
df.to_csv("gaming_dataset.csv", index=False)
# ---------------------------------------------------------
# Step 2: Display Dataset Information
# ---------------------------------------------------------
print("\n----- Dataset Information -----")
df.info()
print("\n----- Statistical Description -----")
print(df.describe())
print("\n----- Missing Values -----")
print(df.isnull().sum())
# ---------------------------------------------------------
# Step 3: Define Features and Target
# ---------------------------------------------------------
features = [
"Gameplay_Hours",
"Achievements_Unlocked",
"In_Game_Purchases",
"Friend_Count"
]
X = df[features]
y = df["Engagement_Score"]
# ---------------------------------------------------------
# Step 4: Split Dataset
# ---------------------------------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.20,
random_state=42
)
print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))
# ---------------------------------------------------------
# Step 5: Apply Multiple Linear Regression
# ---------------------------------------------------------
model = LinearRegression()
model.fit(X_train, y_train)
# Predict test data
y_pred = model.predict(X_test)
# ---------------------------------------------------------
# Step 6: Display Actual and Predicted Values
# ---------------------------------------------------------
result = pd.DataFrame({
"Actual Engagement Score": y_test.values,
"Predicted Engagement Score": y_pred
})
print("\n----- Actual vs Predicted Values -----")
print(result)
# ---------------------------------------------------------
# Step 7: Regression Coefficients
# ---------------------------------------------------------
print("\n----- Regression Coefficients -----")
for feature, coefficient in zip(features, model.coef_):
print(feature, ":", round(coefficient, 4))
print("\nIntercept:", round(model.intercept_, 4))
# ---------------------------------------------------------
# Step 8: Model Evaluation
# ---------------------------------------------------------
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print("\n----- Model Evaluation -----")
print("Mean Absolute Error (MAE) :", round(mae, 4))
print("Mean Squared Error (MSE) :", round(mse, 4))
print("Root Mean Squared Error (RMSE) :", round(rmse, 4))
print("R2 Score :", round(r2, 4))
# ---------------------------------------------------------
# Step 9: Actual vs Predicted Plot
# ---------------------------------------------------------
plt.figure(figsize=(8, 6))
plt.scatter(
y_test,
y_pred,
s=70
)
plt.xlabel("Actual Engagement Score")
plt.ylabel("Predicted Engagement Score")
plt.title(
"Actual vs Predicted Player Engagement Score"
)
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
# ---------------------------------------------------------
# Step 10: Predict Engagement for a New Player
# ---------------------------------------------------------
new_player = [[
35, # Gameplay Hours
55, # Achievements
1500, # In-Game Purchases
40 # Friend Count
]]
predicted_score = model.predict(new_player)
print("\n----- New Player Prediction -----")
print("Gameplay Hours :", new_player[0][0])
print("Achievements :", new_player[0][1])
print("In-Game Purchases :", new_player[0][2])
print("Friend Count :", new_player[0][3])
print(
"Predicted Engagement Score:",
round(predicted_score[0], 2)
)
3. Use a Sports Performance dataset containing attributes such as practice hours, fitness score, match experience, and training sessions. Apply Simple Linear Regression and Multiple Linear Regression to predict player performance. Compare the models using MAE, MSE, RMSE, and R² Score, and identify the most suitable regression model.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score
)
# ---------------------------------------------------------
# Step 1: Create Sports Performance Dataset
# ---------------------------------------------------------
data = {
"Player": [
"P1", "P2", "P3", "P4", "P5",
"P6", "P7", "P8", "P9", "P10",
"P11", "P12", "P13", "P14", "P15",
"P16", "P17", "P18", "P19", "P20",
"P21", "P22", "P23", "P24", "P25",
"P26", "P27", "P28", "P29", "P30"
],
"Practice_Hours": [
2, 3, 4, 5, 6,
7, 8, 9, 10, 11,
12, 13, 14, 15, 16,
17, 18, 19, 20, 21,
22, 23, 24, 25, 26,
27, 28, 29, 30, 32
],
"Fitness_Score": [
45, 48, 50, 52, 55,
57, 60, 62, 64, 66,
68, 70, 72, 74, 76,
78, 80, 82, 84, 85,
87, 88, 89, 91, 92,
93, 94, 95, 96, 98
],
"Match_Experience": [
2, 3, 4, 5, 6,
8, 9, 10, 12, 13,
15, 16, 18, 20, 22,
24, 25, 27, 28, 30,
32, 35, 36, 38, 40,
42, 45, 48, 50
],
"Training_Sessions": [
10, 12, 15, 18, 20,
22, 25, 28, 30, 32,
35, 38, 40, 42, 45,
48, 50, 52, 55, 58,
60, 62, 65, 68, 70,
72, 75, 78, 80
],
"Performance_Score": [
42, 45, 48, 51, 54,
57, 60, 62, 65, 67,
70, 72, 74, 76, 78,
80, 82, 84, 86, 87,
89, 90, 91, 93, 94,
95, 96, 97, 98, 99
]
}
df = pd.DataFrame(data)
print("----- Sports Performance Dataset -----")
print(df)
# Save dataset
df.to_csv("sports_performance.csv", index=False)
# ---------------------------------------------------------
# Step 2: Display Dataset Information
# ---------------------------------------------------------
print("\n----- Dataset Information -----")
df.info()
print("\n----- Statistical Description -----")
print(df.describe())
print("\n----- Missing Values -----")
print(df.isnull().sum())
# ---------------------------------------------------------
# Step 3: Define Variables
# ---------------------------------------------------------
features = [
"Practice_Hours",
"Fitness_Score",
"Match_Experience",
"Training_Sessions"
]
target = "Performance_Score"
# ---------------------------------------------------------
# Step 4: Split Dataset
# ---------------------------------------------------------
X = df[features]
y = df[target]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.20,
random_state=42
)
print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))
# =========================================================
# MODEL 1: SIMPLE LINEAR REGRESSION
# =========================================================
# Use Practice Hours as the single predictor
X_simple = df[["Practice_Hours"]]
X_train_s, X_test_s, y_train_s, y_test_s = train_test_split(
X_simple,
y,
test_size=0.20,
random_state=42
)
simple_model = LinearRegression()
simple_model.fit(X_train_s, y_train_s)
simple_pred = simple_model.predict(X_test_s)
# ---------------------------------------------------------
# Simple Regression Evaluation
# ---------------------------------------------------------
simple_mae = mean_absolute_error(
y_test_s,
simple_pred
)
simple_mse = mean_squared_error(
y_test_s,
simple_pred
)
simple_rmse = np.sqrt(simple_mse)
simple_r2 = r2_score(
y_test_s,
simple_pred
)
print("\n========================================")
print("SIMPLE LINEAR REGRESSION")
print("========================================")
print("Regression Equation:")
print(
"Performance =",
round(simple_model.intercept_, 2),
"+",
round(simple_model.coef_[0], 2),
"* Practice Hours"
)
print("\nMAE :", round(simple_mae, 4))
print("MSE :", round(simple_mse, 4))
print("RMSE :", round(simple_rmse, 4))
print("R2 :", round(simple_r2, 4))
# ---------------------------------------------------------
# Simple Regression Plot
# ---------------------------------------------------------
plt.figure(figsize=(8, 6))
plt.scatter(
X_simple,
y,
label="Actual Data"
)
plt.plot(
X_simple,
simple_model.predict(X_simple),
linewidth=2,
label="Regression Line"
)
plt.xlabel("Practice Hours")
plt.ylabel("Performance Score")
plt.title("Simple Linear Regression")
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
# =========================================================
# MODEL 2: MULTIPLE LINEAR REGRESSION
# =========================================================
multiple_model = LinearRegression()
multiple_model.fit(
X_train,
y_train
)
multiple_pred = multiple_model.predict(X_test)
# ---------------------------------------------------------
# Multiple Regression Evaluation
# ---------------------------------------------------------
multiple_mae = mean_absolute_error(
y_test,
multiple_pred
)
multiple_mse = mean_squared_error(
y_test,
multiple_pred
)
multiple_rmse = np.sqrt(multiple_mse)
multiple_r2 = r2_score(
y_test,
multiple_pred
)
print("\n========================================")
print("MULTIPLE LINEAR REGRESSION")
print("========================================")
print("\nRegression Coefficients:")
for feature, coefficient in zip(
features,
multiple_model.coef_
):
print(
feature,
":",
round(coefficient, 4)
)
print(
"\nIntercept:",
round(multiple_model.intercept_, 4)
)
print("\nMAE :", round(multiple_mae, 4))
print("MSE :", round(multiple_mse, 4))
print("RMSE :", round(multiple_rmse, 4))
print("R2 :", round(multiple_r2, 4))
# ---------------------------------------------------------
# Actual vs Predicted Plot
# ---------------------------------------------------------
plt.figure(figsize=(8, 6))
plt.scatter(
y_test,
multiple_pred,
s=70
)
plt.xlabel("Actual Performance Score")
plt.ylabel("Predicted Performance Score")
plt.title(
"Multiple Linear Regression: "
"Actual vs Predicted Performance"
)
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
# =========================================================
# MODEL COMPARISON
# =========================================================
comparison = pd.DataFrame({
"Model": [
"Simple Linear Regression",
"Multiple Linear Regression"
],
"MAE": [
simple_mae,
multiple_mae
],
"MSE": [
simple_mse,
multiple_mse
],
"RMSE": [
simple_rmse,
multiple_rmse
],
"R2 Score": [
simple_r2,
multiple_r2
]
})
print("\n========================================")
print("MODEL COMPARISON")
print("========================================")
print(
comparison.round(4)
)
# ---------------------------------------------------------
# Identify Best Model
# ---------------------------------------------------------
if multiple_r2 > simple_r2:
best_model = "Multiple Linear Regression"
else:
best_model = "Simple Linear Regression"
print("\nMost Suitable Model:", best_model)
# ---------------------------------------------------------
# Predict Performance of a New Player
# ---------------------------------------------------------
new_player = [[
15, # Practice Hours
80, # Fitness Score
20, # Match Experience
45 # Training Sessions
]]
predicted_performance = multiple_model.predict(
new_player
)
print("\n----- New Player Prediction -----")
print("Practice Hours :", new_player[0][0])
print("Fitness Score :", new_player[0][1])
print("Match Experience :", new_player[0][2])
print("Training Sessions :", new_player[0][3])
print(
"Predicted Performance Score:",
round(predicted_performance[0], 2)
)
SET C
1. Download a Mobile Phone Specifications dataset. Apply Multiple Linear Regression to predict mobile prices using features such as RAM, storage, battery capacity, and camera resolution. Categorize mobiles into Budget and Premium segments and build a Logistic Regression model for classification. Compare the performance of both models using appropriate regression and classification metrics.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score,
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report
)
# =========================================================
# STEP 1: READ MOBILE PHONE DATASET
# =========================================================
# Downloaded dataset file
df = pd.read_csv("mobile_phone.csv")
print("========== MOBILE PHONE DATASET ==========")
print(df.head())
# =========================================================
# STEP 2: DISPLAY DATASET INFORMATION
# =========================================================
print("\n========== DATASET INFORMATION ==========")
df.info()
print("\n========== STATISTICAL DESCRIPTION ==========")
print(df.describe())
print("\n========== MISSING VALUES ==========")
print(df.isnull().sum())
# =========================================================
# STEP 3: PREPROCESS DATA
# =========================================================
# Change these names if your dataset uses different names
features = [
"RAM",
"Storage",
"Battery",
"Camera"
]
target = "Price"
# Convert selected columns to numeric
for column in features + [target]:
df[column] = pd.to_numeric(
df[column],
errors="coerce"
)
# Remove missing values
df = df.dropna()
print("\n========== CLEANED DATASET ==========")
print(df[features + [target]].head())
# =========================================================
# PART A: MULTIPLE LINEAR REGRESSION
# =========================================================
print("\n\n==========================================")
print("MULTIPLE LINEAR REGRESSION")
print("==========================================")
X = df[features]
y = df[target]
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.20,
random_state=42
)
print("\nTraining Records:", len(X_train))
print("Testing Records :", len(X_test))
# Create model
linear_model = LinearRegression()
# Train model
linear_model.fit(X_train, y_train)
# Predict prices
y_pred = linear_model.predict(X_test)
# ---------------------------------------------------------
# Regression Coefficients
# ---------------------------------------------------------
print("\nRegression Coefficients:")
for feature, coefficient in zip(
features,
linear_model.coef_
):
print(
feature,
":",
round(coefficient, 4)
)
print(
"\nIntercept:",
round(linear_model.intercept_, 4)
)
# ---------------------------------------------------------
# Actual vs Predicted Price
# ---------------------------------------------------------
result = pd.DataFrame({
"Actual Price": y_test.values,
"Predicted Price": y_pred
})
print("\n========== ACTUAL VS PREDICTED PRICE ==========")
print(result)
# ---------------------------------------------------------
# Regression Metrics
# ---------------------------------------------------------
mae = mean_absolute_error(
y_test,
y_pred
)
mse = mean_squared_error(
y_test,
y_pred
)
rmse = np.sqrt(mse)
r2 = r2_score(
y_test,
y_pred
)
print("\n========== REGRESSION EVALUATION ==========")
print("MAE :", round(mae, 2))
print("MSE :", round(mse, 2))
print("RMSE :", round(rmse, 2))
print("R2 Score :", round(r2, 4))
# ---------------------------------------------------------
# Actual vs Predicted Price Plot
# ---------------------------------------------------------
plt.figure(figsize=(8, 6))
plt.scatter(
y_test,
y_pred,
s=70
)
# Reference line
minimum = min(y_test.min(), y_pred.min())
maximum = max(y_test.max(), y_pred.max())
plt.plot(
[minimum, maximum],
[minimum, maximum],
linewidth=2
)
plt.xlabel("Actual Mobile Price")
plt.ylabel("Predicted Mobile Price")
plt.title(
"Multiple Linear Regression: "
"Actual vs Predicted Price"
)
plt.grid(True, linestyle="--", alpha=0.5)
plt.tight_layout()
plt.show()
# =========================================================
# PART B: CREATE BUDGET / PREMIUM CATEGORY
# =========================================================
print("\n\n==========================================")
print("MOBILE PRICE CATEGORIZATION")
print("==========================================")
# Find median price as the classification threshold
threshold = df["Price"].median()
print(
"Price Classification Threshold:",
threshold
)
# 0 = Budget
# 1 = Premium
df["Price_Category"] = np.where(
df["Price"] <= threshold,
0,
1
)
print("\nPrice Categories:")
print("0 = Budget")
print("1 = Premium")
print("\nCategory Distribution:")
print(
df["Price_Category"].value_counts()
)
# =========================================================
# PART C: LOGISTIC REGRESSION
# =========================================================
print("\n\n==========================================")
print("LOGISTIC REGRESSION")
print("==========================================")
X_class = df[features]
y_class = df["Price_Category"]
# Split data
X_train_c, X_test_c, y_train_c, y_test_c = train_test_split(
X_class,
y_class,
test_size=0.20,
random_state=42,
stratify=y_class
)
print("\nTraining Records:", len(X_train_c))
print("Testing Records :", len(X_test_c))
# Create Logistic Regression model
logistic_model = LogisticRegression(
max_iter=2000
)
# Train model
logistic_model.fit(
X_train_c,
y_train_c
)
# Predict categories
y_pred_c = logistic_model.predict(X_test_c)
# ---------------------------------------------------------
# Classification Results
# ---------------------------------------------------------
print("\nActual Categories:")
print(y_test_c.values)
print("\nPredicted Categories:")
print(y_pred_c)
# ---------------------------------------------------------
# Classification Metrics
# ---------------------------------------------------------
accuracy = accuracy_score(
y_test_c,
y_pred_c
)
precision = precision_score(
y_test_c,
y_pred_c,
zero_division=0
)
recall = recall_score(
y_test_c,
y_pred_c,
zero_division=0
)
f1 = f1_score(
y_test_c,
y_pred_c,
zero_division=0
)
print("\n========== CLASSIFICATION EVALUATION ==========")
print("Accuracy :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall :", round(recall, 4))
print("F1-Score :", round(f1, 4))
# ---------------------------------------------------------
# Classification Report
# ---------------------------------------------------------
print("\n========== CLASSIFICATION REPORT ==========")
print(
classification_report(
y_test_c,
y_pred_c,
target_names=[
"Budget",
"Premium"
],
zero_division=0
)
)
# =========================================================
# STEP 4: CONFUSION MATRIX
# =========================================================
cm = confusion_matrix(
y_test_c,
y_pred_c
)
print("\n========== CONFUSION MATRIX ==========")
print(cm)
plt.figure(figsize=(7, 5))
sns.heatmap(
cm,
annot=True,
fmt="d",
xticklabels=[
"Budget",
"Premium"
],
yticklabels=[
"Budget",
"Premium"
]
)
plt.xlabel("Predicted Category")
plt.ylabel("Actual Category")
plt.title(
"Confusion Matrix - Mobile Price Classification"
)
plt.tight_layout()
plt.show()
# =========================================================
# STEP 5: COMPARE THE MODELS
# =========================================================
print("\n\n==========================================")
print("MODEL COMPARISON")
print("==========================================")
comparison = pd.DataFrame({
"Model": [
"Multiple Linear Regression",
"Logistic Regression"
],
"MAE": [
mae,
np.nan
],
"MSE": [
mse,
np.nan
],
"RMSE": [
rmse,
np.nan
],
"R2 Score": [
r2,
np.nan
],
"Accuracy": [
np.nan,
accuracy
],
"F1 Score": [
np.nan,
f1
]
})
print(
comparison.round(4)
)
# =========================================================
# STEP 6: PREDICT A NEW MOBILE
# =========================================================
print("\n\n==========================================")
print("NEW MOBILE PREDICTION")
print("==========================================")
# Example:
# RAM = 8 GB
# Storage = 256 GB
# Battery = 5000 mAh
# Camera = 64 MP
new_mobile = [[
8,
256,
5000,
64
]]
# Predict actual price
predicted_price = linear_model.predict(
new_mobile
)
# Predict category
predicted_category = logistic_model.predict(
new_mobile
)
print(
"Predicted Mobile Price:",
round(predicted_price[0], 2)
)
if predicted_category[0] == 0:
print("Predicted Category: Budget")
else:
print("Predicted Category: Premium")
2. Use a YouTube Video Statistics dataset to predict video views using Simple Linear Regression based on the number of likes. Convert the target variable into Popular and Non-Popular categories using a suitable threshold and apply Logistic Regression. Evaluate the regression model using MAE, MSE, RMSE, and R² Score, and evaluate the classification model using Accuracy, Precision, Recall, F1-Score, and Confusion Matrix.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score,
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report
)
# =========================================================
# STEP 1: READ YOUTUBE DATASET
# =========================================================
df = pd.read_csv("youtube_video_statistics.csv")
print("========== FIRST 5 RECORDS ==========")
print(df.head())
print("\n========== DATASET INFORMATION ==========")
df.info()
print("\n========== STATISTICAL DESCRIPTION ==========")
print(df.describe())
print("\n========== MISSING VALUES ==========")
print(df.isnull().sum())
# =========================================================
# STEP 2: PREPROCESSING
# =========================================================
# Change these column names if your dataset uses different names
# Example: views, likes
df["Views"] = pd.to_numeric(
df["Views"],
errors="coerce"
)
df["Likes"] = pd.to_numeric(
df["Likes"],
errors="coerce"
)
# Remove missing values
df = df.dropna(
subset=["Views", "Likes"]
)
print("\n========== CLEANED DATA ==========")
print(df[["Likes", "Views"]].head())
# =========================================================
# PART A: SIMPLE LINEAR REGRESSION
# =========================================================
print("\n==========================================")
print("SIMPLE LINEAR REGRESSION")
print("==========================================")
# Independent variable = Likes
# Dependent variable = Views
X = df[["Likes"]]
y = df["Views"]
# Split dataset into training and testing
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.20,
random_state=42
)
print("\nTraining Records:", len(X_train))
print("Testing Records :", len(X_test))
# Create Linear Regression model
linear_model = LinearRegression()
# Train model
linear_model.fit(
X_train,
y_train
)
# Predict views
y_pred = linear_model.predict(
X_test
)
# =========================================================
# REGRESSION EQUATION
# =========================================================
print("\nRegression Equation:")
print(
"Views =",
round(linear_model.intercept_, 2),
"+",
round(linear_model.coef_[0], 2),
"* Likes"
)
# =========================================================
# ACTUAL VS PREDICTED VALUES
# =========================================================
result = pd.DataFrame({
"Likes": X_test["Likes"].values,
"Actual Views": y_test.values,
"Predicted Views": y_pred
})
print("\n========== ACTUAL VS PREDICTED ==========")
print(result)
# =========================================================
# REGRESSION EVALUATION
# =========================================================
mae = mean_absolute_error(
y_test,
y_pred
)
mse = mean_squared_error(
y_test,
y_pred
)
rmse = np.sqrt(mse)
r2 = r2_score(
y_test,
y_pred
)
print("\n========== REGRESSION EVALUATION ==========")
print("MAE :", round(mae, 2))
print("MSE :", round(mse, 2))
print("RMSE :", round(rmse, 2))
print("R2 Score :", round(r2, 4))
# =========================================================
# REGRESSION PLOT
# =========================================================
plt.figure(figsize=(9, 6))
plt.scatter(
df["Likes"],
df["Views"],
label="Actual Data"
)
# Sort values so regression line is displayed correctly
sorted_df = df.sort_values("Likes")
plt.plot(
sorted_df["Likes"],
linear_model.predict(
sorted_df[["Likes"]]
),
linewidth=2,
label="Regression Line"
)
plt.xlabel("Number of Likes")
plt.ylabel("Number of Views")
plt.title(
"Simple Linear Regression: "
"Likes vs Views"
)
plt.legend()
plt.grid(
True,
linestyle="--",
alpha=0.5
)
plt.tight_layout()
plt.show()
# =========================================================
# PART B: CREATE POPULAR / NON-POPULAR CATEGORY
# =========================================================
print("\n==========================================")
print("POPULARITY CLASSIFICATION")
print("==========================================")
# Use median views as the popularity threshold
threshold = df["Views"].median()
print(
"\nPopularity Threshold:",
threshold
)
# 0 = Non-Popular
# 1 = Popular
df["Popularity"] = np.where(
df["Views"] >= threshold,
1,
0
)
print("\nCategory Definition:")
print("0 = Non-Popular")
print("1 = Popular")
print("\nPopularity Distribution:")
print(
df["Popularity"].value_counts()
)
# =========================================================
# PART C: LOGISTIC REGRESSION
# =========================================================
print("\n==========================================")
print("LOGISTIC REGRESSION")
print("==========================================")
# Use Likes to classify popularity
X_class = df[["Likes"]]
y_class = df["Popularity"]
# Split dataset
X_train_c, X_test_c, y_train_c, y_test_c = train_test_split(
X_class,
y_class,
test_size=0.20,
random_state=42,
stratify=y_class
)
print("\nTraining Records:", len(X_train_c))
print("Testing Records :", len(X_test_c))
# Create Logistic Regression model
logistic_model = LogisticRegression(
max_iter=2000
)
# Train model
logistic_model.fit(
X_train_c,
y_train_c
)
# Predict popularity
y_pred_c = logistic_model.predict(
X_test_c
)
# =========================================================
# CLASSIFICATION RESULTS
# =========================================================
print("\nActual Categories:")
print(y_test_c.values)
print("\nPredicted Categories:")
print(y_pred_c)
# =========================================================
# CLASSIFICATION METRICS
# =========================================================
accuracy = accuracy_score(
y_test_c,
y_pred_c
)
precision = precision_score(
y_test_c,
y_pred_c,
zero_division=0
)
recall = recall_score(
y_test_c,
y_pred_c,
zero_division=0
)
f1 = f1_score(
y_test_c,
y_pred_c,
zero_division=0
)
print("\n========== CLASSIFICATION EVALUATION ==========")
print("Accuracy :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall :", round(recall, 4))
print("F1-Score :", round(f1, 4))
# =========================================================
# CLASSIFICATION REPORT
# =========================================================
print("\n========== CLASSIFICATION REPORT ==========")
print(
classification_report(
y_test_c,
y_pred_c,
target_names=[
"Non-Popular",
"Popular"
],
zero_division=0
)
)
# =========================================================
# CONFUSION MATRIX
# =========================================================
cm = confusion_matrix(
y_test_c,
y_pred_c
)
print("\n========== CONFUSION MATRIX ==========")
print(cm)
plt.figure(figsize=(7, 5))
sns.heatmap(
cm,
annot=True,
fmt="d",
xticklabels=[
"Non-Popular",
"Popular"
],
yticklabels=[
"Non-Popular",
"Popular"
]
)
plt.xlabel("Predicted Category")
plt.ylabel("Actual Category")
plt.title(
"Confusion Matrix - YouTube Popularity Classification"
)
plt.tight_layout()
plt.show()
# =========================================================
# PART D: PREDICT A NEW VIDEO
# =========================================================
print("\n==========================================")
print("NEW VIDEO PREDICTION")
print("==========================================")
# Example video with 10,000 likes
new_video = [[10000]]
# Predict number of views using Linear Regression
predicted_views = linear_model.predict(
new_video
)
# Predict popularity using Logistic Regression
predicted_category = logistic_model.predict(
new_video
)
print(
"Number of Likes:",
new_video[0][0]
)
print(
"Predicted Views:",
round(predicted_views[0], 2)
)
if predicted_category[0] == 1:
print("Predicted Category: Popular")
else:
print("Predicted Category: Non-Popular")
No comments:
Post a Comment