0

I read the MNIST dataset and put the labels inside the y-variable. It's a ndarray object containing 70000 instances of labels and its shape is (70000,) Now, I wish to convert the labels to float64, but my interpreter tells "maximum supported dimension for an ndarray is 32, found 70000".

So I don't understand why it originally contains 70000 instances as an ndarray object without any problem and than reports this error when trying to convert the internal datatype.

    import pandas as pd
    import numpy as np
    print(type(y))
    print(y.shape)
    y = np.ndarray(y,dtype=np.float64)
    print(X.dtype,y.dtype)
    data = pd.DataFrame()


Output:
<class 'numpy.ndarray'>
(70000,)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-5d7ca8b0f5a9> in <module>
      3 print(type(y))
      4 print(y.shape)
----> 5 y = np.ndarray(y,dtype=np.float64)
      6 print(X.dtype,y.dtype)
      7 data = pd.DataFrame()

ValueError: maximum supported dimension for an ndarray is 32, found 70000
BURNS
  • 711
  • 1
  • 9
  • 20
  • 4
    Use `numpy.array` instead of `numpy.ndarray`. `ndarray` is actually a relatively low-level object. Its first argument is the *shape* of the array to be created, not the data to put in the array. You are passing in `y`, which has length 70000, so `ndarray` interprets that as an attempt to create an array with 70000 dimensions. – Warren Weckesser Jun 30 '20 at 14:34
  • 1
    Also, if what you want is simply to change the type of `y`, you can simply do `y = y.astype(np.float64)` (may pass `copy=False` if you want to avoid making a copy in the case `y` has already type `np.float64`). – jdehesa Jun 30 '20 at 14:50
  • 1
    What are these `labels` objects? Does `float(y[0])` work? If `y` is already a numpy array there are correct methods for converting the `dtype` - within limits. `np.ndarray` is advanced feature that you shouldn't use (especially without reading its docs carefully). – hpaulj Jun 30 '20 at 15:54
  • 1
    You weren't the only one mistakenly using `ndarray` today: https://stackoverflow.com/questions/62661651/cannot-understand-the-output-of-this-code-python-list-to-1-d-numpy-array – hpaulj Jun 30 '20 at 16:33

0 Answers0