首页 > 解决方案 > 如何在 Numpy 对象中处理 Numpy 对象?

问题描述

我想从这个数据集制作一个多分类模型。

当我处理标签时Keras.preprocessing.image.ImageDataGenerator - flow_from_dataframe

y_train:

array([array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object),
       array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object)],
      dtype=object)

它返回上述结果。从这个结果中,我的模型只识别一个 BIG 数组,而不是分隔数组。如何获得正常的 2D 形状 Numpy 数组?

我的代码:

from keras.preprocessing.image import ImageDataGenerator
gen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        rotation_range=20,
        width_shift_range=0.2,
        height_shift_range=0.2,
        horizontal_flip=True)

xray_data['target_vector']

0         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
1         [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0]
2         [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0]
3         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
                             ...                    
112115    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0]
112116    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
112117    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
112118    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
112119    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Name: target_vector, Length: 112120, dtype: object
train_gen = gen.flow_from_dataframe(xray_data, x_col='full_path', y_col='target_vector', class_mode='raw')
X_train, y_train = train_gen.next()

- >以上结果..

感谢您的友好回答!

标签: pythonnumpykeras

解决方案


推荐阅读