简体   繁体   English

Sklearn预测多个输出

[英]Sklearn predict multiple outputs

I wrote the following code: 我写了以下代码:

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]]))

This code receives the user's height and weight, then returns the dress that better fits to her. 此代码接收用户的身高和体重,然后返回更适合她的衣服。 Is there a way to return multiple options? 有没有办法返回多个选项? For instance, return two or more dresses. 例如,返回两件或更多件连衣裙。

UPDATE 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]]))

If the user is 1.72m and 68kg, I want to show both the green and the purple dresses. 如果用户是1.72米和68公斤,我想要显示绿色和紫色礼服。 This example just returns 100% for the green dress. 这个例子只返回100%的绿色礼服。

Yes you can. 是的你可以。 Actually what you can do is that you can get the probability of each class . 实际上你可以做的是你可以得到每个班级的概率 There is a function called .predict_proba() that is implemented in some classifiers. 有一个名为.predict_proba()的函数在一些分类器中实现。

See here , the documentation of sklearn. 请参阅此处 ,sklearn的文档。

It will return the probability of membership of your sample for each class. 它将返回每个类的样本成员资格的概率。

Then you can for example return the labels associated with the two, three highest probabilities. 然后,您可以返回与两个,三个最高概率相关联的标签。

You can use the predict_proba() method to get probability for every class for a given item. 您可以使用predict_proba()方法获取给定项目的每个类的概率。 For more information, check this for "predict_proba()": 有关更多信息,请选中“predict_proba()”:

http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html

Hope this helps.. 希望这可以帮助..

predict() will return only the class with higher probability. predict()只返回概率较高的类。 If you use predict_proba() instead, it will return an array with the probability for each class, so you can pick the ones above a certain threshold, for instance. 如果改为使用predict_proba() ,它将返回一个数组,其中包含每个类的概率,因此您可以选择高于某个阈值的数组。

Here is the documentation for the method. 是该方法的文档。

You could do something like this with it: 你可以用它做这样的事情:

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]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM