Skip to content

Instantly share code, notes, and snippets.

@joshreini1
Created October 25, 2022 01:10
Show Gist options
  • Save joshreini1/813faacd339426c3dba726a16a9a820b to your computer and use it in GitHub Desktop.
Save joshreini1/813faacd339426c3dba726a16a9a820b to your computer and use it in GitHub Desktop.
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])
X['location_gridnum'] = X['latitude'].astype(str) + str('_') + X['longitude'].astype(str)
Xy = X.join(y)
location_means = Xy.groupby('location_gridnum')['price'].mean()
Xy['location_gridnum'] = Xy['latitude'].map(location_means)
Xy['location_gridnum] = 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