M3.2. How do i get rid of the attribute error?

from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import loguniform
param_distributions = {"logisticregression__C": loguniform(0.001, 10),"columntransformer__num_processor__with_mean":[True, False],"columntransformer__num_processor__with_std": [True, False],}
model_random_search = RandomizedSearchCV(model, param_distributions=param_distributions, n_iter=20, error_score=np.nan, n_jobs=2, verbose=1, random_state=1)

model_random_search.fit(data_train, target_train)

Returns a ‘attribute error’ even after editing my preprocessor for Excercise M2.02

Hi nanfuka,

“columntransformer__num_processor__with_mean” and “columntransformer__num_processor__with_std”

are not the correct attributes.

Use

model_random_search.get_params()

to get a list of all attributes. For me this was

{‘cv’: 5,
‘error_score’: nan,
‘estimator__memory’: None,
‘estimator__steps’: [(‘columntransformer’,
ColumnTransformer(transformers=[(‘one-hot-encoder’,
OneHotEncoder(handle_unknown=‘ignore’),
[‘workclass’, ‘education’, ‘marital-status’,
‘occupation’, ‘relationship’, ‘race’, ‘sex’,
‘native-country’]),
(‘standard-scaler’, StandardScaler(),
[‘age’, ‘capital-gain’, ‘capital-loss’,
‘hours-per-week’])])),
(‘logisticregression’, LogisticRegression(max_iter=500))],
‘estimator__verbose’: False,
‘estimator__columntransformer’: ColumnTransformer(transformers=[(‘one-hot-encoder’,
OneHotEncoder(handle_unknown=‘ignore’),
[‘workclass’, ‘education’, ‘marital-status’,
‘occupation’, ‘relationship’, ‘race’, ‘sex’,
‘native-country’]),
(‘standard-scaler’, StandardScaler(),
[‘age’, ‘capital-gain’, ‘capital-loss’,
‘hours-per-week’])]),
‘estimator__logisticregression’: LogisticRegression(max_iter=500),
‘estimator__columntransformer__n_jobs’: None,
‘estimator__columntransformer__remainder’: ‘drop’,
‘estimator__columntransformer__sparse_threshold’: 0.3,
‘estimator__columntransformer__transformer_weights’: None,
‘estimator__columntransformer__transformers’: [(‘one-hot-encoder’,
OneHotEncoder(handle_unknown=‘ignore’),
[‘workclass’,
‘education’,
‘marital-status’,
‘occupation’,
‘relationship’,
‘race’,
‘sex’,
‘native-country’]),
(‘standard-scaler’,
StandardScaler(),
[‘age’, ‘capital-gain’, ‘capital-loss’, ‘hours-per-week’])],
‘estimator__columntransformer__verbose’: False,
‘estimator__columntransformer__one-hot-encoder’: OneHotEncoder(handle_unknown=‘ignore’),
‘estimator__columntransformer__standard-scaler’: StandardScaler(),
‘estimator__columntransformer__one-hot-encoder__categories’: ‘auto’,
‘estimator__columntransformer__one-hot-encoder__drop’: None,
‘estimator__columntransformer__one-hot-encoder__dtype’: numpy.float64,
‘estimator__columntransformer__one-hot-encoder__handle_unknown’: ‘ignore’,
‘estimator__columntransformer__one-hot-encoder__sparse’: True,
‘estimator__columntransformer__standard-scaler__copy’: True,
‘estimator__columntransformer__standard-scaler__with_mean’: True,
‘estimator__columntransformer__standard-scaler__with_std’: True,
‘estimator__logisticregression__C’: 1.0,
‘estimator__logisticregression__class_weight’: None,
‘estimator__logisticregression__dual’: False,
‘estimator__logisticregression__fit_intercept’: True,
‘estimator__logisticregression__intercept_scaling’: 1,
‘estimator__logisticregression__l1_ratio’: None,
‘estimator__logisticregression__max_iter’: 500,
‘estimator__logisticregression__multi_class’: ‘auto’,
‘estimator__logisticregression__n_jobs’: None,
‘estimator__logisticregression__penalty’: ‘l2’,
‘estimator__logisticregression__random_state’: None,
‘estimator__logisticregression__solver’: ‘lbfgs’,
‘estimator__logisticregression__tol’: 0.0001,
‘estimator__logisticregression__verbose’: 0,
‘estimator__logisticregression__warm_start’: False,
‘estimator’: Pipeline(steps=[(‘columntransformer’,
ColumnTransformer(transformers=[(‘one-hot-encoder’,
OneHotEncoder(handle_unknown=‘ignore’),
[‘workclass’, ‘education’,
‘marital-status’,
‘occupation’, ‘relationship’,
‘race’, ‘sex’,
‘native-country’]),
(‘standard-scaler’,
StandardScaler(),
[‘age’, ‘capital-gain’,
‘capital-loss’,
‘hours-per-week’])])),
(‘logisticregression’, LogisticRegression(max_iter=500))]),
‘n_iter’: 20,
‘n_jobs’: -1,
‘param_distributions’: {‘logisticregression__C’: <scipy.stats._distn_infrastructure.rv_frozen at 0x7f7816b5c490>,
‘columntransformer__standard-scaler__with_mean’: [True, False],
‘columntransformer__standard-scaler__with_std’: [True, False]},
‘pre_dispatch’: ‘2*n_jobs’,
‘random_state’: None,
‘refit’: True,
‘return_train_score’: False,
‘scoring’: None,
‘verbose’: 0}​

and the correct attributes were

‘columntransformer__standard-scaler__with_mean’
‘columntransformer__standard-scaler__with_std’

My definition of the preprocessor looked like this:

preprocessor = ColumnTransformer([
    ('one-hot-encoder', categorical_preprocessor, categorical_columns),
    ('standard-scaler', numerical_preprocessor, numerical_columns)])