类的数量必须大于一;得了1分

问题描述 投票:-1回答:1

我正在开发一个机器学习程序,我坚持这个错误。目前我的数据集有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是完整的代码。

class machine-learning scikit-learn
1个回答
0
投票

这是因为在运行resample函数时你正在进行的重采样的随机性,特别是因为样本量太小(<10)并且它不是分层采样,你很可能得到一个只代表一个类的样本。

© www.soinside.com 2019 - 2024. All rights reserved.