-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
31 lines (22 loc) · 1.18 KB
/
utils.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
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.base import BaseEstimator, TransformerMixin
import pandas as pd
# PEP8 padrão CamelCase
class Transformador(BaseEstimator, TransformerMixin):
def __init__(self, colunas_quantitativas, colunas_categoricas):
self.colunas_quantitativas = colunas_quantitativas
self.colunas_categoricas = colunas_categoricas_binarias + colunas_categoricas_nao_binarias
self.enc = OneHotEncoder()
self.scaler = MinMaxScaler()
def fit(self, X, y = None ):
self.enc.fit(X[self.colunas_categoricas])
self.scaler.fit(X[self.colunas_quantitativas])
return self
def transform(self, X, y = None):
X_categoricas = pd.DataFrame(data=self.enc.transform(X[self.colunas_categoricas]).toarray(),
columns= self.enc.get_feature_names(self.colunas_categoricas))
X_quantitativas = pd.DataFrame(data=self.scaler.transform(X[self.colunas_quantitativas]),
columns= self.colunas_quantitativas)
X = pd.concat([X_quantitativas, X_categoricas], axis=1)
return X