2017-06-19 6 views
0

Gaussian Naive Bayesモデルをデータセットに適用して、疾患を予測しようとしています。トレーニングデータの使用を予測しているときには正しく動作していますが、テストデータを使用して予測するときにはValueErrorと表示されます。Scikit Learn - 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 pandas 
from sklearn import metrics 
from sklearn.preprocessing import Imputer 
from 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_test 


def 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_test 


def 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_data 


feature_columns = ["age", "sex", "chol", "cigs", "years", "fbs", "trestbps", "restecg", "thalach", "exang", "oldpeak", "slope", "ca", "thal", "num"] 
predicted_column = ["cp"] 

x_train, y_train, x_test, y_test = load_data(feature_columns, predicted_column) 

predict(x_train, y_train, x_test, y_test) 

N.B:どちらのファイルも同じ列数です。

+0

完全なスタックトレースを投稿できますか? – EFT

+0

@EFT私は完全なトレースバックを投稿しました。 Btw、私はちょうど '欠損値で完全に構成されているので、' Imputer'が1つの列を削除していることを知りました。これを防ぐ方法はありますか? –

+0

ここにあなたが 'read_excel(" training_data.xlsx ")'で使用しているファイルはありません。パブリックデータセットでこの問題を再現できますか? –

答えて

1

このバグが見つかりました。エラーはImputerのために発生しています。 Imputerは、データセットの欠損値を置き換えます。ただし、列がすべて欠損値で構成されている場合は、その列が削除されます。私は、データセットのテストで完全に欠けている列がありました。だから、Imputerがそれを削除していたため、形状がトレーニングデータと一致せず、その原因がエラーの原因です。行番号をfeature_columnsリストから削除しましたが、これは不足している値でいっぱいでした。

関連する問題