我正在开发一个机器学习程序,但我坚持此错误。 目前,我的数据集有2个类,如下所示:
2652,0.09,-1.02,0.43,-0.01,-0.94,0.35,1
1,0.38,-0.90,0.19,0.30,0.95,0.12,2
2653,0.09,-1.02,0.43,-0.01,-0.94,0.35,1
4,0.38,-0.90,0.19,0.29,0.96,0.06,2
5,0.38,-0.90,0.19,0.29,0.96,0.06,2
2654,0.15,-1.01,0.45,-0.01,-0.94,0.35,1
2,0.38,-0.90,0.19,0.29,0.96,0.06,2
当我运行代码时,出现此错误
ValueError Traceback (most recent call last)
<ipython-input-7-c44a67b01cf1> in <module>
11 model, params = train_model(X_train, y_train,
12 est=SVC(probability=True),
---> 13 grid={'C': param_range, 'gamma': param_range, 'kernel': ['linear']})
14 eval_model(model, X_test, y_test, 'SVC')
15
<ipython-input-5-d902442b6ba1> in train_model(X, y, est, grid)
2 print('::::Train Model::::')
3 gs = GridSearchCV(estimator=est, param_grid=grid, scoring='accuracy', cv=4, n_jobs=-1)
----> 4 gs = gs.fit(X, y)
5
6 return (gs.best_estimator_, gs.best_params_)
.
.
.
ValueError: The number of classes has to be greater than one; got 1 class
但是我已经意识到在这部分代码中
feats, y = get_simple_features(data, wsize='10s')
# split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(feats, y, test_size=.25, random_state=0, stratify=y)
print('Support Vector Machine')
model, params = train_model(X_train, y_train,
est=SVC(probability=True),
grid={'C': param_range, 'gamma': param_range, 'kernel': ['linear']})
eval_model(model, X_test, y_test, 'SVC')
当我执行print(np.unique(y))
时,输出为[1]。
它发生在以下代码行中:
y = data['label'].resample(wsize, how=lambda ts: mode(ts)[0] if ts.shape[0] > 0 else np.nan)
因为data ['label']具有两个类,但是重新采样的结果只有1个类。 但是,我已经要求另一个人来运行我的代码,并且完全没有错误。
那会是什么?
PS:Here是完整的代码。
答案 0 :(得分:0)
这是由于运行resample
函数时所进行的重采样是随机的,特别是由于样本量太小(<10)并且不是分层抽样,您很可能会得到仅代表一个类的样本。