【问题标题】:ValueError:Multiple array of one array not same length . conver dtype=object to dtype= float32ValueError:一个数组的多个数组长度不同。将 dtype=object 转换为 dtype=float32
【发布时间】:2021-07-19 00:05:57
【问题描述】:

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(最近一次调用最后一次) TypeError: 只有 size-1 的数组可以转换为 Python 标量

上述异常是以下异常的直接原因:

ValueError Traceback(最近一次调用最后一次) 在 () ----> 1 y = np.array(y,dtype='float32') ValueError: 使用序列设置数组元素。

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:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。

如何将 y dtype=object 改为 dtype=float32

【问题讨论】:

  • 您的问题不清楚,请清楚问题是什么以及正在寻找什么建议,
  • 请发布输入数据和预期输出数据。
  • 我有预感你在找y = y.values
  • y.values,注意shape、dtype和一些元素。

标签: python arrays numpy


【解决方案1】:

用数组元素制作一个框架:

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]

此框架包含对象,而不是数字。尝试从中生成数字数组会产生错误:

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.

values

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

这是一个二维数组; values 只有一列是一维数组:

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

我们可以使用stack(或np.vstack)将这些数组连接成一个数字二维数组(前提是所有数组的大小相同):

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

确保values 看起来正确。数据框显示可能无法充分区分字符串、列表和数组元素。

另一种方法是将values 数组转换为列表,并从中创建一个数组:

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]]])

再次,通过选择列去除多余的维度:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    相关资源
    最近更新 更多