This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathoperations.py
More file actions
288 lines (236 loc) · 7.26 KB
/
operations.py
File metadata and controls
288 lines (236 loc) · 7.26 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import numpy as np
from sklearn.decomposition import PCA, TruncatedSVD
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.feature_selection import f_classif, SelectKBest, SelectPercentile
from dffml.df.base import op
from .definitions import (
n_iter,
strategy,
input_data,
categories,
output_data,
random_state,
n_components,
missing_values,
target_data,
k,
percentile,
score_func
)
@op(
inputs={"data": input_data, "n_components": n_components},
outputs={"result": output_data},
)
async def principal_component_analysis(
data, n_components=None,
):
"""
Decomposes the data into (n_samples, n_components)
using PCA method
Parameters
----------
data : List[List[int]]
data to be decomposed.
n_components : int
number of colums the data should have after decomposition.
Returns
-------
result: Data having dimensions (n_samples, n_components)
"""
pca = PCA(n_components=n_components)
new_data = pca.fit_transform(data)
return {"result": new_data}
@op(
inputs={
"data": input_data,
"n_components": n_components,
"n_iter": n_iter,
"random_state": random_state,
},
outputs={"result": output_data},
)
async def singular_value_decomposition(
data, n_components=2, n_iter=5, random_state=None,
):
"""
Decomposes the data into (n_samples, n_components)
using SVD method.
Parameters
----------
data : List[List[int]]
data to be decomposed.
n_components : int
number of colums the data should have after decomposition.
Returns
-------
result: Data having dimensions (n_samples, n_components)
"""
svd = TruncatedSVD(
n_components=n_components, n_iter=n_iter, random_state=random_state
)
new_data = svd.fit_transform(data)
return {"result": new_data}
@op(
inputs={
"data": input_data,
"missing_values": missing_values,
"strategy": strategy,
},
outputs={"result": output_data},
)
async def simple_imputer(data, missing_values=np.nan, strategy="mean"):
"""
Imputation method for missing values
Parameters
----------
data : List[List[int]]
data in which missing values are present
missing_values : Any str, int, float, None default = np.nan
The value present in place of missing value
strategy : str "mean", "median", "constant", "most_frequent" default = "mean"
The value present in place of missing value
Returns
-------
result: Dataset having missing values imputed with the strategy
"""
if missing_values not in (int, float, str, None, np.nan):
raise Exception(
f"Missing values should be one of: str, float, int, None, np.nan got {missing_values}"
)
if strategy not in ("mean", "median", "constant", "most_frequent"):
raise Exception(
f"Strategy should be one of mean, median, constant, most_frequent got {strategy}"
)
imp = SimpleImputer(missing_values=missing_values, strategy=strategy)
new_data = imp.fit_transform(data)
return {"result": new_data}
@op(
inputs={"data": input_data, "categories": categories},
outputs={"result": output_data},
)
async def one_hot_encoder(data, categories):
"""
One hot encoding for categorical data columns
Parameters
----------
data : List[List[int]]
data to be encoded.
categories : List[List[str]]
Categorical values which needs to be encoded
Returns
-------
result: Encoded data for categorical values
"""
enc = OneHotEncoder(categories=categories)
enc.fit(data)
new_data = enc.transform(data).toarray()
return {"result": new_data}
@op(inputs={"data": input_data}, outputs={"result": output_data})
async def standard_scaler(data):
"""
Standardize features by removing the mean and
scaling to unit variance.
Parameters
----------
data: List[List[int]]
data that needs to be standardized
Returns
-------
result: Standardized data
"""
scaler = StandardScaler()
new_data = scaler.fit_transform(data)
return {"result": new_data.tolist()}
@op(
inputs={"data": input_data}, outputs={"result": output_data},
)
async def remove_whitespaces(data):
"""
Removes white-spaces from the dataset
Parameters
----------
data : List[List[int]]
dataset.
Returns
-------
result: dataset having whitespaces removed
"""
new_data = np.char.strip(data)
return {"result": new_data}
@op(
inputs={"data": input_data}, outputs={"result": output_data},
)
async def ordinal_encoder(data):
"""
One hot encoding for categorical data columns
Parameters
----------
data : List[List[int]]
data to be encoded.
categories : List[List[str]]
Categorical values which needs to be encoded
Returns
-------
result: Encoded data for categorical values
References:
- https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectKBest.html
"""
enc = OneHotEncoder()
enc.fit(data)
new_data = enc.transform(data).toarray()
return {"result": new_data}
@op(
inputs={"data": input_data, "target_data": target_data, "k": k, "score_func": score_func},
outputs={"result": output_data}
)
async def select_k_best(data, target_data, score_func=f_classif, k=10):
"""
Select the top k features, based on the score function.
Parameters
----------
data : List[List[int]]
Input data, excluding the target column
target_data : List[int]
1D list containing values for the target column.
score_func : function
Function that takes in data and target_data, and returns
a pair of arrays (scores, pvalues) or a single array with
scores.
k : int
Number of top features to select.
Returns
-------
result: Encoded data for categorical values
"""
selector = SelectKBest(score_func, k=k)
new_data = selector.fit_transform(data, target_data)
return {"result": new_data}
@op(
inputs={"data": input_data, "target_data": target_data, "percentile": percentile, "score_func": score_func},
outputs={"result": output_data}
)
async def select_percentile(data, target_data, score_func=f_classif, percentile=10):
"""
Select a certain top percentile of features, based on the score function.
Parameters
----------
data : List[List[int]]
Input data, excluding the target column
target_data : List[int]
1D list containing values for the target column.
score_func : function
Function that takes in data and target_data, and returns
a pair of arrays (scores, pvalues) or a single array with
scores.
percentile : int
Percentile of top features to select.
Returns
-------
result: Encoded data for categorical values
References:
- https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectPercentile.html
"""
selector = SelectPercentile(score_func, percentile=percentile)
new_data = selector.fit_transform(data, target_data)
return {"result": new_data}