Scikit-Learn决策树:预测为a或b的概率?

我有一个使用Scikit-Learn的基本决策树分类器:

#Used to determine men from women based on height and shoe sizefrom sklearn import tree#height and shoe sizeX = [[65,9],[67,7],[70,11],[62,6],[60,7],[72,13],[66,10],[67,7.5]]Y=["male","female","male","female","female","male","male","female"]#creating a decision treeclf = tree.DecisionTreeClassifier()#fitting the data to the treeclf.fit(X, Y)#predicting the gender based on a predictionprediction = clf.predict([68,9])#print the predicted genderprint(prediction)

当我运行程序时,它总是输出“male”或“female”,但我如何才能看到预测为男性或女性的概率呢?例如,上面的预测返回“male”,但我如何让它打印出预测为男性的概率呢?

谢谢!


回答:

你可以像下面这样做:

from sklearn import tree#load dataX = [[65,9],[67,7],[70,11],[62,6],[60,7],[72,13],[66,10],[67,7.5]]Y=["male","female","male","female","female","male","male","female"]#build modelclf = tree.DecisionTreeClassifier()#fitclf.fit(X, Y)#predictprediction = clf.predict([[68,9],[66,9]])#probabilitiesprobs = clf.predict_proba([[68,9],[66,9]])#print the predicted genderprint(prediction)print(probs)

理论

clf.predict_proba(X)的结果是:叶子节点中同一类样本的比例,即预测的类概率。

结果的解释:

第一个print返回['male' 'male'],所以数据[[68,9],[66,9]]被预测为males

第二个print返回:

[[ 0. 1.] [ 0. 1.]]

这意味着数据被预测为男性,并且由第二列的1表示。

要查看类的顺序,请使用:clf.classes_

这将返回:['female', 'male']

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注