Facing problem when try to Fit Ridge model

I am trying to fit the model but I get an error saying:
ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’).

and when I check the null values I get 0 nuns and idk where is the problem can anyone help me

and I thought maybe cuz targets i didn’t scale them so I made a scale to target (0 to 1 ) but didn’t work

this is the code :

X_train = train_inputs[numeric_cols + encoded_cols]
X_val = val_inputs[numeric_cols + encoded_cols]

Create the model

model = Ridge()

model.fit(X_train,targets)
#the error :Input contains NaN, infinity or a value too large for dtype(‘float64’).

#check null values
X_train.isna().sum()

@fady-aziz6799 have u been able to implement simple imputer successfully.

i u have done it correctly then u shouldn’t get this problem.

Anyways if u continue to get the error pls provide some more code snapshots

yes, you bet, I figured out that I made fit to the input not to the data frame so that’s why I got this error but I fixed it but now working and thank you for your help <3

2 Likes

The categorical features contain NaN values , so you need to fill them with the string ‘unknown’, that way the model ignore them:

inputs_df[categorical_cols]= inputs_df[categorical_cols].fillna(‘unknown’)

thanks! good luck bro !

2 Likes

I’m having the same problem, plz Could you please provide the code snapshot how you fix the model fit :

I wrote like this

model.fit(train_inputs[numeric_cols + categorical_cols], train_targets)
I don’t have any idea how to to change it.
Thank in advance

1 Like

@compengmalik
look I fixed it by noticed that I made fit the imputer to the input, not to the data frame
that what I made :
imputer.fit(inputs_df[numeric_cols]
but when I tried to fit the df it’s worked like this :
imputer.fit(prices_df[numeric_cols])
if this doesn’t work with you : try what @samir-kouider said above
or just try to make it from the beginning you would notice something
good luck bro <3

Hey you are using categorical_cols to fit the model, instead you have to use encoded columns, categorical cols are non numeric type data they can not be interpreted by machines, you have to encode them using any encoder like label encoder, one hot encoder etc.

1 Like

Facing the same issue.

model.fit(train_inputs,train_targets)

even though I used prices_df for inputing also.

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

please help

1 Like

I too got issue while fitting ridge regression

My code:
model.fit(inputs_df[numeric_cols + encoded_cols] ,targets)

error:
TypeError: fit() missing 1 required positional argument: ‘y’

1 Like

did you put after ridge two brackets Ridge()

2 Likes

is train_inputs contain numeric_cols + encoded_cols
like this
X_train =train_inputs[numeric_cols + encoded_cols]

1 Like

solved it thanx for your reply

1 Like

Hi, I tried but it still doesn’t help. Can’t figure out what I’ve done wrong?

Hey, please don’t send the entire assignment notebook in forum, as this is an assignment. You can share snippets of code. Give others a chance to complete the assignments on their own.
Regarding the error, You have used prices_df for imputing and scaling, but while you are doing train_test_split() you are using inputs_df which was not imputed/scaled so it has NaN values. Instead you should use inputs_df for imputing and scaling.

2 Likes

Thank you for telling me. My bad. I’ve removed the link.

2 Likes