簡體   English   中英

ValueError:n_samples=1,test_size=0.5 和 train_size=None

[英]ValueError: With n_samples=1, test_size=0.5 and 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM