-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn_pca_model.py
More file actions
142 lines (121 loc) · 4.12 KB
/
knn_pca_model.py
File metadata and controls
142 lines (121 loc) · 4.12 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
131
132
133
134
135
136
137
138
139
140
141
142
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.decomposition import PCA
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix, ConfusionMatrixDisplay
from sklearn.feature_selection import SelectKBest, mutual_info_classif, f_classif
import matplotlib.pyplot as plt
# ----------------------
# BUILD PIPELINE
# ----------------------
def build_knn_pipeline(selection):
"""Return pipeline and parameter grid based on selection type."""
base_steps = [("scaler", StandardScaler())]
param_grid = {}
if selection == "selectk":
base_steps.append(("select", SelectKBest(mutual_info_classif)))
param_grid = {
"select__k": [1, 5, 10, 20, 40, 60, 80, 100],
"knn__n_neighbors": range(1, 41, 2),
}
elif selection == "default":
param_grid = {
"knn__n_neighbors": range(1, 41, 2)
}
# Append KNN classifier at the end
base_steps.append(("knn", KNeighborsClassifier()))
return Pipeline(base_steps), param_grid
# ======================================================
# TRAIN AND EVALUATE KNN MODEL
# ======================================================
def train_knn(
nba: pd.DataFrame,
selection: str,
test_size: float,
tier_count: int,
use_grid_search: bool,
manual_params: dict = None
):
# Create quantile-based salary tiers (target variable)
nba["salary_tier"], bins = pd.qcut(
nba["SALARY"],
q=tier_count,
labels=range(tier_count),
retbins=True
)
# Columns not usable with KNN (categorical / strings)
drop_cols = [
"PLAYER_ID", "PLAYER_NAME", "NICKNAME_base",
"TEAM_ID_base", "TEAM_ABBREVIATION_base",
"NICKNAME_adv", "TEAM_ID_adv", "TEAM_ABBREVIATION_adv"
]
default_features = [
'MIN_base', 'FGM_base', 'FG3M', 'FTM',
'FG_PCT_base', 'FG3_PCT', 'FT_PCT', 'REB', 'AST',
'TOV', 'STL', 'BLK', 'PF', 'PTS', 'PLUS_MINUS', 'TD3'
]
# Feature matrix X
if selection == "default":
X = nba[default_features]
else:
# Use all numeric features except dropped ones
X = nba.drop(columns=drop_cols + ["SALARY", "salary_tier"])
# Target Variable
y = nba["salary_tier"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42, stratify=y
)
# Build pipeline and parameter grid
pipeline, param_grid = build_knn_pipeline(selection)
# Option 1: GridSearchCV
if use_grid_search:
grid = GridSearchCV(
estimator=pipeline,
param_grid=param_grid,
cv=5,
scoring="balanced_accuracy",
n_jobs=-1
)
grid.fit(X_train, y_train)
model = grid.best_estimator_
parameters = grid.best_params_
# Option 2: Manual user parameters
else:
if manual_params:
pipeline.set_params(**manual_params)
parameters = manual_params
else:
parameters = {}
model = pipeline.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
cr = classification_report(y_test, y_pred, output_dict=True)
rounded_cr = {}
for key, value in cr.items():
if isinstance(value, dict):
# Round inner metrics
rounded_cr[key] = {
m: round(v, 3) if isinstance(v, float) else v
for m, v in value.items()
}
else:
# Accuracy (a float)
rounded_cr[key] = round(value, 3)
# Metrics
metrics = {
"accuracy": round(accuracy_score(y_test, y_pred), 3),
"classification_report": rounded_cr,
}
cm = confusion_matrix(y_test, y_pred)
return {
"model": model,
"parameters": parameters,
"metrics": metrics,
"confusion_matrix": cm,
"bins": bins,
}