Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notebook 5 "can't convert np.ndarray of type numpy.object_" TypeError #99

Open
CaseyHaralson opened this issue Oct 1, 2023 · 3 comments

Comments

@CaseyHaralson
Copy link

When running the 5th notebook "Linear Model and Neural Network From Scratch", there is a type error when trying to convert the dataframe to a tensor.

This code gives the error:

indep_cols = ['Age', 'SibSp', 'Parch', 'LogFare'] + added_cols

t_indep = tensor(df[indep_cols].values, dtype=torch.float)
t_indep

The error: TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

I was able to get around the error by converting the values into a list, but that threw a warning that that method was very slow and to try using an np array. Changing the code to use a np array gives the original error so that didn't help.

What is the best way to do the conversion?

@mayco2070
Copy link

Replace df[indep_cols].values with df[indep_cols].to_numpy(dtype=np.float32)

indep_cols = ['Age', 'SibSp', 'Parch', 'LogFare'] + added_cols
t_indep = tensor(df[indep_cols].to_numpy(dtype=np.float32), dtype=torch.float)
t_indep

This is the recommended way: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.values.html

@CaseyHaralson
Copy link
Author

Replace df[indep_cols].values with df[indep_cols].to_numpy(dtype=np.float32)

indep_cols = ['Age', 'SibSp', 'Parch', 'LogFare'] + added_cols
t_indep = tensor(df[indep_cols].to_numpy(dtype=np.float32), dtype=torch.float)
t_indep

This is the recommended way: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.values.html

Thanks! That worked

@s99100532
Copy link

FYI: the original code is working in kaggle and it is using panda with older version 1.3.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants