Reputation: 81
I am doing a course on udemy and I am using a custom scaler function they've provided to scale the features that aren't dummies in the dataset but I am receiving an error. I am pretty certain that I've followed the code line for line, but I could have missed something
I've attached the code and a file to my github
Below are the functions:
#Custom Scaler to avoid scaling dummies
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler
class CustomScaler(BaseEstimator, TransformerMixin):
def _init_(self,columns, copy=True, with_mean=True, with_std=True):
self.scaler = StandardScaler(copy, with_mean, with_std)
self.columns = columns
self.mean_ = None
self.var_ = None
def fit(self, X, y=None):
self.scaler.fit(X[self.columns], y)
self.mean_ = np.mean(X[self.columns])
self.var_ = np.var(X[self.columns])
return self
def transform(self, X, y=None, copy=None):
init_col_order = X.columns
X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]), columns=self.columns)
X_not_scaled = X.loc[:,~X.columns.isin(self.columns)]
return pd.concat([X_not_scaled, X_scaled], axis=1)[init_col_order]
Then I pull the columns that I want to scale into a variable and call the function:
unscaled_inputs.columns.values
columns_to_scale=['Month Value','Day of the Week', 'Transportation Expense', 'Distance to Work',
'Age', 'Daily Work Load Average', 'Body Mass Index', 'Children', 'Pets']
absenteeism_scaler = CustomScaler(columns_to_scale)
Then the following error is outputted:
TypeError Traceback (most recent call last)
<ipython-input-26-c6390babc5b1> in <module>
----> 1 absenteeism_scaler = CustomScaler(columns_to_scale)
TypeError: CustomScaler() takes no arguments
This seems weird as I thought ordinarily this was avoided by including self in the parentheses of the functions.
If anyone is able to help I'd be most grateful!
Upvotes: 0
Views: 614
Reputation: 5164
Your class definition has a typo. The __init__()
function is a special method to instantiate customized objects. Observe that it has two leading and two trailing _
enclosing the word init
, while yours has only one on each side.
The correct definition should be:
class CustomScaler(BaseEstimator, TransformerMixin):
def __init__(self,columns, copy=True, with_mean=True, with_std=True):
self.scaler = StandardScaler(copy, with_mean, with_std)
self.columns = columns
self.mean_ = None
self.var_ = None
...
Now you can pass columns_to_scale
to the constructor of CustomScaler
as you do in your code snippet:
absenteeism_scaler = CustomScaler(columns_to_scale)
Upvotes: 1