首页 > 解决方案 > ValueError:n_samples=1,test_size=0.5 和 train_size=None

问题描述

我有支持向量机的代码

pick_in = open('data.pickle','rb')
data = pickle.load(pick_in)
pick_in.close()      

print(len(data))

features = []
labels = []

for feature, label in data:
    features.append(feature)
    labels.append(label)
    
xtrain, xtest, ytrain, ytest = train_test_split(features, labels, test_size= 0.50)

model = SVC(C=1, kernel='poly', gamma='auto')
model.fit(xtrain, ytrain)

prediction = model.predict(xtest)
accuracy = model.score(xtest,ytest)

categories = ['C','NC']

print('Accuracy:', accuracy)
print('Prediction is:',categories[prediction[0]])

myphoto=xtest[0].reshape(224,224)
plt.imshow(myphoto,cmap='gray')
plt.show()

我正在使用每个类别的 20 张图像开始。但是,我收到此错误:

ValueError:当 n_samples=1、test_size=0.5 和 train_size=None 时,生成的训练集将为空。调整任何上述参数。

标签: python

解决方案


train_test_split将矩阵/数组作为输入。换句话说,输入列表必须是二维矩阵。由于您的输入是一维列表,train_test_split因此会将整个列表视为一个元素,因此会出现错误。

我尚未对此进行测试,但请尝试以下操作:

 for feature, label in data:
    features.append(list(feature))
    labels.append(list(label))

推荐阅读