简体   繁体   中英

ValueError:Multiple array of one array not same length . conver dtype=object to dtype= float32

enter image description here

  train_image = []
  for i in tqdm(range(sample_train.shape[0])):
    img = image.load_img(sample_train["images"][i],target_size=(224,224))
    img = image.img_to_array(img)
    img = img/255
    train_image.append(img)

x = np.array(train_image)
x = x.astype(np.float32)
print(x.shape)
print(type(x))
x.dtype

(101, 224, 224, 3)
<class 'numpy.ndarray'>
dtype('float32')

x[1]

array([[[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        ...,
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
         dtype=float32)

y = np.array(sample_train.number.map(lambda x: np.array(x, dtype=np.float32)))
y
array([21., 29., 18., 27., 21., 16.,  7., 24.,  5., 18.,  8.,  7., 20.,
        8.,  7., 22., 11., 23.,  8., 24.,  5., 26.,  7.,  4.,  7., 11.,
        4., 13.,  4., 14.,  4.,  7., 14.,  0.,  7., 13.,  4., 11.,  4.,
       10.,  4.,  7.,  7.,  4.,  8.,  8.,  4.,  8.,  6.,  0.,  7., 11.,
        1.,  7., 13.,  1.,  8., 15.,  0.,  8., 13.,  3.,  8., 14.,  1.,
        8., 11.,  4.,  7.,  9.,  4., 15.,  4.,  7., 12.,  0.,  7.,  6.,
        4.,  7., 10.,  4.,  8., 11.,  1.,  8.,  7.,  4.,  8., 10.,  4.,
        8.,  9.,  4.,  7., 15.,  4., 12.,  4.,  8.,  4.,  9.,  4.,  7.,
        8.,  4.,  8., 12.,  0.,  7., 15.,  1.,  8.,  7.,  5., 27.,  8.,
        4., 14.,  3.,  7.,  7.,  4.,  7.,  8.,  3.,  7., 12., 20.,  3.,
       15.,  4.,  7.,  6.,  3.,  7.,  9.,  4.,  7., 10., 20.,  8.,  3.,
        7., 20.,  9.], dtype=float32),
       array([21., 29., 18., 27., 21., 16.,  7., 24.,  5., 18.,  7.,  9., 20.,
        7., 14., 22.,  8., 23., 11., 24.,  5., 26.,  7.,  4., 10.,  4.,
        7.,  6.,  0.,  7.,  9.,  0.,  7., 13.,  1.,  7., 14.,  1.,  7.,
       11.,  4.,  7.,  8.,  0.,  7., 12.,  1., 15.,  4., 11.,  4., 13.,
        0.,  8.,  1., 14.,  0.,  9.,  1.,  7.,  7.,  0., 12.,  4., 15.,
        1.,  8.,  7.,  0.,  7., 10.,  3.,  7., 15.,  1.,  8.,  6.,  5.,
       27., 11.,  4., 12.,  3.,  7.,  6., 20.,  3., 10., 20.,  8.,  3.,
        7.,  4.,  9., 20.,  9.,  3.,  0., 20.,  3.,  7., 11.,  3.,  7.,
       12.,  1.,  0., 20.,  3.,  7., 13.,  3.,  7., 14.,  1.,  0., 20.,
        8.,  3.,  7., 10.,  3.,  7., 15.,  3.,  8.,  6.,  1.],
      dtype=float32),
       array([21., 29., 18., 27., 21., 16.,  7., 24.,  5., 18.,  7.,  9., 20.,
        7., 11., 17., 30., 22.,  8., 23.,  9.,  5., 26.,  7.,  4., 13.,
        4., 11.,  4.,  7., 11.,  4., 12.,  4., 15.,  0., 13.,  1.,  7.,
        8.,  0.,  7., 13.,  1.,  7., 12.,  4.,  7.,  7.,  4.,  9.,  4.,
        8.,  4., 14.,  0.,  7.,  9.,  0.,  7., 14.,  1.,  7., 15.,  1.,
       10.,  4.,  7.,  6.,  0.,  7.,  7.,  1.,  7., 10.,  5., 27.,  8.,
        4., 10.,  3., 13.,  3., 15.,  3.,  7., 11., 20.,  3., 11.,  4.,
       12., 20.,  8.,  3.,  7., 20.,  9.,  3.,  0., 20.,  3.,  7., 12.,
        3.,  7., 13.,  1.,  0., 20.,  3.,  7., 14.,  3.,  7., 15.,  1.,
        5., 32., 13.,  4.,  3., 15.,  4.,  5., 28.,  7.,  5., 31.,  7.],
      dtype=float32),
       array([21., 29., 18., 27., 21., 16.,  7., 24.,  5., 18.,  7.,  9., 20.,
        7.,  8., 17., 30., 19.,  9., 22., 10., 23., 24.,  5., 26.,  7.,
       10.,  4., 14.,  4., 10.,  4., 15.,  0.,  8.,  9.,  4., 12.,  4.,
       14.,  1.,  7.,  8.,  0.,  8.,  8.,  1.,  7., 14.,  4., 11.,  4.,
        7.,  7.,  4.,  8.,  6.,  4.,  7., 15.,  4.,  7.,  6.,  4.,  9.,
        4., 13.,  0.,  7.,  9.,  0.,  7., 11.,  3.,  7., 12.,  1.,  7.,
       13.,  1.,  7.,  4.,  8.,  4.,  8.,  7.,  0.,  7.,  6.,  1.,  7.,
        7.,  5., 27., 10.,  3., 12.,  4., 13., 20.,  3.,  7.,  4.,  9.,
        3., 11., 20.,  8.,  3.,  0., 20.,  3.,  7., 14.,  3.,  8.,  8.,
        1.,  5., 32., 13.,  4.,  5., 28.,  6.,  5., 31.,  7.],
      dtype=float32)], dtype=object)
y = np.array(y,dtype='float32')



TypeError Traceback (most recent call last) TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last) in () ----> 1 y = np.array(y,dtype='float32') ValueError: setting an array element with a sequence.

y = np.array(y)

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=42, test_size=0.1)

print("x_train :",len(x_train),"Type:",type(x_train))
print("x_test  :",len(x_test),"Type:",type(x_test))
print("y_train :",len(y_train),"Type:",type(y_train))
print("y_test  :",len(y_test),"Type:",type(y_test))

x_train : 90 Type: <class 'numpy.ndarray'>
x_test  : 11 Type: <class 'numpy.ndarray'>
y_train : 90 Type: <class 'numpy.ndarray'>
y_test  : 11 Type: <class 'numpy.ndarray'>

Input_img = Input(shape=(224, 224, 3))  
    
#encoding architecture
x1 = Conv2D(256, (3, 3), activation='relu', padding='same')(Input_img)
x2 = Conv2D(128, (3, 3), activation='relu', padding='same')(x1)
x2 = MaxPool2D( (2, 2))(x2)
encoded = Conv2D(64, (3, 3), activation='relu', padding='same')(x2)

# decoding architecture
x3 = Conv2D(64, (3, 3), activation='relu', padding='same')(encoded)
x3 = UpSampling2D((2, 2))(x3)
x2 = Conv2D(128, (3, 3), activation='relu', padding='same')(x3)
x1 = Conv2D(256, (3, 3), activation='relu', padding='same')(x2)
decoded = Conv2D(3, (3, 3), padding='same')(x1)

autoencoder = Model(Input_img, decoded)
autoencoder.compile(optimizer='adam', loss='mse', metrics=['accuracy'])


model = autoencoder.fit(x_train,y_train,epochs=1,
            validation_data=(x_test,y_test))


ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

How change the y dtype=object to dtype=float32

Make a frame with array elements:

In [218]: df = pd.DataFrame([None,None],columns=['a'])
In [219]: df
Out[219]: 
      a
0  None
1  None
In [220]: df['a']=[np.array([1,2,3]), np.array([5,6,7])]
In [221]: df
Out[221]: 
           a
0  [1, 2, 3]
1  [5, 6, 7]

This frame contains objects, not numbers. Trying to make numeric array from that produces your error:

In [222]: np.array(df, int)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<ipython-input-222-145b73159da6>", line 1, in <module>
    np.array(df, int)
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py", line 1899, in __array__
    return np.asarray(self._values, dtype=dtype)
  File "/usr/local/lib/python3.8/dist-packages/numpy/core/_asarray.py", line 102, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

Look at values :

In [223]: df.values
Out[223]: 
array([[array([1, 2, 3])],
       [array([5, 6, 7])]], dtype=object)

That's a 2d array; values of just one column is a 1d array:

In [225]: df['a'].values
Out[225]: array([array([1, 2, 3]), array([5, 6, 7])], dtype=object)

We can use stack (or np.vstack ) to join those arrays into one numeric 2d array (provided all arrays have the same size):

In [227]: np.stack(df['a'].values)
Out[227]: 
array([[1, 2, 3],
       [5, 6, 7]])

Make sure the values look right. The dataframe display may not adequately distinguish between string, list and array elements.

Another way is to convert the values array to a list, and make an array from that:

In [228]: df.values.tolist()
Out[228]: [[array([1, 2, 3])], [array([5, 6, 7])]]
In [229]: np.array(df.values.tolist())
Out[229]: 
array([[[1, 2, 3]],

       [[5, 6, 7]]])

Again, get rid of the extra dimension by selecting the column:

In [230]: np.array(df['a'].values.tolist())
Out[230]: 
array([[1, 2, 3],
       [5, 6, 7]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM