-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
executable file
·248 lines (204 loc) · 8.26 KB
/
pipeline.py
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
File defining pipeline for our data preprocessing
algorithm.
"""
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder, FunctionTransformer
from sklearn.compose import ColumnTransformer
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
import numpy as np
import pandas as pd
from sklearn.feature_selection import VarianceThreshold, SelectFromModel
from sklearn.linear_model import LassoCV
from sklearn.neural_network import MLPRegressor
from dataset_info import get_dataset_info
from typing import Literal
class FeatureExtraction(BaseEstimator, TransformerMixin):
"""
Class used for feature extraction with various algorithms.
Parameters
----------
n_components : float | int
Number of components to keep, if it is a positive integer, or variance to be
kept if it is a float between 0 and 1.
method : Literal["PCA", "LDA"]
Feature extraction algorithm that should be employed. The following ones
are supported:
- PCA (Principal Component Analysis),
- LDA (Linear Discriminant Analysis).
"""
def __init__(self, n_components: float | int, method: Literal["PCA", "LDA"]) -> None:
self.set_params(n_components, method)
def set_params(self, n_components: float | int, method: Literal["PCA", "LDA"]) -> None:
"""
Sets the parameters for the feature extraction algorithm.
Parameters
----------
n_components : float | int
Number of components to keep, if it is a positive integer, or variance to be
kept if it is a float between 0 and 1.
method : Literal["PCA", "LDA"]
Feature extraction algorithm that should be employed. The following ones
are supported:
- PCA (Principal Component Analysis),
- LDA (Linear Discriminant Analysis).
"""
assert (isinstance(n_components, float) and 0 < n_components < 1) or \
(isinstance(n_components, int) and n_components > 0), \
"Parameter 'n_components' must be a float between 0 and 1 or a positive integer."
self.method = method
self.n_components = n_components
self.y = False # if y is used by feature extraction algorithm
if method == "PCA":
self.extractor = PCA(n_components=n_components)
elif method == "LDA":
self.extractor = LDA(n_components=n_components)
self.y = True
else:
raise ValueError(f"Unsupported feature extraction method: '{method}'.")
def fit(self, X, y=None) -> "FeatureExtraction":
"""
Fits the feature extraction algorithm to the data.
Parameters
----------
X
The input features for training.
y
The target values corresponding to the training samples in X.
Returns
-------
`self` (an instance of `FeatureExtraction` class fit to the data).
"""
if self.y:
self.extractor.fit(X, y)
else:
self.extractor.fit(X)
return self
def transform(self, X, y=None) -> np.ndarray:
"""
Transforms the data by performing feature extraction.
Parameters
----------
X
The input features.
y
The target values corresponding to the training samples in X.
Returns
-------
np.ndarray
An array of transformed features.
"""
if self.y:
return self.extractor.transform(X)
return self.extractor.transform(X)
def get_full_pipeline(
df: pd.DataFrame,
target: str,
variance_treshold: float = 0.01,
feature_selection_treshold: float = 200.0,
feature_extraction_method: Literal["PCA", "LDA"] = "PCA",
n_components: int | float = 30,
verbose: bool = False
) -> Pipeline:
"""
Creates a pipeline with selected parameters.
Parameters
----------
df : pd.DataFrame
Dataset on which the preprocessing shall be performed, in the form of
a pandas dataframe.
target : str
Name of the target column (for which the machine learning algorithm
should predict the value).
variance_treshold : float
Treshold such that features with variance below it will be removed.
feature_selection_treshold : float
Treshold used for feature selection.
feature_extraction_method : Literal["PCA", "LDA"]
Feature extraction algorithm that should be employed. The following ones
are supported:
- PCA (Principal Component Analysis),
- LDA (Linear Discriminant Analysis).
n_components : float | int
Number of components to keep, if it is a positive integer, or variance to be
kept if it is a float between 0 and 1.
verbose : bool
If set to true, the pipeline will print information about the progress
of data preprocessing when used.
Returns
-------
Pipeline with provided parameters.
"""
num_cols, cat_cols = get_dataset_info(df, target)
# Pipeline to be applied to numeric columns.
num_pipeline = Pipeline([
('Variance_threshold', VarianceThreshold(threshold=variance_treshold)),
('std_scaler', StandardScaler())
])
# Pipeline to be applied to categorical columns.
cat_pipeline = Pipeline([
('ohe', OneHotEncoder(handle_unknown='ignore')),
('Variance_threshold', VarianceThreshold(threshold=variance_treshold)),
])
col_transform = ColumnTransformer([
('num', num_pipeline, num_cols),
('cat', cat_pipeline, cat_cols)
])
full_pipeline = Pipeline([
('transform', col_transform),
('dense', FunctionTransformer(lambda x: np.array(x.todense()),
accept_sparse=True)),
('select_from_model', SelectFromModel(LassoCV(), threshold=feature_selection_treshold)),
('feature_extraction', FeatureExtraction(n_components, feature_extraction_method)),
],
verbose=verbose,
)
return full_pipeline
def get_full_pipeline_with_model(
df: pd.DataFrame,
target: str,
variance_treshold: float = 0.01,
feature_selection_treshold: float = 200.0,
feature_extraction_method: Literal["PCA", "LDA"] = "PCA",
n_components: int | float = 30,
verbose: bool = False
) -> Pipeline:
"""
Creates a pipeline with selected parameters and a machine learning algorithm attached to it.
Parameters
----------
df : pd.DataFrame
Dataset on which the preprocessing shall be performed, in the form of
a pandas dataframe.
target : str
Name of the target column (for which the machine learning algorithm
should predict the value).
variance_treshold : float
Treshold such that features with variance below it will be removed.
feature_selection_treshold : float
Treshold used for feature selection.
feature_extraction_method : Literal["PCA", "LDA"]
Feature extraction algorithm that should be employed. The following ones
are supported:
- PCA (Principal Component Analysis),
- LDA (Linear Discriminant Analysis).
n_components : float | int
Number of components to keep, if it is a positive integer, or variance to be
kept if it is a float between 0 and 1.
verbose : bool
If set to true, the pipeline will print information about the progress
of data preprocessing when used.
Returns
-------
Pipeline with provided parameters and machine learning model.
"""
full_pipeline = get_full_pipeline(df, target, variance_treshold, feature_selection_treshold,
feature_extraction_method, n_components, verbose)
full_pipeline_with_model = Pipeline([
('full_pipeline', full_pipeline),
('model', MLPRegressor(hidden_layer_sizes=(50), batch_size = 8,
learning_rate_init = 0.1, verbose = True, max_iter=30))
])
return full_pipeline_with_model