有可能量化变量的重要性,以弄清观察结果落入一类的可能性。类似于Logistic回归。
例如: 如果我有以下自变量 1)该人拥有的猫数 2)一个人有多少只狗 3)一个人的鸡只数量
我的因变量是:一个人是否是PETA的一部分
是否可以这样说:“如果该人收养的猫多于其现有动物的范围,那么他成为PETA的几率将增加0.12”
我目前正在使用以下方法来实现此特定方案: 1)使用训练数据建立随机森林模型 2)预测客户落入特定类别的概率(Peta与非Peta) 3)人为地将每个观测所拥有的猫的数量增加1 4)预测客户落入两个类别之一的新概率 5)(4)的概率与(2)的概率之间的平均变化是一个人收养猫的概率的平均增加。
这有意义吗?我没有想到的方法上有什么缺陷吗?有没有更好的方法可以做到这一点?
答案 0 :(得分:1)
如果您使用scikitlearn,则可以通过访问已拟合的RandomForestClassifier的feature_importance_
属性来轻松实现此目的。根据SciKitLearn:
用作决策节点的要素的相对等级(即深度) 一棵树可用于评估该功能的相对重要性 关于目标变量的可预测性。特征 在树的顶部使用有助于最终预测 决定更大比例的输入样本。预期的 因此,它们贡献的样本的一部分可以用作 估计功能的相对重要性。 通过平均 那些在几棵随机树上的预期活动率可以 减少这种估计的方差并将其用于特征 选择。
属性feature_importance_
存储树中每个要素的平均深度。
这是一个例子。让我们从导入必要的库开始。
# using this for some array manipulations
import numpy as np
# of course we're going to plot stuff!
import matplotlib.pyplot as plt
# dummy iris dataset
from sklearn.datasets import load_iris
#random forest classifier
from sklearn.ensemble import RandomForestClassifier
一旦有了这些,我们将加载虚拟数据集,定义分类模型并将数据拟合到模型中。
data = load_iris()
# we're gonna use 100 trees
forest = RandomForestClassifier(n_estimators = 100)
# fit data to model by passing features and labels
forest.fit(data.data, data.target)
现在,我们可以根据要素能够将数据分类到不同目标的良好程度,使用要素重要性属性来为其评分。
# find importances of each feature
importances = forest.feature_importances_
# find the standard dev of each feature to assess the spread
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
# find sorting indices of importances (descending)
indices = np.argsort(importances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in range(data.data.shape[1]):
print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
功能排名: 1.功能2(0.441183) 2.功能3(0.416197) 3.功能0(0.112287) 4.功能1(0.030334)
现在,我们可以将每个功能的重要性绘制为条形图,并确定是否值得保留所有功能。我们还绘制了误差线以评估其重要性。
plt.figure()
plt.title("Feature importances")
plt.bar(range(data.data.shape[1]), importances[indices],
color="b", yerr=std[indices], align="center")
plt.xticks(range(data.data.shape[1]), indices)
plt.xlim([-1, data.data.shape[1]])
plt.show()
答案 1 :(得分:0)
很抱歉。在您提到要发表什么样的陈述时,我没有注意到。我假设您的响应变量为1或零。您可以尝试这样的事情:
也可以通过逻辑回归进行尝试。它实际上取决于您的数据及其分布方式,以找到哪种回归效果最好。您绝对必须使用回归来找到输入变化带来的概率变化。
您甚至可以尝试使用具有回归/线性输出层的单层神经网络来执行相同的操作。如果数据不太可能是线性的,则添加图层或非线性激活函数。
干杯!