我寫了下面的代碼:Sklearn預測多路輸出
from sklearn import tree
# Dataset & labels
# Using metric units
# features = [height, weight, style]
styles = ['modern', 'classic']
features = [[1.65, 65, 1],
[1.55, 50, 1],
[1.76, 64, 0],
[1.68, 77, 0] ]
labels = ['Yellow dress', 'Red dress', 'Blue dress', 'Green dress']
# Decision Tree
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
# Returns the dress
height = input('Height: ')
weight = input('Weight: ')
style = input('Modern [0] or Classic [1]: ')
print(clf.predict([[height,weight,style]]))
此代碼接收用戶的身高和體重,然後返回禮服能更好地滿足她。有沒有辦法返回多個選項?例如,返回兩件或更多禮服。
UPDATE
from sklearn import tree
import numpy as np
# Dataset & labels
# features = [height, weight, style]
# styles = ['modern', 'classic']
features = [[1.65, 65, 1],
[1.55, 50, 1],
[1.76, 64, 1],
[1.72, 68, 0],
[1.73, 68, 0],
[1.68, 77, 0]]
labels = ['Yellow dress',
'Red dress',
'Blue dress',
'Green dress',
'Purple dress',
'Orange dress']
# Decision Tree
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
# Returns the dress
height = input('Height: ')
weight = input('Weight: ')
style = input('Modern [0] or Classic [1]: ')
print(clf.predict_proba([[height,weight,style]]))
如果用戶是1.72米和68千克,我想同時顯示綠色和紫色的衣服。這個例子只是返回100%的綠色禮服。
什麼時候會返回一個以上?你的意思是把它們按照最有可能的順序歸還? – erip