Ad Click Prediction Using Logistic Regression
INTRODUCTION:
Logistic regression is a data analysis technique that uses mathematics to find the relationships between two data factors. It then uses this relationship to predict the value of one of those factors based on the other. The prediction usually has a finite number of outcomes, like yes or no.
CODE 😃👇:
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.preprocessing import StandardScaler
# Load the dataset
# Replace ‘path_to_csv’ with the actual file path
df = pd.read_csv(‘/content/advertising.csv’)
# Display the first few rows of the dataset
print(“Dataset Preview:\n”, df.head())
# Select relevant features and the target variable
# Dropping columns that are not directly useful for prediction
df = df[[‘Daily Time Spent on Site’, ‘Age’, ‘Area Income’, ‘Daily Internet Usage’, ‘Male’, ‘Clicked on Ad’]]
# Separate the features (X) and target variable (y)
X = df.drop(“Clicked on Ad”, axis=1)
y = df[“Clicked on Ad”]
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize the feature values
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Initialize and train the Logistic Regression model
log_reg_model = LogisticRegression()
log_reg_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = log_reg_model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(“\nAccuracy of the Logistic Regression model:”, accuracy)
print(“\nClassification Report:\n”, classification_report(y_test, y_pred))
print(“\nConfusion Matrix:\n”, confusion_matrix(y_test, y_pred))
No comments:
Post a Comment