-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEX_ModelSelection_Classification.py
More file actions
130 lines (105 loc) · 4.52 KB
/
Copy pathEX_ModelSelection_Classification.py
File metadata and controls
130 lines (105 loc) · 4.52 KB
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# coding: utf-8
# ============================================================
# Executive Summary: Model Selection (Classification)
# ============================================================
# Purpose:
# This script demonstrates how to perform model selection for classification
# using multiple algorithms from scikit-learn. We compare models such as
# Logistic Regression, Decision Trees, Random Forests, Gradient Boosting,
# KNN, Naive Bayes, and Support Vector Machines on the Iris dataset.
# Why it matters:
# Recruiters and collaborators can see not only the code but also the
# reasoning behind each step. This makes the script a professional,
# tutorial-style report suitable for GitHub or LinkedIn.
# Dataset:
# Iris dataset — contains measurements of iris flowers (features) and species labels (targets).
# Workflow:
# 1. Illustrate basic Python logic with lists and tuples
# 2. Load and preprocess the Iris dataset
# 3. Train/test split
# 4. Fit multiple classification models
# 5. Evaluate performance
# 6. Summarize results
# ============================================================
# ==== Step 1: Illustrate Basic Python Logic ====
# Example with a list
X = [10, 20, 30, 40, 65, 78]
print("List values:")
for i in X:
print(i)
# Example with a tuple
Tup = [('dhiraj', 36), ('Sangram', 25), ('Ashok', 19)]
print("\nTuple values:")
for Name, Age in Tup:
print("Name:", Name, "| Age:", Age)
# ==== Step 2: Load Libraries ====
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid') # clean, professional plot style
# ==== Step 3: Load Dataset ====
# The Iris dataset is a classic dataset for classification.
from sklearn.datasets import load_iris
Iris = load_iris()
print("\nIris dataset loaded successfully.")
# ==== Step 4: Transform Dataset ====
# Features (X) = measurements of iris flowers
# Target (Y) = species labels
X = Iris.data
Y = Iris.target
print("\nFeature matrix shape:", X.shape)
print("Target vector shape:", Y.shape)
# ==== Step 5: Import Classification Models ====
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
# ==== Step 6: Train/Test Split ====
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.2, random_state=0
)
print("\nTrain/Test split completed.")
# ==== Step 7: Prepare Models ====
# We create a list of models to test. Each model represents a different
# approach to classification, from linear to ensemble methods.
models = [
('LR', LogisticRegression()), # Logistic Regression
('DT', DecisionTreeClassifier()), # Decision Tree
('RT', RandomForestClassifier(n_estimators=10)), # Random Forest
('GB', GradientBoostingClassifier(n_estimators=10)), # Gradient Boosting
('KNN', KNeighborsClassifier(n_neighbors=10)), # KNN
('GN', GaussianNB()), # Naive Bayes
('SV', SVC(kernel='linear')) # Support Vector Machine
]
print("\nModels prepared for evaluation.")
# ==== Step 8: Fit and Evaluate Models ====
# Loop through each model, fit it to the training data, and evaluate
# performance on the test set. Scores represent accuracy.
print("\nModel Evaluation Results:")
for Name, Cls in models:
model = Cls
model.fit(X_train, y_train) # train model
score = model.score(X_test, y_test) # evaluate model
print(Name, ':', score)
# ==== Final Summary ====
# ------------------------------------------------------------
# Model Selection Results:
# - Multiple classification algorithms were tested on the Iris dataset.
# - Each model provides a different perspective:
# * Logistic Regression: simple, interpretable baseline.
# * Decision Tree: captures non-linear relationships.
# * Random Forest & Gradient Boosting: ensemble methods, strong performance.
# * KNN: similarity-based classification.
# * Naive Bayes: probabilistic, fast baseline.
# * SVM: effective for linear separation.
#
# Key Takeaway:
# This script demonstrates how to systematically compare classification models.
# The recruiter or collaborator can see both the technical workflow and
# the reasoning behind each choice, making it a polished, professional report.
# ------------------------------------------------------------