ValueError:找到具有0个样本的数组(shape =(0,40)),而最少需要1个

时间:2018-11-13 20:37:10

标签: python parameters scikit-learn spyder grid-search

我一直在寻找解决此错误的方法已有一段时间了。这是我的代码片段,用于通过使用nflgame模块提取数据来预测nfl游戏的结果。当我第一次把它放在一起时,它起作用了。但是,它不再起作用,并给我以下值错误:

  File "<ipython-input-2-3d530b02cd5f>", line 1, in <module>
    runfile('C:/Users/jtberg/Desktop/Machine_Learning-PYTHON/predict.py', wdir='C:/Users/jtberg/Desktop/Machine_Learning-PYTHON')

  File "C:\Python27\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Python27\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 93, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/jtberg/Desktop/Machine_Learning-PYTHON/predict.py", line 98, in <module>
    predictions = model.predict(prediction_data)

  File "C:\Python27\lib\site-packages\sklearn\svm\base.py", line 567, in predict
    y = super(BaseSVC, self).predict(X)

  File "C:\Python27\lib\site-packages\sklearn\svm\base.py", line 325, in predict
    X = self._validate_for_predict(X)

  File "C:\Python27\lib\site-packages\sklearn\svm\base.py", line 458, in _validate_for_predict
    accept_large_sparse=False)

  File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 577, in check_array
    context))

ValueError: Found array with 0 sample(s) (shape=(0, 40)) while a minimum of 1 is required.

我相信这是我的一部分代码,可以捕获该错误:

def svc_param_selection(X, y, nfolds):
from sklearn import svm

   GridSearchCV = sklearn.model_selection.GridSearchCV
   Cs = [0.001, 0.01, 0.1, 1, 10]
   gammas = [0.001, 0.01, 0.1, 1]
   param_grid = {'C': Cs, 'gamma': gammas}
   grid_search = GridSearchCV(svm.SVC(kernel = 'rbf'), param_grid, cv=nfolds)
   grid_search.fit(X, y)
   return grid_search.best_params_


# Training model
print('Training model')
years = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']
all_files = [data_path + str(year) + '_database.csv' for year in years]
train_df = pd.concat((pd.read_csv(f) for f in all_files), ignore_index=True)
train_df = utils.scrub_data(train_df)

X_train = train_df[feature_names]
Y_train = train_df['result']

params = svc_param_selection(X_train, Y_train, 2)
model = SVC(probability=True, C=params['C'], gamma=params['gamma'])
# model = SVC(probability=True)
model.fit(X_train, Y_train)

print('Accuracy of SVM classifier on training set: {:.2f}'
  .format(model.score(X_train, Y_train)*100))

0 个答案:

没有答案