Fit the model of Ridge class from sklearn.linear_model

Facing problem in Fitting the model of Ridge class from sklearn.linear_model.
Can anyone help with this…

Code:

from sklearn.linear_model import Ridge

Create the model

model = Ridge(solver = ‘liblinear’)

Fit the model using inputs and targets

model.fit(train_inputs[numeric_cols + encoded_cols], train_targets)

Output:

ValueError Traceback (most recent call last)
in
1 # Fit the model using inputs and targets
----> 2 model.fit(train_inputs[numeric_cols + encoded_cols], train_targets)

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_ridge.py in fit(self, X, y, sample_weight)
760 self : returns an instance of self.
761 “”"
→ 762 return super().fit(X, y, sample_weight=sample_weight)
763
764

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_ridge.py in fit(self, X, y, sample_weight)
540 _accept_sparse = _get_valid_accept_sparse(sparse.issparse(X),
541 self.solver)
→ 542 X, y = self._validate_data(X, y,
543 accept_sparse=_accept_sparse,
544 dtype=_dtype,

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
431 y = check_array(y, **check_y_params)
432 else:
→ 433 X, y = check_X_y(X, y, **check_params)
434 out = X, y
435

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
61 extra_args = len(args) - len(all_args)
62 if extra_args <= 0:
—> 63 return f(*args, **kwargs)
64
65 # extra_args > 0

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
869 raise ValueError(“y cannot be None”)
870
→ 871 X = check_array(X, accept_sparse=accept_sparse,
872 accept_large_sparse=accept_large_sparse,
873 dtype=dtype, order=order, copy=copy,

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
61 extra_args = len(args) - len(all_args)
62 if extra_args <= 0:
—> 63 return f(*args, **kwargs)
64
65 # extra_args > 0

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
718
719 if force_all_finite:
→ 720 _assert_all_finite(array,
721 allow_nan=force_all_finite == ‘allow-nan’)
722

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in _assert_all_finite(X, allow_nan, msg_dtype)
101 not allow_nan and not np.isfinite(X).all()):
102 type_err = ‘infinity’ if allow_nan else ‘NaN, infinity’
→ 103 raise ValueError(
104 msg_err.format
105 (type_err,

ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’).

Hi,
Ridge does not take the argument ‘liblinear’ as a solver input.

Following are the solver inputs for Ridge:
solver = {‘auto’, ‘svd’, ‘cholesky’, ‘lsqr’, ‘sparse_cg’, ‘sag’, ‘saga’}, default=’auto’

For more details on Ridge: sklearn.linear_model.Ridge — scikit-learn 0.24.2 documentation

Hence, you can simply use: model = Ridge()
This will create a Ridge model.

Additionally, train_inputs is combination of numerical_cols and encoded_cols. Hence, you can just use model.fit(train_inputs, train_targets)

Hope this helps.

Thanks

1 Like

Thanks for reply…
I have done applied what you have suggested and user #Ridge()

But now it started showing:

ValueError: could not convert string to float: ‘RL’

Make sure you have encoded the categorical columns and did not include the columns with dtype=object while training the model.

my assignment 1 is not yet evaluated can you look into it and grade accordingly

which is training ml model zero to GBMs

We will evaluate the assignments after the deadline of 5th July.

Yes sir, i tried to troubleshoot it by various ways but still stuck at same point…

It says :
ValueError: could not convert string to float: ‘unknown’

Now.

Sir, I’m also getting the same issue

could not convert string to float: ‘MSZoning_C (all)’

Make sure to use the encoded_columns for training, instead of categorical_columns.

Hi gnspda,

how did you solve that issue?
I’m facing the same here.

Thanks

This code generates an error and i cant figure out where my error lies can anyone help out?

Create the model

model = Ridge()

Fit the model using inputs and targets

model.fit(train_inputs[numeric_cols + encoded_cols], train_targets)

@birajde kindly look into this

Can you post a screenshot with the error message?


ValueError Traceback (most recent call last)
/tmp/ipykernel_54/1206261872.py in
1 # Fit the model using inputs and targets
----> 2 model.fit(train_inputs[numeric_cols + encoded_cols], train_targets)

/opt/conda/lib/python3.9/site-packages/sklearn/linear_model/_ridge.py in fit(self, X, y, sample_weight)
1001 “”"
1002 _accept_sparse = _get_valid_accept_sparse(sparse.issparse(X), self.solver)
→ 1003 X, y = self._validate_data(
1004 X,
1005 y,

/opt/conda/lib/python3.9/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
579 y = check_array(y, **check_y_params)
580 else:
→ 581 X, y = check_X_y(X, y, **check_params)
582 out = X, y
583

/opt/conda/lib/python3.9/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
962 raise ValueError(“y cannot be None”)
963
→ 964 X = check_array(
965 X,
966 accept_sparse=accept_sparse,

/opt/conda/lib/python3.9/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
798
799 if force_all_finite:
→ 800 _assert_all_finite(array, allow_nan=force_all_finite == “allow-nan”)
801
802 if ensure_min_samples > 0:

/opt/conda/lib/python3.9/site-packages/sklearn/utils/validation.py in _assert_all_finite(X, allow_nan, msg_dtype)
112 ):
113 type_err = “infinity” if allow_nan else “NaN, infinity”
→ 114 raise ValueError(
115 msg_err.format(
116 type_err, msg_dtype if msg_dtype is not None else X.dtype

ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’).

@ utkarsh736
i have just provided the error message i get