Reputation: 243
I am working with the California Housing data set in scikit-learn
. I want to engineer two binary features: "within 10 km of San Francisco" and "within 10 km of Los Angeles". I created a custom transformer that works fine on its own, but throws a TypeError
when I put it into a ColumnTransformer
. Here's the code:
from math import radians
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.compose import ColumnTransformer
from sklearn.metrics.pairwise import haversine_distances
from sklearn.datasets import fetch_california_housing
import numpy as np
import pandas as pd
# Import data into DataFrame
data = fetch_california_housing()
X = pd.DataFrame(data['data'], columns=data['feature_names'])
y = data['target']
# Custom transformer for 'Latitude' and 'Longitude' cols
class NearCity(BaseEstimator, TransformerMixin):
def __init__(self, distance=10):
self.la = (34.05, -118.24)
self.sf = (37.77, -122.41)
self.dis = distance
def calc_dist(self, coords_1, coords_2):
coords_1 = [radians(_) for _ in coords_1]
coords_2 = [radians(_) for _ in coords_2]
result = haversine_distances([coords_1, coords_2])[0,-1]
return result * 6_371
def fit(self, X, y=None):
return self
def transform(self, X):
dist_to_sf = np.apply_along_axis(self.calc_dist, 1, X, coords_2=self.sf)
dist_to_sf = (dist_to_sf < self.dis).astype(int)
dist_to_la = np.apply_along_axis(self.calc_dist, 1, X, coords_2=self.la)
dist_to_la = (dist_to_la < self.dis).astype(int)
X_trans = np.column_stack((X, dist_to_sf, dist_to_la))
return X_trans
ct = ColumnTransformer([('near_city', NearCity(), ['Latitude', 'Longitude'])],
remainder='passthrough')
ct.fit_transform(X)
#> /Users/.../anaconda3/envs/data3/lib/python3.7/site-packages/sklearn/base.py:197: FutureWarning: From version 0.24, get_params will raise an AttributeError if a parameter cannot be retrieved as an instance attribute. Previously it would return None.
#> FutureWarning)
#> Traceback (most recent call last):
#> <ipython-input-13-603f6cd4afd3> in transform(self, X)
#> 17 def transform(self, X):
#> 18 dist_to_sf = np.apply_along_axis(self.calc_dist, 1, X, coords_2=self.sf)
#> ---> 19 dist_to_sf = (dist_to_sf < self.dis).astype(int)
#> 20
#> 21 dist_to_la = np.apply_along_axis(self.calc_dist, 1, X, coords_2=self.la)
#> TypeError: '<' not supported between instances of 'float' and 'NoneType'
Created on 2020-04-23 by the reprexpy package
The issue is that the self.dis
attribute doesn't persist. If I instantiate the transformer by itself, no problem: self.dis = distance = 10
. But in the ColumnTransformer
, it ends up as NoneType
. Oddly enough, if I just hard-code in self.dis = 10
, it works.
What do folks think is going on?
Session info --------------------------------------------------------------------
Platform: Darwin-18.7.0-x86_64-i386-64bit (64-bit)
Python: 3.7
Date: 2020-04-23
Packages ------------------------------------------------------------------------
numpy==1.18.1
pandas==1.0.1
reprexpy==0.3.0
scikit-learn==0.22.1
Upvotes: 3
Views: 1282
Reputation: 243
Turns out the problem lies in sklearn.base
.
deep_items = value.get_params().items()
The get_params()
function looks at the init
arguments to figure out what the class parameters are and then assumes that they're the same as the internal variable names.
So I can solve this problem by changing my init
method to:
def __init__(self, distance=10):
self.la = (34.05, -118.24)
self.sf = (37.77, -122.41)
self.distance = distance # <-- give same name
Many thanks to a colleague of mine who figured this out!
Upvotes: 6