Scikit Learn – ValueError: 操作数无法一起广播

我在尝试对数据集应用Gaussian Naive Bayes模型来预测疾病。当使用训练数据进行预测时运行正常,但当我尝试使用测试数据进行预测时,它会返回ValueError错误。

runfile(‘D:/ROFI/ML/Heart Disease/prediction.py’, wdir=’D:/ROFI/ML/Heart Disease’) Traceback (most recent call last):

File “”, line 1, in runfile(‘D:/ROFI/ML/Heart Disease/prediction.py’, wdir=’D:/ROFI/ML/Heart Disease’)

File “C:\Users\User\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 866, in runfile execfile(filename, namespace)

File “C:\Users\User\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile exec(compile(f.read(), filename, ‘exec’), namespace)

File “D:/ROFI/ML/Heart Disease/prediction.py”, line 85, in predict(x_train, y_train, x_test, y_test)

File “D:/ROFI/ML/Heart Disease/prediction.py”, line 73, in predict predicted_data = model.predict(x_test)

File “C:\Users\User\Anaconda3\lib\site-packages\sklearn\naive_bayes.py”, line 65, in predict jll = self._joint_log_likelihood(X)

File “C:\Users\User\Anaconda3\lib\site-packages\sklearn\naive_bayes.py”, line 429, in _joint_log_likelihood n_ij -= 0.5 * np.sum(((X – self.theta_[i, :]) ** 2) /

ValueError: operands could not be broadcast together with shapes (294,14) (15,)

这里出了什么问题?

import pandasfrom sklearn import metricsfrom sklearn.preprocessing import Imputerfrom sklearn.naive_bayes import GaussianNB    def load_data(feature_columns, predicted_column):    train_data_frame = pandas.read_excel("training_data.xlsx")    test_data_frame = pandas.read_excel("testing_data.xlsx")    data_frame = pandas.read_excel("data_set.xlsx")    x_train = train_data_frame[feature_columns].values    y_train = train_data_frame[predicted_column].values    x_test = test_data_frame[feature_columns].values    y_test = test_data_frame[predicted_column].values    x_train, x_test = impute(x_train, x_test)    return x_train, y_train, x_test, y_testdef impute(x_train, x_test):    fill_missing = Imputer(missing_values=-9, strategy="mean", axis=0)    x_train = fill_missing.fit_transform(x_train)    x_test = fill_missing.fit_transform(x_test)    return x_train, x_testdef predict(x_train, y_train, x_test, y_test):    model = GaussianNB()    model.fit(x_train, y_train.ravel())    predicted_data = model.predict(x_test)    accuracy = metrics.accuracy_score(y_test, predicted_data)    print("Accuracy of our naive bayes model is : %.2f"%(accuracy * 100))    return predicted_datapredicted_column = ["cp"]feature_columns = ["age", "sex", "chol", "cigs", "years", "fbs", "trestbps", "restecg", "thalach", "exang", "oldpeak", "slope", "ca", "thal", "num"]x_train, y_train, x_test, y_test = load_data(feature_columns, predicted_column)predict(x_train, y_train, x_test, y_test)

注意:两个文件的列数相同。


回答:

我找到了错误。错误是由Imputer引起的。Imputer用于替换数据集中的缺失值。但是,如果某列完全由缺失值组成,它会删除该列。我的测试数据集中有一列完全是缺失值。因此,Imputer删除了该列,导致形状与训练数据不匹配,这就是错误的原因。我从feature_columns列表中移除了完全由缺失值组成的列名,问题就解决了。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注