ValueError:无法从形状(2712,1)中广播输入阵列(2712,)

发布于 2025-01-23 14:06:20 字数 1894 浏览 0 评论 0原文

我正在为分类问题建立神经网络模型,该模型决定客户是否会流失,输出是二进制0和1。我还使用了随机森林模型和XGBoost模型。他们都工作了。我将随机森林与Xgboost结合在一起,效果很好。

但是,当我使用投票分类器将随机森林(Xgboost)与神经网络(KERAS分类器)结合在一起时,我得到了错误值:无法从形状(2712,1)中广播输入阵列(2712,2712,


from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy


 
# Function to create model, required for KerasClassifier
def create_model():
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=17, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1,activation='sigmoid'))
# Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model


# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# create model


Kc_model = KerasClassifier(build_fn=create_model)
#model.set_params(epochs=100, batch_size=10, verbose=0)
Kc_model._estimator_type = "classifier"

Kc_model.fit(X_train, y_train, epochs=100,batch_size=10)


print("The accuracy score for Keras Model is")
print("Test set: {}%".format(round(Kc_model.score(X_test, y_test)*100))) 

)以下投票分类器的代码:


 from keras.wrappers.scikit_learn import KerasClassifier
import scikeras
from tensorflow import keras


voting = VotingClassifier(
    estimators = [('rf',rf),('xgboost_model',xgboost_model),('Kc_model',Kc_model) ],
    voting='hard')
#reshaping=y_test.reshape(2712,1)
voting_model =voting.fit(X_train, y_train)
voting_pred = voting_model.predict(X_test)
#Model Score
print("The accuracy score for Voting Classifier is")
print("Training:{}%".format(round(voting_model.score(X_train, y_train)*100)))
print("Test set: {}%".format(round(voting_model.score(X_test, y_test)*100)))

I am building a neural network model for a classification problem that determines whether a customer will churn or not, and the output is a binary 0 and 1. I also used Random Forest Model and XGboost model. They all worked. I combined the random forest with XGBoost, and it worked fine.

However, when I combined the random forest, XGBoost , with the neural network (Keras classifier) using the voting classifier, I got the error ValueError: could not broadcast input array from the shape (2712,1) into shape (2712,)


from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy


 
# Function to create model, required for KerasClassifier
def create_model():
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=17, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1,activation='sigmoid'))
# Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model


# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# create model


Kc_model = KerasClassifier(build_fn=create_model)
#model.set_params(epochs=100, batch_size=10, verbose=0)
Kc_model._estimator_type = "classifier"

Kc_model.fit(X_train, y_train, epochs=100,batch_size=10)


print("The accuracy score for Keras Model is")
print("Test set: {}%".format(round(Kc_model.score(X_test, y_test)*100))) 

The code for the voting classifier below:


 from keras.wrappers.scikit_learn import KerasClassifier
import scikeras
from tensorflow import keras


voting = VotingClassifier(
    estimators = [('rf',rf),('xgboost_model',xgboost_model),('Kc_model',Kc_model) ],
    voting='hard')
#reshaping=y_test.reshape(2712,1)
voting_model =voting.fit(X_train, y_train)
voting_pred = voting_model.predict(X_test)
#Model Score
print("The accuracy score for Voting Classifier is")
print("Training:{}%".format(round(voting_model.score(X_train, y_train)*100)))
print("Test set: {}%".format(round(voting_model.score(X_test, y_test)*100)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2025-01-30 14:06:21

似乎您的分类器之一以Shape (2712,1)(2d)输出预测,而一个带有Shape (2712,)(1d)的预测。 投票Classifier无法将2D和1D预测结合在一起,因此您会出现错误。

我建议仔细查看您的分类器的预测。尝试使它们具有相同数量的尺寸的输出预测。

It seems like one of your classifiers outputs prediction with shape (2712,1) (2D), and one with shape (2712,) (1D). VotingClassifier can't combine 2D and 1D predictions, so you obtain an error.

I suggest looking carefully at predictions of your classifiers. Try to make them output predictions with the same number of dimensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文