0% found this document useful (0 votes)
8 views30 pages

Student Data Cleaning and Analysis

The document outlines several Python programs focused on data processing and analysis, including handling missing values, detecting outliers, and building classification models for student performance and loan predictions. It also includes a recipe classification model and clustering analysis on student performance data. Each program utilizes libraries like pandas, numpy, and scikit-learn for data manipulation and machine learning tasks.

Uploaded by

Re Vani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views30 pages

Student Data Cleaning and Analysis

The document outlines several Python programs focused on data processing and analysis, including handling missing values, detecting outliers, and building classification models for student performance and loan predictions. It also includes a recipe classification model and clustering analysis on student performance data. Each program utilizes libraries like pandas, numpy, and scikit-learn for data manipulation and machine learning tasks.

Uploaded by

Re Vani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1: program for plot handling missing values, and duplication values in the

Student database
import pandas as pd
import numpy as np
import [Link] as plt
df=pd.read_csvSAMPLE-[Link]");
print([Link]());
display(df)
#
missing_values = [Link]().sum()
print(f"Number of Missing Values: {missing_values}")
[Link](figsize=(10, 6))
missing_values.plot(kind='bar')
[Link]('Columns')
[Link]('Number of Missing Values')
plt.tight_layout()
[Link]()
#-
duplicate_rows = [Link]()
num_duplicate_rows = duplicate_rows.sum()
print(f"Number of duplicate rows: {num_duplicate_rows}")

[Link](figsize=(6, 4))
[Link](['Duplicate Rows'], [num_duplicate_rows])
[Link]('Count of Duplicate Rows')
[Link]('Count')
[Link]()
Program 2: program for handling missing values, and duplication values in the Student
database

import pandas as pd
import numpy as np
import [Link] as plt
df=pd.read_csvSAMPLE-[Link]");
print([Link]());
display(df)
# For numeric columns - use mean
df['Age'] = df['Age'].fillna(df['Age'].mean())
df['1st Yr %'] = df['1st Yr %'].fillna(df['1st Yr %'].mean())
df['2nd Yr %'] = df['2nd Yr %'].fillna(df['2nd Yr %'].mean())

# For categorical columns - use mode


df['Gender'] = df['Gender'].fillna(df['Gender'].mode()[0])
df['Area'] = df['Area'].fillna(df['Area'].mode()[0])

# For email and mobile


df['Email'] = df['Email'].fillna('Not Available')
df['Mobile No'] = df['Mobile No'].fillna(0)
# Handling Duplicate values
df.drop_duplicates(inplace=True)

display(df)
df.to_csv(“[Link]”)
Program 3: Outlier Detection
import seaborn as sns
import pandas as pd
import numpy as np
import [Link] as plt #
df = pd.read_csv("[Link]")
print(df)
# Convert percentage columns to numeric
for col in ['1st Yr %', '2nd Yr %']:
df[col] = pd.to_numeric(df[col], errors='coerce')
def find_outliers(col):
Q1 = df[col].quantile(0.25)
Q3 = df[col].quantile(0.75)
IQR = Q3 - Q1
lower, upper = Q1 - 1.5 * IQR, Q3 + 1.5 * IQR
return df[(df[col] < lower) | (df[col] > upper)]
# Find and merge outliers
outliers = [Link]([find_outliers(col) for col in ['1st Yr %', '2nd Yr %']]).drop_duplicates()
print(outliers)
# Plotting
[Link](figsize=(8, 5))
[Link](data=df, x='1st Yr %', y='2nd Yr %', s=80, label='Normal')
[Link](data=outliers, x='1st Yr %', y='2nd Yr %', color='red', marker='X', s=120, label='Outlier')
[Link]('Academic Percentage Outliers')
[Link]('1st Year %')
[Link]('2nd Year %')
[Link]()
[Link](True)
[Link]()
Classification Models
Program 4: Student Performance Classification
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from sklearn.model_selection import train_test_split


from [Link] import LabelEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import DecisionTreeClassifier
from [Link] import (classification_report, confusion_matrix, accuracy_score,
roc_curve, roc_auc_score, ConfusionMatrixDisplay)

# Load data
data = pd.read_csv('/content/sample_data/[Link]')

# Drop unnecessary columns if present


data = [Link](columns=[col for col in ['Unnamed: 0', 'Name'] if col in [Link]])

# Drop duplicates
data = data.drop_duplicates()

# Encode categorical columns


le = LabelEncoder()
data['Gender'] = le.fit_transform(data['Gender'])
data['City'] = le.fit_transform(data['City'])

# Create binary target based on 2nd Year %


data['Performance'] = (data['2nd Yr %'] >= 85).astype(int)

# Feature selection and engineering


data['Average %'] = (data['1st Yr %'] + data['2nd Yr %']) / 2
features = ['Age', 'Gender', '1st Yr %', 'City', 'Average %']
X = data[features]
y = data['Performance']

# Scale features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=42)

# Train models
lr = LogisticRegression(max_iter=500)
[Link](X_train, y_train)
y_pred_lr = [Link](X_test)

dt = DecisionTreeClassifier(max_depth=4)
[Link](X_train, y_train)
y_pred_dt = [Link](X_test)

# Accuracy
print("Accuracy (Logistic Regression):", accuracy_score(y_test, y_pred_lr))
print("Accuracy (Decision Tree):", accuracy_score(y_test, y_pred_dt))

# Confusion Matrices
fig, axes = [Link](1, 2, figsize=(12, 5))
[Link](confusion_matrix(y_test, y_pred_lr), annot=True, fmt='d', ax=axes[0],
cmap='Blues')
axes[0].set_title("Logistic Regression")
[Link](confusion_matrix(y_test, y_pred_dt), annot=True, fmt='d', ax=axes[1],
cmap='Greens')
axes[1].set_title("Decision Tree")
[Link]()

# ROC Curve
y_prob_lr = lr.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_prob_lr)
[Link](fpr, tpr, label='Logistic Regression')
[Link]([0, 1], [0, 1], linestyle='--', color='gray')
[Link]("False Positive Rate")
[Link]("True Positive Rate")
[Link]("ROC Curve")
[Link]()
[Link]()

# Feature Importance (Decision Tree)


[Link](figsize=(8, 4))
[Link](features, dt.feature_importances_)
[Link]("Feature Importance - Decision Tree")
[Link]()
5. Loan Prediction with Feature Engineering and Classification
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from [Link] import RandomForestClassifier


from [Link] import SVC
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import classification_report, confusion_matrix, accuracy_score, roc_curve,
roc_auc_score

# ----------------------------
# Generate Synthetic Loan Data
# ----------------------------
[Link](42)
n = 100 # sample size
data = [Link]({
'Age': [Link](21, 60, n),
'Income': [Link](20, 150, n), # in thousands
'Credit_Score': [Link](300, 850, n),
'Loan_Amount': [Link](5, 100, n), # in thousands
'Loan_Term': [Link]([12, 24, 36, 60], n),
'Employment_Status': [Link]([0, 1, 2], n),
'Married': [Link]([0, 1], n),
'Approved': [Link]([0, 1], n)
})

# ----------------------------
# Feature Engineering
# ----------------------------

# 1. Debt-to-Income Ratio
data['DTI'] = data['Loan_Amount'] / data['Income']

# 2. Loan-to-Income Ratio
data['LoanToIncome'] = (data['Loan_Amount'] * 1000) / (data['Income'] * 1000)

# 3. Credit Score Category (0=Low, 1=Mid, 2=High)


def credit_bucket(score):
if score < 580:
return 0 # Low
elif score < 700:
return 1 # Mid
else:
return 2 # High
data['Credit_Bucket'] = data['Credit_Score'].apply(credit_bucket)

# ----------------------------
# Feature Selection & Scaling
# ----------------------------
features = ['Age', 'Income', 'Loan_Amount', 'Loan_Term', 'Employment_Status',
'Married', 'DTI', 'LoanToIncome', 'Credit_Bucket']
X = data[features]
y = data['Approved']

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=42)

# ----------------------------
# Model Training
# ----------------------------
# 1. Random Forest
rf = RandomForestClassifier(random_state=0)
[Link](X_train, y_train)
y_pred_rf = [Link](X_test)

# 2. SVM
svm = SVC(probability=True)
[Link](X_train, y_train)
y_pred_svm = [Link](X_test)

# ----------------------------
# Evaluation
# ----------------------------

print("Accuracy (Random Forest):", accuracy_score(y_test, y_pred_rf))


print("Accuracy (SVM):", accuracy_score(y_test, y_pred_svm))

# Confusion Matrix
fig, axes = [Link](1, 2, figsize=(12, 5))
[Link](confusion_matrix(y_test, y_pred_rf), annot=True, fmt='d', ax=axes[0],
cmap='Blues')
axes[0].set_title("Random Forest")
[Link](confusion_matrix(y_test, y_pred_svm), annot=True, fmt='d', ax=axes[1],
cmap='Greens')
axes[1].set_title("SVM")
[Link]()
# Classification Reports
print("Random Forest Report:\n", classification_report(y_test, y_pred_rf))
print("SVM Report:\n", classification_report(y_test, y_pred_svm))
6. Smart Recipe Classifiers
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt

from [Link] import MultiLabelBinarizer


from sklearn.naive_bayes import MultinomialNB
from [Link] import KNeighborsClassifier
from [Link] import classification_report, confusion_matrix, accuracy_score

# ----------------------------
# 1. Create Sample Recipe Dataset
# ----------------------------
data = [
{'Recipe': 'Pasta Alfredo', 'Ingredients': ['pasta', 'milk', 'cheese'], 'MealType': 'Dinner'},
{'Recipe': 'Chicken Curry', 'Ingredients': ['chicken', 'onion', 'garlic'], 'MealType': 'Dinner'},
{'Recipe': 'Evening Pasta', 'Ingredients': ['pasta', 'butter', 'cheese'], 'MealType': 'Dinner'},

{'Recipe': 'Veggie Omelette', 'Ingredients': ['egg', 'onion', 'tomato'], 'MealType': 'Breakfast'},


{'Recipe': 'Banana Smoothie', 'Ingredients': ['banana', 'milk', 'honey'], 'MealType': 'Breakfast'},
{'Recipe': 'Boiled Eggs', 'Ingredients': ['egg', 'salt'], 'MealType': 'Breakfast'},

{'Recipe': 'Fried Rice', 'Ingredients': ['rice', 'carrot', 'peas'], 'MealType': 'Lunch'},


{'Recipe': 'Paneer Wrap', 'Ingredients': ['paneer', 'bread', 'onion'], 'MealType': 'Lunch'},
{'Recipe': 'Rice Bowl', 'Ingredients': ['rice', 'chicken', 'peas'], 'MealType': 'Lunch'},

{'Recipe': 'Cheese Sandwich', 'Ingredients': ['bread', 'cheese', 'butter'], 'MealType': 'Snack'},


{'Recipe': 'Fruit Salad', 'Ingredients': ['apple', 'banana', 'orange'], 'MealType': 'Snack'},
{'Recipe': 'Cheese Toast', 'Ingredients': ['bread', 'cheese'], 'MealType': 'Snack'},
]

df = [Link](data)

# ----------------------------
# 2. Feature Engineering
# ----------------------------
mlb = MultiLabelBinarizer()
X = [Link](mlb.fit_transform(df['Ingredients']), columns=mlb.classes_)
y = df['MealType']

# Manual split to ensure all classes represented


df['MealType_id'] = [Link]('MealType').cumcount()
train_idx = df[df['MealType_id'] == 0].index
val_idx = df[df['MealType_id'] == 1].index
test_idx = df[df['MealType_id'] == 2].index
X_train, y_train = [Link][train_idx], [Link][train_idx]
X_val, y_val = [Link][val_idx], [Link][val_idx]
X_test, y_test = [Link][test_idx], [Link][test_idx]

# ----------------------------
# 3. Model Training & Evaluation
# ----------------------------
models = {
'Naive Bayes': MultinomialNB(),
'KNN': KNeighborsClassifier(n_neighbors=1)
}

for name, model in [Link]():


[Link](X_train, y_train)
val_pred = [Link](X_val)
test_pred = [Link](X_test)

print(f"\n🔹 {name}")
print(f"Validation Accuracy: {accuracy_score(y_val, val_pred):.2f}")
print(f"Test Accuracy: {accuracy_score(y_test, test_pred):.2f}")
print("Classification Report (Test):\n", classification_report(y_test, test_pred))

# ----------------------------
# 5. User Input Prediction
# ----------------------------
def predict_meal_type_and_recipe(user_ingredients, model):
user_ingredients = [[Link]().lower() for ing in user_ingredients]

# Create input vector


user_input = [Link]([0]*len(mlb.classes_), index=mlb.classes_).T
user_input[user_ingredients] = 1

# Predict meal type


predicted_meal = [Link](user_input)[0]

# Filter recipes by predicted meal type


candidates = df[df['MealType'] == predicted_meal].copy()

# Score by matching ingredient count


candidates['match_score'] = candidates['Ingredients'].apply(
lambda ing_list: len(set(user_ingredients) & set(ing_list))
)
# Get best-matching recipe(s)
top_match = candidates.sort_values(by='match_score', ascending=False).iloc[0]

return predicted_meal, top_match['Recipe']

# Accept user input


print("\n🌟 Enter ingredients separated by commas (e.g., 'bread, cheese, butter')")
user_input_str = input("Ingredients: ")
user_ingredients = user_input_str.split(',')

# Predict
predicted_meal, recipe_name = predict_meal_type_and_recipe(user_ingredients, models['Naive
Bayes'])

# Display
print(f"\n🍽️ Suggested Meal Type: {predicted_meal}")
print(f"📌 Best Matching Recipe: {recipe_name}")
Clustering
7. Student Performance Cluster
#!pip install pandas numpy matplotlib seaborn scikit-learn opencv-python Pillow scikit-image
category_encoders feature-engine shap yellowbrick mlxtend optuna torch torchvision pandas-
profiling albumentations imgaug

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import StandardScaler
from [Link] import KMeans, AgglomerativeClustering
from [Link] import PCA
from [Link] import silhouette_score

# 1. Simulated Student Dataset


data = {
'Name': list("ABCDEFGHIJKLMNO"),
'1st Yr %': [78, 65, 88, 92, 58, 73, 90, 67, 75, 80, 85, 62, 70, 91, 83],
'2nd Yr %': [80, 63, 85, 95, 60, 70, 88, 66, 72, 84, 87, 61, 69, 93, 81],
'Attendance %': [85, 60, 90, 95, 55, 75, 98, 62, 77, 89, 92, 59, 73, 96, 88]
}
df = [Link](data)

# 2. Feature Scaling
features = ['1st Yr %', '2nd Yr %', 'Attendance %']
scaler = StandardScaler()
X_scaled = scaler.fit_transform(df[features])

# 3. Apply Clustering
kmeans = KMeans(n_clusters=3, random_state=1)
df['KMeansCluster'] = kmeans.fit_predict(X_scaled)

agglo = AgglomerativeClustering(n_clusters=3)
df['AggloCluster'] = agglo.fit_predict(X_scaled)

# 4. Dimensionality Reduction (PCA)


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# 5. Plot PCA Cluster Results


[Link](figsize=(12, 5))

[Link](1, 2, 1)
[Link](x=X_pca[:, 0], y=X_pca[:, 1], hue=df['KMeansCluster'], palette='tab10', s=100)
[Link]("KMeans Clusters (PCA View)")
[Link]("PCA Component 1")
[Link]("PCA Component 2")

[Link](1, 2, 2)
[Link](x=X_pca[:, 0], y=X_pca[:, 1], hue=df['AggloCluster'], palette='Set2', s=100)
[Link]("Agglomerative Clusters (PCA View)")
[Link]("PCA Component 1")
[Link]("PCA Component 2")

plt.tight_layout()
[Link]()

# 6. Cluster Summary (Explainable)


def explain_clusters(method_name, label_column):
summary = [Link](label_column)[features].mean().round(2)
print(f"\n🍽️ {method_name} Cluster Summary:\n")
print(summary)

for cluster_id, row in [Link]():


print(f"\n🔹 Cluster {cluster_id}:")
if row['1st Yr %'] > 85 and row['Attendance %'] > 90:
print(" - Excellent students with high attendance.")
elif row['1st Yr %'] < 70 and row['Attendance %'] < 65:
print(" - Struggling students needing attention.")
else:
print(" - Moderate performance group.")

# Show KMeans explanation


explain_clusters("KMeans", "KMeansCluster")
# Show Agglomerative explanation
explain_clusters("Agglomerative", "AggloCluster")

# 8. Visual Cluster Comparison (Bar Plot)


kmeans_avg = [Link]('KMeansCluster')[features].mean().reset_index()
kmeans_avg['Method'] = 'KMeans'

agglo_avg = [Link]('AggloCluster')[features].mean().reset_index()
agglo_avg = agglo_avg.rename(columns={"AggloCluster": "Cluster"})
agglo_avg['Method'] = 'Agglomerative'

kmeans_avg = kmeans_avg.rename(columns={"KMeansCluster": "Cluster"})

combined_avg = [Link]([kmeans_avg, agglo_avg], ignore_index=True)


plot_data = combined_avg.melt(id_vars=['Method', 'Cluster'], var_name='Feature',
value_name='Average')

[Link](figsize=(10, 6))
[Link](data=plot_data, x='Feature', y='Average', hue='Method', errorbar=None)
[Link]("Cluster Feature Averages (KMeans vs Agglomerative)")
[Link]("Average %")
[Link]("Feature")
[Link](title="Clustering Method")
plt.tight_layout()
[Link]()

# 9. Final Data Overview


print("\n📋 Final Data with Cluster Assignments:\n")
print(df[['Name'] + features + ['KMeansCluster', 'AggloCluster']])
8. Recipe Cluster
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from [Link] import StandardScaler


from [Link] import DBSCAN, MeanShift
from [Link] import PCA
from [Link] import silhouette_score

# -------------------------
# 1. Sample Recipe Dataset
# -------------------------
recipes = [Link]({
'Recipe': [
'Oats Porridge', 'Grilled Chicken', 'Veg Salad', 'Pasta Alfredo', 'Fruit Smoothie',
'Paneer Wrap', 'Fried Rice', 'Mutton Curry', 'Veg Sandwich', 'Samosa',
'Boiled Egg', 'Dal Rice', 'Pizza Slice', 'Veg Soup', 'Roti & Sabji'
],
'Calories': [150, 300, 120, 400, 180, 280, 350, 500, 200, 450, 155, 290, 600, 160, 250],
'Protein': [5, 25, 3, 10, 6, 12, 8, 30, 7, 5, 13, 10, 15, 6, 8],
'Fat': [2, 10, 1, 15, 3, 8, 10, 25, 4, 20, 11, 8, 22, 2, 6],
'Carbs': [28, 5, 20, 50, 30, 25, 45, 20, 33, 40, 1, 35, 55, 15, 40]
})

# -------------------------
# 2. Preprocessing
# -------------------------
features = ['Calories', 'Protein', 'Fat', 'Carbs']
X = recipes[features]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# -------------------------
# 3. Clustering
# -------------------------
dbscan = DBSCAN(eps=1.3, min_samples=2)
recipes['DBSCAN_Cluster'] = dbscan.fit_predict(X_scaled)

meanshift = MeanShift()
recipes['MeanShift_Cluster'] = meanshift.fit_predict(X_scaled)

# -------------------------
# 4. PCA for Visualization
# -------------------------
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# -------------------------
# 5. Plot Clusters
# -------------------------
[Link](figsize=(12, 5))
# DBSCAN
[Link](1, 2, 1)
[Link](x=X_pca[:, 0], y=X_pca[:, 1], hue=recipes['DBSCAN_Cluster'], palette='Set1',
s=100)
[Link]("DBSCAN Clustering")
[Link]("PCA 1"); [Link]("PCA 2")
# MeanShift
[Link](1, 2, 2)
[Link](x=X_pca[:, 0], y=X_pca[:, 1], hue=recipes['MeanShift_Cluster'], palette='Set2',
s=100)
[Link]("MeanShift Clustering")
[Link]("PCA 1"); [Link]("PCA 2")
plt.tight_layout()
[Link]()

# -------------------------
# 6. Explain Clusters
# -------------------------
def explain_clusters(df, method, label):
print(f"\n🔍 {method} Cluster Summary:")
summary = [Link](label)[features].mean().round(2)
print(summary)
return summary

summary_db = explain_clusters(recipes, "DBSCAN", "DBSCAN_Cluster")


summary_ms = explain_clusters(recipes, "MeanShift", "MeanShift_Cluster")

# Cluster counts
db_clusters = recipes['DBSCAN_Cluster'].nunique()
ms_clusters = recipes['MeanShift_Cluster'].nunique()
print(f"\n📦 DBSCAN Clusters: {db_clusters}")
print(f"📦 MeanShift Clusters: {ms_clusters}")

# Recommendation
print("\n✅ Best Clustering Recommendation:")
if score_meanshift > score_dbscan:
print("👉 MeanShift is better based on higher silhouette score and more consistent clusters.")
else:
print("👉 DBSCAN is better based on silhouette score or noise detection.")

# -------------------------
# 9. Final Table
# -------------------------
print("\n📋 Final Recipe Clustering Table:")
print(recipes[['Recipe'] + features + ['DBSCAN_Cluster', 'MeanShift_Cluster']])
9. Loan Cluster
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from [Link] import StandardScaler


from [Link] import PCA
from [Link] import DBSCAN, SpectralClustering
from [Link] import silhouette_score

# -----------------------------
# 1. Sample Loan Dataset (15 rows)
# -----------------------------
loan_data = [Link]({
'Applicant': [f'Applicant_{i+1}' for i in range(15)],
'Age': [25, 34, 28, 40, 36, 45, 30, 32, 29, 50, 42, 27, 48, 38, 31],
'Income': [30000, 55000, 40000, 80000, 62000, 90000, 45000, 47000, 42000, 100000, 85000,
39000, 95000, 70000, 48000],
'LoanAmount': [100000, 150000, 120000, 200000, 170000, 220000, 130000, 140000, 125000,
250000, 210000, 110000, 240000, 180000, 135000],
'CreditScore': [650, 720, 690, 800, 750, 820, 700, 710, 695, 850, 790, 680, 840, 760, 705],
'LoanTenure': [5, 10, 6, 15, 12, 20, 7, 8, 6, 25, 18, 5, 22, 14, 8]
})

# -----------------------------
# 2. Preprocessing
# -----------------------------
features = ['Age', 'Income', 'LoanAmount', 'CreditScore', 'LoanTenure']
X = loan_data[features]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# -----------------------------
# 3. Apply Clustering
# -----------------------------
# DBSCAN
dbscan = DBSCAN(eps=1.4, min_samples=2)
loan_data['DBSCAN_Cluster'] = dbscan.fit_predict(X_scaled)

# Spectral Clustering
spectral = SpectralClustering(n_clusters=3, affinity='nearest_neighbors', random_state=42)
loan_data['Spectral_Cluster'] = spectral.fit_predict(X_scaled)

# -----------------------------
# 4. PCA for Visualization
# -----------------------------
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# -----------------------------
# 5. Plot Clusters
# -----------------------------
[Link](figsize=(12, 5))

# DBSCAN
[Link](1, 2, 1)
[Link](x=X_pca[:, 0], y=X_pca[:, 1], hue=loan_data['DBSCAN_Cluster'], palette='Set1',
s=100)
[Link]("DBSCAN Clustering")
[Link]("PCA 1"); [Link]("PCA 2")

# Spectral
[Link](1, 2, 2)
[Link](x=X_pca[:, 0], y=X_pca[:, 1], hue=loan_data['Spectral_Cluster'], palette='Set2',
s=100)
[Link]("Spectral Clustering")
[Link]("PCA 1"); [Link]("PCA 2")

plt.tight_layout()
[Link]()

# -----------------------------
# 6. Explain Clusters
# -----------------------------
def explain_clusters(df, label, method):
print(f"\n🔍 {method} Cluster Summary:")
summary = [Link](label)[features].mean().round(2)
print(summary)
return summary

summary_db = explain_clusters(loan_data, 'DBSCAN_Cluster', "DBSCAN")


summary_sp = explain_clusters(loan_data, 'Spectral_Cluster', "Spectral")

# -----------------------------
# 8. Recommendation
# -----------------------------
print("\n✅ Best Clustering Recommendation:")
if score_sp > score_db:
print("👉 Spectral Clustering is better for this dataset based on Silhouette Score.")
else:
print("👉 DBSCAN is better or detected outliers (noise) effectively.")
# -----------------------------
# 9. Final Overview Table
# -----------------------------
print("\n📋 Final Loan Clustering Table:")
print(loan_data[['Applicant'] + features + ['DBSCAN_Cluster', 'Spectral_Cluster']])
Regression
10. House Price Prediction
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt

from sklearn.model_selection import train_test_split


from sklearn.linear_model import LinearRegression
from [Link] import RandomForestRegressor
from [Link] import mean_squared_error, r2_score

# ---------------------------
# 1️⃣🍽️ Generate Sample Dataset
# ---------------------------
[Link](42)
data = [Link]({
'Size(sqft)': [Link](600, 5000, 100),
'Age(yrs)': [Link](0, 30, 100),
'Bedrooms': [Link](1, 5, 100),
'Bathrooms': [Link](1, 4, 100),
'Garage': [Link](0, 2, 100),
'DistanceToCity(km)': [Link](1, 25, 100),
})
data['Price'] = (
data['Size(sqft)'] * 250 +
data['Bedrooms'] * 30000 +
data['Bathrooms'] * 20000 +
data['Garage'] * 15000 -
data['Age(yrs)'] * 2000 -
data['DistanceToCity(km)'] * 1000 +
[Link](0, 20000, 100)
)

# ---------------------------
# 2️⃣🍽️ Feature Setup & Split
# ---------------------------
X = [Link]('Price', axis=1)
y = data['Price']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

# ---------------------------
# 3️⃣🍽️ Define & Train Models
# ---------------------------
models = {
'Linear Regression': LinearRegression(),
'Random Forest': RandomForestRegressor(n_estimators=100, random_state=1)
}

results = {}
for name, model in [Link]():
[Link](X_train, y_train)
y_pred = [Link](X_test)
results[name] = {
'Model': model,
'R2': r2_score(y_test, y_pred),
'RMSE': [Link](mean_squared_error(y_test, y_pred)),
'Predictions': y_pred
}

# ---------------------------
# 4️⃣🍽️ Plot: Residuals
# ---------------------------
[Link](figsize=(14, 5))
for i, (name, res) in enumerate([Link]()):
[Link](1, 2, i+1)
residuals = y_test - res['Predictions']
[Link](residuals, bins=20, kde=True, color='salmon')
[Link](0, linestyle='--', color='black')
[Link](f'{name} Residuals')
plt.tight_layout()
[Link]()

# ---------------------------
# 5️⃣🍽️ Compare Model Metrics
# ---------------------------
metrics_df = [Link]({
model: {'R²': results[model]['R2'], 'RMSE': results[model]['RMSE']}
for model in results
}).T

print("\n📊 Model Comparison Metrics:")


print(metrics_df.round(2))

metrics_df.plot(kind='bar', figsize=(10, 5), title='Model Performance Comparison',


colormap='viridis')
[Link]("Score / Error")
[Link](rotation=0)
[Link](axis='y')
plt.tight_layout()
[Link]()

# ---------------------------
# 7️⃣🍽️ User Input Prediction
# ---------------------------
print("\n🏡 Enter Property Details to Predict Price:")
try:
size = int(input("Size in sqft (e.g., 1500): "))
age = int(input("Age of the house in years (e.g., 5): "))
beds = int(input("No. of bedrooms (e.g., 3): "))
baths = int(input("No. of bathrooms (e.g., 2): "))
garage = int(input("Garage (0 or 1): "))
distance = float(input("Distance to city center in km (e.g., 10): "))
except ValueError:
print("❌ Invalid input. Please enter numbers only.")
exit()

user_input = [Link]([[size, age, beds, baths, garage, distance]],


columns=[Link])

best_model = results['Random Forest']['Model']


predicted_price = best_model.predict(user_input)[0]

# ---------------------------
# 8️⃣🍽️ Explainable Output
# ---------------------------
print(f"\n💰 Estimated House Price: ₹{predicted_price:,.2f}")
print("🔍 Feature Impact (Top 3):")
imp_df = [Link](importances, index=[Link]).sort_values(ascending=False)
for feat, imp in imp_df.head(3).items():
print(f" - {feat}: {imp*100:.1f}% impact on price")
11. Electricity Usage

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from sklearn.model_selection import train_test_split, KFold


from sklearn.linear_model import Ridge
from [Link] import KNeighborsRegressor
from [Link] import mean_squared_error, r2_score

# -------------------------------
# 1️⃣🍽️ Generate Electricity Dataset
# -------------------------------
[Link](42)
n = 150
data = [Link]({
'Temperature': [Link](15, 40, n),
'Humidity': [Link](30, 80, n),
'WindSpeed': [Link](1, 10, n),
'ApplianceCount': [Link](3, 10, n),
'Weekend': [Link](0, 2, n),
'Holiday': [Link](0, 2, n),
})
# Target variable: Electricity Usage (kWh)
data['ElectricityUsage'] = (
20 * data['ApplianceCount'] +
1.5 * data['Temperature'] +
0.7 * data['Humidity'] -
2 * data['WindSpeed'] +
15 * data['Weekend'] +
20 * data['Holiday'] +
[Link](0, 10, n)
)

# -------------------------------
# 2️⃣🍽️ Features and Train/Test Split
# -------------------------------
X = [Link]('ElectricityUsage', axis=1)
y = data['ElectricityUsage']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

# -------------------------------
# 3️⃣🍽️ Define Models
# -------------------------------
models = {
'Ridge Regression': Ridge(alpha=1.0),
'KNN Regression': KNeighborsRegressor(n_neighbors=5)
}
results = {}

# -------------------------------
# 4️⃣🍽️ Train, Predict, Evaluate
# -------------------------------
for name, model in [Link]():
[Link](X_train, y_train)
y_pred = [Link](X_test)
results[name] = {
'model': model,
'R2': r2_score(y_test, y_pred),
'RMSE': [Link](mean_squared_error(y_test, y_pred)),
'pred': y_pred
}

# -------------------------------
# 5️⃣🍽️ Residual Plot
# -------------------------------
[Link](figsize=(14, 5))
for i, (name, res) in enumerate([Link]()):
[Link](1, 2, i + 1)
residuals = y_test - res['pred']
[Link](residuals, bins=20, kde=True, color='skyblue')
[Link](f"{name} Residuals")
[Link]("Residual Error")
plt.tight_layout()
[Link]()

# -------------------------------
# 6️⃣🍽️ Metric Comparison
# -------------------------------
metric_df = [Link]({
k: {'R²': v['R2'], 'RMSE': v['RMSE']}
for k, v in [Link]()
}).T

print("\n Model Performance Comparison:")


print(metric_df.round(3))

# Plot metrics
metric_df.plot(kind='bar', figsize=(10, 5), title="Model Comparison (R² and RMSE)")
[Link]("Score")
[Link](rotation=0)
[Link](True, axis='y')
plt.tight_layout()
[Link]()

# 8️⃣🍽️ User Input Prediction


# -------------------------------
print("\n⚡ Predict Electricity Usage for a Day")
try:
temp = float(input("Temperature (°C): "))
humid = float(input("Humidity (%): "))
wind = float(input("Wind Speed (m/s): "))
apps = int(input("Appliance Count: "))
weekend = int(input("Is Weekend? (0/1): "))
holiday = int(input("Is Holiday? (0/1): "))
except:
print("Invalid input. Please enter numbers only.")
exit()

user_input = [Link]([[temp, humid, wind, apps, weekend, holiday]],


columns=[Link])
pred_usage = models['Ridge Regression'].predict(user_input)[0]
print(f"\nEstimated Electricity Usage: {pred_usage:.2f} kWh")

print("\nSuggestion:")
if pred_usage > 200:
print("High usage. Consider reducing appliance use or improve insulation.")
elif pred_usage < 100:
print("Efficient day. Good job!")
else:
print("Moderate usage. Stay aware of weekend/holiday spikes.")
12: Health Insurance Cost Estimator
# -----------------------------
# Program 3: Health Insurance Cost Estimator
# Models: HuberRegressor, ElasticNet
# -----------------------------

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn.linear_model import HuberRegressor, ElasticNet
from sklearn.model_selection import train_test_split, KFold, cross_val_score
from [Link] import LabelEncoder, StandardScaler
from [Link] import mean_squared_error, mean_absolute_error, r2_score
import warnings
[Link]("ignore")

# ------------------------------------------
# Step 1: Create synthetic dataset
# ------------------------------------------
[Link](1)
df = [Link]({
'Age': [Link](18, 65, 100),
'BMI': [Link]([Link](30, 5, 100), 1),
'Smoker': [Link](['Yes', 'No'], 100, p=[0.2, 0.8]),
'Children': [Link](0, 5, 100),
'Exercise_per_week': [Link](0, 7, 100),
'Region': [Link](['North', 'South', 'East', 'West'], 100)
})

# Cost generation (simulate insurance pattern)


df['Cost'] = (
df['Age'] * 100 +
df['BMI'] * 300 +
df['Children'] * 500 +
df['Exercise_per_week'] * -200 +
[Link](df['Smoker'] == 'Yes', 12000, 0) +
[Link](0, 1000, 100)
)

# ------------------------------------------
# Step 2: Feature Engineering
# ------------------------------------------
le_smoker = LabelEncoder()
le_region = LabelEncoder()
df['Smoker'] = le_smoker.fit_transform(df['Smoker'])
df['Region'] = le_region.fit_transform(df['Region'])

# ------------------------------------------
# Step 3: Split the dataset
# ------------------------------------------
X = [Link]('Cost', axis=1)
y = df['Cost']
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)

# ------------------------------------------
# Step 4: Scale features
# ------------------------------------------
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_val_scaled = [Link](X_val)
X_test_scaled = [Link](X_test)

# ------------------------------------------
# Step 5: Train Models
# ------------------------------------------
model1 = HuberRegressor(max_iter=500)
model2 = ElasticNet(alpha=0.5, l1_ratio=0.6, max_iter=1000)

[Link](X_train_scaled, y_train)
[Link](X_train_scaled, y_train)

# ------------------------------------------
# Step 6: Evaluation
# ------------------------------------------
def evaluate_model(name, model, X_test, y_test):
preds = [Link](X_test)
rmse = [Link](mean_squared_error(y_test, preds))
print(f"{name} Metrics")
print(f"MAE: {mean_absolute_error(y_test, preds):.2f}")
print(f"RMSE: {rmse:.2f}")
print(f"R² Score: {r2_score(y_test, preds):.2f}")
print("-"*30)
return preds

y_pred_huber = evaluate_model("Huber Regressor", model1, X_test_scaled, y_test)


y_pred_elastic = evaluate_model("ElasticNet", model2, X_test_scaled, y_test)

# ------------------------------------------
# Step 8: K-Fold Cross Validation
# ------------------------------------------
kf = KFold(n_splits=5, shuffle=True, random_state=1)
cv1 = cross_val_score(model1, [Link](X), y, cv=kf, scoring='r2')
cv2 = cross_val_score(model2, [Link](X), y, cv=kf, scoring='r2')

print(f"Huber CV R²: {[Link]():.2f}")


print(f"ElasticNet CV R²: {[Link]():.2f}")

# ------------------------------------------
# Step 10: User Input + Prediction + Suggestion
# ------------------------------------------
def get_valid_input(prompt, min_val, max_val, dtype=int):
while True:
try:
val = dtype(input(f"{prompt} ({min_val}–{max_val}): "))
if min_val <= val <= max_val:
return val
else:
print(f" Enter between {min_val} and {max_val}.")
except:
print(" Invalid input.")

def get_valid_choice(prompt, choices):


while True:
val = input(f"{prompt} {choices}: ").strip().title()
if val in choices:
return val
else:
print(f" Choose from {choices}.")

print("\n Predict your insurance cost:")


age = get_valid_input("Age", 18, 100)
bmi = get_valid_input("BMI", 15, 45, float)
smoker = get_valid_choice("Smoker?", ['Yes', 'No'])
children = get_valid_input("Children", 0, 5)
exercise = get_valid_input("Exercise per week", 0, 7)
region = get_valid_choice("Region", ['North', 'South', 'East', 'West'])

# Encode and predict


input_data = [Link]([{
'Age': age,
'BMI': bmi,
'Smoker': le_smoker.transform([smoker])[0],
'Children': children,
'Exercise_per_week': exercise,
'Region': le_region.transform([region])[0]
}])

input_scaled = [Link](input_data)
pred1 = [Link](input_scaled)[0]
pred2 = [Link](input_scaled)[0]

print(f"\n Huber Estimate: ₹{pred1:.2f}")


print(f" ElasticNet Estimate: ₹{pred2:.2f}")

# Suggestion based on smoker + BMI


if smoker == "Yes":
print(" Tip: Quitting smoking could reduce your premium.")
if bmi > 30:
print(" Tip: Reducing BMI could lower costs.")
if exercise < 3:
print(" Tip: Increasing exercise can reduce risk.")

You might also like