-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformers.py
293 lines (239 loc) · 11.2 KB
/
transformers.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
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
289
290
291
292
293
# -*- coding: utf-8 -*-
"""
Those modules contain mainly Pandas transformers. Get dataframe as input and returns dataframe
Pandas transformers classes:
PandasTransformer - General purpose transformer. Get as an input a regular sklearn transformer and returns
a dataframe.
P_StandardScaler - StandardScaler that returns a dataframe.
P_MaxAbsScaler - MaxAbsScaler that returns a dataframe.
P_MinMaxScaler - MinMaxScaler that returns a dataframe.
P_SimpleImputer - SimpleImputer that returns a dataframe.
P_SelectKBest - Returns a dataframe + can deal with negative number if chosen
P_LabelEncoder - Label encoder to all relevant columns (that are not numeric and not in the not transformed list)
Other transformers:
BinaryDownSizeTransformer - Use when a data downsize is needed.
"""
import pandas as pd
from sklearn.preprocessing import MaxAbsScaler, MinMaxScaler, StandardScaler
from sklearn.impute import SimpleImputer
# noinspection PyProtectedMember
from pandas.api.types import is_numeric_dtype
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.feature_selection import SelectKBest, chi2, f_classif, mutual_info_classif, f_regression
from sklearn.preprocessing import LabelEncoder
from collections import defaultdict
class PandasTransformer(BaseEstimator, TransformerMixin):
def __init__(self, TransFormerObject, columns=None):
"""
Transformer that gets a DataFrame as an input and use sklearn transformer on a specific columns
:param TransFormerObject: A sklearn transformer sent by the user with the desired parameters
:param columns: list. List of columns names to apply the transformation on.
If empty it will work on all numeric columns
:return: DataFrame, with the transformed values in the wanted columns.
### Original code written by Dror geva and general purposes by Gal merom ###
"""
if columns is None:
columns = []
self.columns = columns
self.Transformer_model = TransFormerObject
def fit(self, X, y=None):
# In case columns were not defined then find all the numeric columns in the dataframe
if len(self.columns) == 0:
for col in X.columns:
if is_numeric_dtype(X[col].dtype):
self.columns.append(col)
self.Transformer_model.fit(X[self.columns])
return self
def transform(self, X):
X_new = X.copy()
scaled_cols = self.Transformer_model.transform(X_new[self.columns])
X_new.loc[:, self.columns] = scaled_cols
return X_new
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y=None)
return self.transform(X)
class P_StandardScaler(PandasTransformer):
def __init__(self, columns=None):
"""
Like a StandardScaler. returns a dataframe (not numpy)
columns - list, list of columns names to apply the transformation on.
If empty it will work on all numeric columns
:return: DataFrame, with the transformed values in the wanted columns.
"""
if columns is None:
columns = []
self.columns = columns
self.Transformer_model = StandardScaler()
class P_MaxAbsScaler(PandasTransformer):
def __init__(self, columns=None):
"""
Like a MaxAbsScaler. returns a dataframe (not numpy)
columns - list, list of columns names to apply the transformation on.
If empty it will work on all numeric columns
:return: DataFrame, with the transformed values in the wanted columns.
"""
if columns is None:
columns = []
self.columns = columns
self.Transformer_model = MaxAbsScaler()
class P_MinMaxScaler(PandasTransformer):
def __init__(self, columns=None):
"""
Like a MinMaxScaler, but it returns a dataframe
columns - list.list of columns names to apply the transformation on.If empty it will work on all numeric columns
:return: DataFrame, with the transformed values in the wanted columns.
"""
if columns is None:
columns = []
self.columns = columns
self.Transformer_model = MinMaxScaler()
class P_SimpleImputer(PandasTransformer):
def __init__(self, columns=None, **kwargs):
"""
Like a SimpleImputer, but it returns a dataframe
columns - list, list of columns names to apply the transformation on.If empty it will work on all numeric columns
:return: DataFrame, with the transformed values in the wanted columns.
"""
if columns is None:
columns = []
self.columns = columns
self.Transformer_model = SimpleImputer(**kwargs)
class P_SelectKBest(BaseEstimator, TransformerMixin):
def __init__(self, score_func=f_classif, k=10, DealWithNegValues=0):
"""
Like a SelectKBest, but it returns a dataframe
score_func - callable, default = f_classif
Function taking two arrays X and y, and returning a pair of arrays (scores, pvalues) or a single
array with scores.
k - int or “all”, default=10
Number of top features to select. The “all” option bypasses selection, for use in a parameter search.
DealWithNegValues - What to do with negative values:
0 - Don't do anything
1 - Use MinMaxScaler
2 - Add minimum value for each column
:return: DataFrame, with the selected columns.
"""
self.score_func = score_func
self.k = k
self.Transformer_model = SelectKBest(score_func=self.score_func, k=self.k)
self.NegValueProcess = DealWithNegValues
def fit(self, X, y=None, **kwargs):
X_fit_new = X.copy()
if self.NegValueProcess == 1:
MinMax = P_MinMaxScaler()
X_fit_new = MinMax.fit_transform(X_fit_new, y)
elif self.NegValueProcess == 2:
for col in X_fit_new.columns:
if X_fit_new[col].min() < 0:
X_fit_new[col] = X_fit_new[col] + abs(X_fit_new[col].min())
# run the following in all cases
self.Transformer_model.fit(X_fit_new, y, **kwargs)
return self
def transform(self, X):
X_new = X.copy()
X_np = self.Transformer_model.transform(X_new)
# The results are np array we change them back to dataframe
mask = self.Transformer_model.get_support() # list of booleans
new_features = [] # The list of K best features
# noinspection PyTypeChecker
for Flag, feature in zip(mask, X_new.columns):
if Flag:
new_features.append(feature)
X_newDF = pd.DataFrame(X_np, columns=new_features, index=X_new.index)
return X_newDF
def fit_transform(self, X, y=None, **kwargs):
self.fit(X, y=y, **kwargs)
return self.transform(X)
class P_LabelEncoder:
"""
A dataframe LabelEncoder can get columns that shouldn't be transformed
"""
def __init__(self, ListOfColNotToTransformed=None):
"""
:param ListOfColNotToTouch: List of columns headers that should not be transformed
"""
self.ColLabelsDict = defaultdict(LabelEncoder)
self.TransformedCol = {}
if ListOfColNotToTransformed is None:
self.ColNotToTouch = []
else:
self.ColNotToTouch = ListOfColNotToTransformed
def fit_transform(self, Dataframe):
df = Dataframe.copy()
# Encoding the variable
Output = df.apply(lambda x: self.ColConvertor(x, ActionType='fit_transform'))
return Output
def transform(self, Dataframe):
df = Dataframe.copy()
Output = df.apply(lambda x: self.ColConvertor(x, ActionType='transform'))
return Output
def inverse_transform(self, Dataframe):
df = Dataframe.copy()
# Inverse the encoded
Output = df.apply(lambda x: self.ColConvertor(x, ActionType='inverse_transform'))
return Output
def ColConvertor(self, Col, ActionType):
if Col.name in self.ColNotToTouch:
return Col
colName = Col.name
NumCol = is_numeric_dtype(Col.dtype)
if NumCol and not ActionType == 'inverse_transform':
self.TransformedCol[colName] = False
return Col
else:
self.TransformedCol[colName] = True
if ActionType == 'fit_transform':
return self.ColLabelsDict[colName].fit_transform(Col)
elif ActionType == 'transform':
return self.ColLabelsDict[colName].transform(Col)
elif ActionType == 'inverse_transform':
if self.TransformedCol[colName]:
return self.ColLabelsDict[colName].inverse_transform(Col)
else:
return Col
class BinaryDownSizeTransformer(BaseEstimator, TransformerMixin):
"""
This transformer reduce a dataframe, so we will get a predefined proportional
between the "positive value" that we seek and the rest.
For example: if we have only 10% people that have "yes" in the BUY column, and we want
the dataframe to contain 25% people who bought then the positiveLabel will be "yes"
and the TargetCol will be BUY the proportion will be 0.25
PropDesiredOfPos = The proportional of the positive value (number between 0 and 1)
positiveLabel = What value is considered "positive"
TargetCol = What is the column to search for the positiveLabel
Direction = if value is 1 then the records of the non-positive will be removed from the end of
the dataframe records toward the beginning (meaning the last records).
If the value 2 is given then use random.
if value is 0 : Remove the first records (good for time series)
"""
def __init__(self, PropDesiredOfPos, positiveLabel, TargetCol, Direction=2):
self.PosLabel = positiveLabel
self.TargetCol = TargetCol
self.Prop = PropDesiredOfPos
self.Direction = Direction
def fit(self, X, y=None):
return self
def transform(self, X):
X_new = X.copy()
posDf = X_new[X_new[self.TargetCol] == self.PosLabel]
OtherDf = X_new[X_new[self.TargetCol] != self.PosLabel]
numOfPos = len(posDf)
numOfOther = len(OtherDf)
FutureNumOfOther = int((numOfPos / self.Prop) - numOfPos)
if FutureNumOfOther > numOfOther:
print('There are ' + str(numOfPos) +
' with the label asked. And there are only ' +
str(numOfOther) + ' with other labels.\n Use must enter different proportion.')
return
else:
if self.Direction == 1:
ReducedOtherDf = OtherDf.iloc[range(FutureNumOfOther, 0, -1), :]
elif self.Direction == 2:
tmp = OtherDf.sample(frac=1)
ReducedOtherDf = tmp.iloc[range(0, FutureNumOfOther), :]
else:
ReducedOtherDf = OtherDf.iloc[range(0, FutureNumOfOther), :]
AllData = pd.concat([posDf, ReducedOtherDf])
if self.Direction == 2:
AllData = AllData.sample(frac=1)
return AllData