ValueError:无法将输入数组从形状 (2,128) 广播到形状 (1,128)

问题描述 投票:0回答:1

我正在创建一个预测不同类型药物的神经网络,但我收到了一个我不理解的错误,并且我在代码中没有找到问题,如果有人可以帮助我并给我一个解释请。

这是错误的回溯:

Traceback (most recent call last):
  File "C:\Users\matte\code\projects\python_projects\simple_drug_classification\rework.py", line 93, in <module>
    model = model_class.model_training()
  File "C:\Users\matte\code\projects\python_projects\simple_drug_classification\rework.py", line 50, in model_training
    model.fit(
  File "C:\Users\matte\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\matte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py", line 1997, in __array__
    values = self._values
  File "C:\Users\matte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 1001, in _values
    return ensure_wrapped_if_datetimelike(self.values)
  File "C:\Users\matte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 11360, in values
    return self._mgr.as_array()
  File "C:\Users\matte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 1732, in as_array
    arr = self._interleave(dtype=dtype, na_value=na_value)
  File "C:\Users\matte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 1794, in _interleave
    result[rl.indexer] = arr
ValueError: could not broadcast input array from shape (2,128) into shape (1,128)

这是我创建神经网络模型的代码部分:

class Model:
    def __init__(self, X_train, X_test, y_train, y_test):
        self.X_train = X_train
        self.X_test = X_test
        self.y_train = y_train
        self.y_test = y_test

        self.N_HIDDEN_LAYERS = 128
        self.NB_CLASSES = 5


    def model_architecture(self):
        model = keras.models.Sequential()

        model.add(keras.layers.Dense(
            self.N_HIDDEN_LAYERS, input_shape=(6,), activation="relu"
        ))

        model.add(keras.layers.Dropout(0.2))
        
        model.add(keras.layers.Dense(
            self.N_HIDDEN_LAYERS, activation="relu"
        ))

        model.add(keras.layers.Dense(
            self.NB_CLASSES, activation="softmax"
        ))

        model.compile(loss="categorical_crossentropy", metrics=["accuracy"])
        return model

    
    def model_training(self):
        model = self.model_architecture()

        BATCH_SIZE = 32
        EPOCHS = 10
        VERBOSE = 1
        VALIDATION_SPLIT = 0.2

        model.fit(
            self.X_train,
            self.y_train,
            batch_size=BATCH_SIZE,
            epochs=EPOCHS,
            verbose=VERBOSE,
            validation_split=VALIDATION_SPLIT
        )

        model.evaluate(self.X_test, self.y_test)
        model.save("model")

这是我预处理数据的代码部分:

data = pd.read_csv("data.csv")

numerical_data = data[["Age", "Na_to_K"]]
categorical_data = data[["Sex", "Drug"]]
text_data = data[["BP", "Cholesterol"]]

# Label encode categorical data
label_encoder = LabelEncoder()
for column in categorical_data.columns:
    categorical_data[column] = label_encoder.fit_transform(categorical_data[column])

# TF-IDF vectorization for text data
vectorizer = TfidfVectorizer()
for column in text_data.columns:
    text_data[column] = vectorizer.fit_transform(text_data[column]).toarray()

# Standard scaling for numerical data
scaler = StandardScaler()
numerical_data = scaler.fit_transform(numerical_data)

# Merge all data together
merged_data = pd.concat([pd.DataFrame(numerical_data), categorical_data, pd.DataFrame(text_data)], axis=1)

# Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(merged_data, data["Drug"], test_size=0.2, random_state=42)

print("X_train shape: ", X_train.shape, " y_train shape: ", y_train.shape)
print("X_test shape: ", X_test.shape, " y_test shape: ", y_test.shape)

model_class = Model(X_train, X_test, y_train, y_test)
model = model_class.model_training()

X_train、X_test、y_train、y_test 的形状如下:

X_train shape:  (160, 6)  y_train shape:  (160,)
X_test shape:  (40, 6)  y_test shape:  (40,)

最后,我在 data.csv 中的数据如下所示:

Age,Sex,BP,Cholesterol,Na_to_K,Drug
23,F,HIGH,HIGH,25.355,DrugY

我尝试将 y_train 和 y_test 转换为数组,在 (-1, 1) 中重塑它们,然后使用 OneHotencoding 进行编码,但我仍然遇到相同的错误。

python neural-network shapes prediction
1个回答
0
投票

看来问题出在

preprocessing
这一步。您正在使用
LabelEncoder
对“药物”列进行编码,这是您的
output
标签。但是,LabelEncoder 不支持 one-hot 编码,而这对于像您这样的
multi-class
分类问题来说是必需的。

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
#...

# One-hot encode output labels
onehot_encoder = OneHotEncoder(sparse=False)
y_train_encoded = onehot_encoder.fit_transform(y_train.values.reshape(-1, 1))
y_test_encoded = onehot_encoder.transform(y_test.values.reshape(-1, 1))

# ...
© www.soinside.com 2019 - 2024. All rights reserved.