Skip to content

Instantly share code, notes, and snippets.

@joshreini1
Created October 24, 2022 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshreini1/c2838e2ba592e88797afe000cf70ab8f to your computer and use it in GitHub Desktop.
Save joshreini1/c2838e2ba592e88797afe000cf70ab8f 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])
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