python - 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 时,生成的训练集将为空。调整任何上述参数。
解决方案
train_test_split
将矩阵/数组作为输入。换句话说,输入列表必须是二维矩阵。由于您的输入是一维列表,train_test_split
因此会将整个列表视为一个元素,因此会出现错误。
我尚未对此进行测试,但请尝试以下操作:
for feature, label in data:
features.append(list(feature))
labels.append(list(label))
推荐阅读
- python - 在 python def 参数中传递部分数据框名称
- python - 如何在 SQLAlchemy 中获取关系列的数据?
- amazon-web-services - AWS MSK Kafka 集群在最大大小更改后不会自动扩展
- java - 本机查询 mapstruct
- indexing - 使用 solr 8.9 在索引的所有字段上搜索单词
- android - 什么是读取 Sqlite 数据库时出错:数据库 'LiveSqliteDatabaseId>?
- python - 输出中没有 CSV 文件
- android - Gradle 不会一次性同步库
- flutter - 我想使用疫苗的图标,但添加代码时它不起作用
- java - 如何选择要上传的 PDF 文件