AttributeError: 'DataFrame' object has no attribute 'to_numpy'

Viewed 4612

I am using Jupyter notebook and have the following piece of code

def dataframe_to_arrays(dataframe):
# Make a copy of the original dataframe
dataframe1 = dataframe.copy(deep=True)        # deep=True makes a deep copy, including a copy of the data and the indices. 

# Convert non-numeric categorical columns to numbers
c = dataframe1['prognosis'].astype('category')         #  assign catagories
dataframe1['prognosis'] = c.cat.codes                  # this will change categories with codes

# Extract input & outupts as numpy arrays
inputs_array = dataframe1[training_inputs].to_numpy()    # convert dataframe object to numpy arrays
targets_array = dataframe1[training_targets].to_numpy()
return inputs_array, targets_array

The next code block is :

inputs, targets = dataframe_to_arrays(train_df)                   # calling above function
inputs = torch.from_numpy(inputs).type(torch.float32)             # converting numpy arrays to tensors
targets = torch.from_numpy(targets).type(torch.float32)
inputs.dtype, targets.dtype

I have the following error message :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-43-c9e23be5b8bd> in <module>()
----> 1 t_inputs, t_targets = dataframe_to_arrays(test_df)
      2 t_inputs = torch.from_numpy(t_inputs).type(torch.float32)
      3 t_targets = torch.from_numpy(t_targets).type(torch.float32)
      4 t_inputs.dtype, t_targets.dtype

<ipython-input-41-4332692b9701> in dataframe_to_arrays(dataframe)
      8 
      9     # Extract input & outupts as numpy arrays
---> 10     inputs_array = dataframe1[training_inputs].to_numpy()    # convert dataframe object to numpy arrays
     11     targets_array = dataframe1[training_targets].to_numpy()
     12     return inputs_array, targets_array

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   3079 
   3080             .. versionchanged:: 1.1.0
-> 3081 
   3082                Passing compression options as keys in dict is
   3083                supported for compression modes 'gzip' and 'bz2'

AttributeError: 'DataFrame' object has no attribute 'to_numpy'

I can't understand what is going wrong over here. I updated pandas as suggested in one reply to a similar question but it is of no use apparently.

0 Answers
Related