-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkmeans_imputer.py
More file actions
201 lines (161 loc) · 8.07 KB
/
kmeans_imputer.py
File metadata and controls
201 lines (161 loc) · 8.07 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import numpy as np
import pandas as pd
from scipy.stats import mode
import sys
import os
from pathlib import Path
sys.path.append(str(Path(f"{__file__}").parent.parent))
from sklearn.utils.validation import check_is_fitted, check_array
from sklearn.model_selection import GridSearchCV
from kmodes.kprototypes import KPrototypes
from kmodes.kmodes import KModes
from .abstract_null_imputer import AbstractNullImputer
from ..utils.dataframe_utils import _get_mask
def get_kmeans_imputer_params_for_tuning(seed: int):
return {
"KMeansImputer": {
"kprototypes_model": KPrototypes(random_state=seed),
"kmodes_model": KModes(random_state=seed),
"params": {
"n_clusters": [2, 3, 4, 5, 6, 7, 8, 9, 10],
"max_iter": [100, 200],
"init": ["Huang", "Cao", "random"],
"n_init": [1, 5, 10],
}
# "params": {
# "n_clusters": [2, 10],
# "max_iter": [100],
# "init": ["Cao"],
# "n_init": [1],
# }
}
}
def custom_scoring_function(estimator, X, y=None):
return estimator.epoch_costs_[-1]
class KMeansImputer(AbstractNullImputer):
def __init__(self, imputer_mode: str,
seed: int, missing_values=np.nan,
n_jobs: int = -1, verbose: int = 0,
hyperparameters: dict = None):
super().__init__(seed)
self.missing_values = missing_values
self.n_jobs = n_jobs
self.verbose = verbose
self.hyperparameters = hyperparameters
self.imputer_mode = imputer_mode
if self.hyperparameters is not None:
print("Hyperparameters are provided. Grid search will not be performed.")
if imputer_mode == "kprototypes":
self.model = KPrototypes(random_state=self.seed, n_jobs=self.n_jobs, **self.hyperparameters)
elif imputer_mode == "kmodes":
self.model = KModes(random_state=self.seed, n_jobs=self.n_jobs, **self.hyperparameters)
else:
raise ValueError("Invalid imputer mode. Choose either 'kprototypes' or 'kmodes'.")
else:
print("Hyperparameters are not provided. Grid search will be performed.")
tuning_params = get_kmeans_imputer_params_for_tuning(seed)["KMeansImputer"]
self.tuning_params = tuning_params["params"]
if imputer_mode == "kprototypes":
estimator = tuning_params["kprototypes_model"]
elif imputer_mode == "kmodes":
estimator = tuning_params["kmodes_model"]
else:
raise ValueError("Invalid imputer mode. Choose either 'kprototypes' or 'kmodes'.")
self.model_grid_search = GridSearchCV(
estimator=estimator,
param_grid=self.tuning_params,
scoring=custom_scoring_function,
cv=3,
n_jobs=self.n_jobs,
verbose=self.verbose
)
def _validate_input(self, df=None):
# Check data integrity and calling arguments
force_all_finite = False if self.missing_values in ["NaN", np.nan] else True
X = check_array(df, accept_sparse=False, dtype=np.float64,
force_all_finite=force_all_finite, copy=True)
# Check for +/- inf
if np.any(np.isinf(X)):
raise ValueError("+/- inf values are not supported.")
# Check if any column has all missing
mask = _get_mask(X, self.missing_values)
if np.any(mask.sum(axis=0) >= (X.shape[0])):
raise ValueError("One or more columns have all rows missing.")
return X, mask
def fit(self, X, cat_vars, y=None):
X, mask = self._validate_input(X)
# Check cat_vars type and convert if necessary
if cat_vars is not None:
if type(cat_vars) == int:
cat_vars = [cat_vars]
elif type(cat_vars) == list or type(cat_vars) == np.ndarray:
if np.array(cat_vars).dtype != int:
raise ValueError(
"cat_vars needs to be either an int or an array "
"of ints.")
else:
raise ValueError("cat_vars needs to be either an int or an array "
"of ints.")
# Identify numerical variables
num_vars = np.setdiff1d(np.arange(X.shape[1]), cat_vars)
num_vars = num_vars if len(num_vars) > 0 else None
# Get col and row indices for missing
_, missing_cols = np.where(mask)
missing_cols = list(set(missing_cols))
observed_columns = np.setdiff1d(np.arange(X.shape[1]), missing_cols)
observed_cat_vars = np.intersect1d(observed_columns, cat_vars).tolist()
observed_num_vars = np.intersect1d(observed_columns, num_vars).tolist()
X_observed = np.hstack([X[:, observed_cat_vars], X[:, observed_num_vars]])
self.cat_vars_ = list(range(len(observed_cat_vars)))
self.num_vars_ = list(range(len(observed_cat_vars), len(observed_columns)))
self.missing_cat_columns_ = np.intersect1d(missing_cols, cat_vars).tolist()
self.missing_num_columns_ = np.intersect1d(missing_cols, num_vars).tolist()
self.observed_columns_ = observed_columns
self.missing_columns_ = missing_cols
if self.hyperparameters is None:
self.model_grid_search.fit(X_observed, categorical=self.cat_vars_)
self.model = self.model_grid_search.best_estimator_
self.best_params_ = self.model_grid_search.best_params_
else:
self.model.fit(X_observed, categorical=self.cat_vars_)
pred_clusters = self.model.predict(X_observed, categorical=self.cat_vars_)
self._calculate_cluster_stats(X, pred_clusters)
# save percentage of clusters
self.cluster_percentages_ = {str(cluster): len(np.where(pred_clusters == cluster)[0]) / len(pred_clusters) for cluster in set(pred_clusters)}
print(f"Cluster percentages: {self.cluster_percentages_}")
return self
def _calculate_cluster_stats(self, X, clusters):
self.cluster_statistics_ = {}
for cluster in set(clusters):
cluster_indices = np.where(clusters == cluster)[0]
for col in self.missing_columns_:
if col in self.missing_cat_columns_:
self.cluster_statistics_[(cluster, col)] = mode(X[cluster_indices, col], axis=0, nan_policy='omit')[0]
else:
self.cluster_statistics_[(cluster, col)] = np.nanmean(X[cluster_indices, col])
return self
def transform(self, X, y=None):
# Confirm whether fit() has been called
check_is_fitted(self, ["cat_vars_", "num_vars_"])
X, mask = self._validate_input(X)
missing_rows, _ = np.where(mask)
X_observed = X[:, self.observed_columns_]
clusters = self.model.predict(X_observed, categorical=self.cat_vars_)
for cluster in set(clusters):
cluster_indices = np.where(clusters == cluster)[0]
missing_in_cluster_indices = np.intersect1d(cluster_indices, missing_rows)
for col in self.missing_columns_:
if col in self.missing_cat_columns_:
# calucate mode discarding nan and assign to missing values
X[missing_in_cluster_indices, col] = self.cluster_statistics_[(cluster, col)]
else:
X[missing_in_cluster_indices, col] = self.cluster_statistics_[(cluster, col)]
return X
def fit_transform(self, df, target_column: str = None, **fit_params):
return self.fit(df, **fit_params).transform(df)
def get_predictors_params(self):
if self.hyperparameters is None:
output = self.best_params_
else:
output = self.hyperparameters
return output