Third Year Engineering (2019 Pattern)
Course Code: 310256
Course Name: Data Science and Big Data Analytics Laboratory
Group A
4) Data Analytics II
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score,
recall_score
# Step 1: Load the dataset
df = pd.read_csv("Social_Network_Ads.csv")
print("\nDataset Info:")
print(df.info())
print("\nFirst 5 Rows:")
print(df.head())
# Step 2: Data Preprocessing
# Selecting relevant features and target variable
X = df[['Age', 'EstimatedSalary']]
y = df['Purchased'] # Target variable
# Splitting dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Feature Scaling
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Step 3: Train Logistic Regression Model
model = LogisticRegression()
model.fit(X_train_scaled, y_train)
# Step 4: Make Predictions
y_pred = model.predict(X_test_scaled)
# Step 5: Compute Confusion Matrix
conf_matrix = confusion_matrix(y_test, y_pred)
tn, fp, fn, tp = conf_matrix.ravel()
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
error_rate = 1 - accuracy
# Step 6: Display Results
print("\nConfusion Matrix:")
print(conf_matrix)
print(f"\nTrue Positives (TP): {tp}")
print(f"False Positives (FP): {fp}")
print(f"True Negatives (TN): {tn}")
print(f"False Negatives (FN): {fn}")
print(f"Accuracy: {accuracy:.2f}")
print(f"Error Rate: {error_rate:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
Explanation of Each Step:
1. Loading the Dataset
o Read Social_Network_Ads.csv into a Pandas DataFrame.
2. Data Preprocessing
o Selected Age and EstimatedSalary as features.
o Used Purchased as the target variable.
o Applied StandardScaler() for feature scaling.
3. Splitting the Data
o Split into 80% training and 20% testing using train_test_split().
4. Training the Model
o Trained a Logistic Regression model using LogisticRegression().
5. Making Predictions
o Predicted labels for the test set using .predict().
6. Computing the Confusion Matrix
o Extracted True Positives (TP), False Positives (FP), True Negatives (TN),
False Negatives (FN).
o Calculated Accuracy, Error Rate, Precision, and Recall.
OUTPUT-