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%的绿色礼服.
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)
| 归档时间: |
|
| 查看次数: |
2070 次 |
| 最近记录: |