Sklearn预测多个输出

bod*_*ruk 3 python python-3.x scikit-learn

我写了以下代码:

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]]))
Run Code Online (Sandbox Code Playgroud)

此代码接收用户的身高和体重,然后返回更适合她的衣服.有没有办法返回多个选项?例如,返回两件或更多件连衣裙.

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]]))
Run Code Online (Sandbox Code Playgroud)

如果用户是1.72米和68公斤,我想要显示绿色和紫色礼服.这个例子只返回100%的绿色礼服.

MMF*_*MMF 5

是的你可以.实际上你可以做的是你可以得到每个班级的概率..predict_proba()在一些分类器中实现了一个被调用的函数.

请参阅此处,sklearn的文档.

它将返回每个类的样本成员资格的概率.

然后,您可以返回与两个,三个最高概率相关联的标签.


Art*_*ara 5

predict()将仅返回概率较高的类。如果您 predict_proba()改为使用,它将返回一个包含每个类别的概率的数组,因此您可以选择高于特定阈值的类别。

是该方法的文档。

你可以用它做这样的事情:

probs = clf.predict_proba([[height, weight, style]])
threshold = 0.25 # change this accordingly
for index, prob in enumerate(probs[0]):
    if prob > threshold:
        print(styles[index]) 
Run Code Online (Sandbox Code Playgroud)