PACTICAL NO: 10
Exercise: 1. Modify the given X, y =
program to use your own dataset make_classification(n_samples=1000,
n_features=10, n_informative=5,
instead of breast-cancer dataset. 2.
n_redundant=2, random_state=101)
Change the test size to 30% and
find the new accuracy. # 2. Split with 30% Test Size
X_train, X_test, y_train, y_test =
CODE: train_test_split(X, y, test_size=0.3,
print("[Exercise: Q1 Modify the given random_state=42)
program to use your own dataset instead of
breast-cancer dataset]") # Feature Scaling
print("[Exercise: Q2 Change the test size scaler = StandardScaler()
to 30% and find the new accuracy]") X_train_scaled =
scaler.fit_transform(X_train)
import numpy as np X_test_scaled = [Link](X_test)
import pandas as pd
import [Link] as plt # Logistic Regression Model
import seaborn as sns model =
from sklearn.model_selection import LogisticRegression(random_state=42)
train_test_split [Link](X_train_scaled, y_train)
from [Link] import y_pred = [Link](X_test_scaled)
StandardScaler y_prob =
from sklearn.linear_model import model.predict_proba(X_test_scaled)[:, 1] #
LogisticRegression Probabilities for ROC curve
from [Link] import
accuracy_score, confusion_matrix,
classification_report, roc_curve, auc
from [Link] import # PLOT 1: Confusion Matrix Heatmap
make_classification [Link](figsize=(10, 7)) # Separate
Figure
# --- THEME SETUP --- cm = confusion_matrix(y_test, y_pred)
# 1. Dark Background # Using 'mako' as requested
[Link]('dark_background') [Link](cm, annot=True, fmt='d',
# 2. Monospace Font (Cyberpunk/Code cmap='mako', cbar=False,
style) annot_kws={"size": 18, "weight":
[Link]['[Link]'] = 'monospace' "bold", "color": neon_cyan})
# 3. Custom Neon Palette (No Pink)
neon_cyan = '#00FFCC' [Link]('CONFUSION MATRIX',
neon_lime = '#CCFF00' fontsize=16, fontweight='bold',
color=neon_lime)
# 1. Generate Custom Dataset (More [Link]('Predicted Label', fontsize=12,
robust synthetic data) color='white')
PACTICAL NO: 10
[Link]('True Label', fontsize=12,
color='white')
[Link](False) # Turn off grid for heatmap
cleaner look
[Link]()
# PLOT 2: ROC Curve
fpr, tpr, thresholds = roc_curve(y_test,
y_prob)
roc_auc = auc(fpr, tpr)
[Link](figsize=(10, 7))
[Link](fpr, tpr, color=neon_cyan, lw=3,
label=f'ROC Curve (AUC =
{roc_auc:.2f})')
[Link]([0, 1], [0, 1], color='white', lw=2,
linestyle='--')
[Link]([0.0, 1.0])
[Link]([0.0, 1.05])
[Link]('False Positive Rate',
fontsize=12, color='white')
[Link]('True Positive Rate',
fontsize=12, color='white')
[Link]('RECEIVER OPERATING
CHARACTERISTIC (ROC)', fontsize=16,
fontweight='bold', color=neon_lime)
[Link](loc="lower right", fontsize=12)
[Link](True, alpha=0.3) # Subtle grid
[Link]()
print(f"New Accuracy with 30% Test Split:
{accuracy_score(y_test,
y_pred)*100:.2f}%")
print("\nClassification Report:\n",
classification_report(y_test, y_pred))
OUTPUT:
PACTICAL NO: 10
PACTICAL NO: 10
3. Apply logistic regression to
identify whether a student will X_vals = df_student[['Study_Hours',
'Marks']].values
pass or fail based on his marks &
y_vals = df_student['Result'].values
study hours.
X_train_st, X_test_st, y_train_st, y_test_st
CODE:
= train_test_split(X_vals, y_vals,
print("[Exercise: Q3 Apply logistic
test_size=0.3, random_state=0)
regression to identify whether a student
scaler_st = StandardScaler()
will pass or fail based on his marks &
X_train_st =
study hours]")
scaler_st.fit_transform(X_train_st)
X_test_st = scaler_st.transform(X_test_st)
import pandas as pd
import numpy as np
# Train Model
import [Link] as plt
model_st = LogisticRegression()
from [Link] import
model_st.fit(X_train_st, y_train_st)
ListedColormap
from sklearn.model_selection import
# Prediction
train_test_split
acc = model_st.score(X_test_st, y_test_st)
from [Link] import
print(f"Model Accuracy on Student Data:
StandardScaler
{acc*100:.2f}%")
from sklearn.linear_model import
LogisticRegression
# --- Visualization: Decision Boundary ---
X_set, y_set =
scaler_st.inverse_transform(X_train_st),
[Link]('dark_background')
y_train_st # Plotting training data points
[Link]['[Link]'] = 'monospace'
# Create a meshgrid to plot the decision
boundary
color_fail = '#FF4444' # Neon Red
X1, X2 = [Link](
color_pass = '#00FF00' # Neon Green
[Link](start=X_set[:, 0].min() - 1,
stop=X_set[:, 0].max() + 1, step=0.01),
[Link](start=X_set[:, 1].min() - 1,
data = {
stop=X_set[:, 1].max() + 1, step=0.01)
'Study_Hours': [0.5, 1.0, 1.5, 2.0, 2.5,
)
3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 7.0, 8.0],
'Marks': [10, 15, 20, 25, 30, 35, 55, 60,
# Predict for every point on the grid
65, 70, 75, 80, 85, 90],
grid_points = [Link]([[Link](),
'Result': [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
[Link]()]).T
1, 1]
grid_points_scaled =
}
scaler_st.transform(grid_points)
df_student = [Link](data)
PACTICAL NO: 10
Z= color=custom_cmap(i),
model_st.predict(grid_points_scaled).resha label=['Fail (0)', 'Pass (1)'][j],
pe([Link]) edgecolor='white', # White edge
to make dots pop against dark background
# Plot s=100)
[Link](figsize=(10, 6))
[Link]('LOGISTIC REGRESSION:
# Define Colormap (Red/Green) STUDY HOURS VS MARKS',
custom_cmap = fontsize=14, fontweight='bold',
ListedColormap((color_fail, color_pass)) color='white')
[Link]('Study Hours', fontsize=12,
# Contourf plots the background color color='white')
regions based on prediction (Z) [Link]('Marks', fontsize=12,
[Link](X1, X2, Z, alpha=0.2, color='white')
cmap=custom_cmap) [Link](facecolor='black',
[Link]([Link](), [Link]()) edgecolor='white')
[Link]([Link](), [Link]()) [Link](True, color='white', linestyle='--',
alpha=0.1)
# Scatter plot for actual data points [Link]()
for i, j in enumerate([Link](y_set)):
[Link](X_set[y_set == j, 0], OUTPUT:
X_set[y_set == j, 1],