Created
October 15, 2019 06:52
-
-
Save MariaLavrovskaya/0920bcb017f7eeeead0aa691a60aa4d7 to your computer and use it in GitHub Desktop.
airbnb_16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import StandardScaler | |
from sklearn import preprocessing | |
from sklearn.preprocessing import LabelEncoder | |
le = preprocessing.LabelEncoder() | |
# LabelEncoder for a number of columns | |
class MultiColumnLabelEncoder: | |
def __init__(self, columns = None): | |
self.columns = columns # list of column to encode | |
def fit(self, X, y=None): | |
return self | |
def transform(self, X): | |
''' | |
Transforms columns of X specified in self.columns using | |
LabelEncoder(). If no columns specified, transforms all | |
columns in X. | |
''' | |
output = X.copy() | |
if self.columns is not None: | |
for col in self.columns: | |
output[col] = LabelEncoder().fit_transform(output[col]) | |
else: | |
for colname, col in output.iteritems(): | |
output[colname] = LabelEncoder().fit_transform(col) | |
return output | |
def fit_transform(self, X, y=None): | |
return self.fit(X, y).transform(X) | |
le = MultiColumnLabelEncoder() | |
X_train_le = le.fit_transform(X) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment