Created
October 24, 2022 20:21
-
-
Save joshreini1/c2838e2ba592e88797afe000cf70ab8f to your computer and use it in GitHub Desktop.
This file contains 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
class target_encoder(BaseEstimator, TransformerMixin): | |
def __init__(self): | |
pass | |
def fit(self, X, y = None): | |
return self | |
def transform(self, X, y = None): | |
#target encode lat and long | |
#drop target | |
qt = QuantileTransformer() | |
X[['latitude','longitude']] = qt.fit_transform(X[['latitude','longitude']]) | |
X['latitude'] = pd.cut(X['latitude'], bins = [0,0.2,0.4,0.6,0.8,1], labels = [1,2,3,4,5]) | |
X['longitude'] = pd.cut(X['longitude'], bins = [0,0.2,0.4,0.6,0.8,1], labels = [1,2,3,4,5]) | |
Xy = X.join(y) | |
latitude_means = Xy.groupby('latitude')['price'].mean() | |
Xy['latitude'] = Xy['latitude'].map(latitude_means) | |
longitude_means = Xy.groupby('longitude')['price'].mean() | |
Xy['longitude'] = Xy['longitude'].map(longitude_means) | |
Xy[['latitude','longitude']] = Xy[['latitude','longitude']].astype(float) | |
X = Xy.drop('price', axis = 1) | |
return X |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment