python - 如何计算哪个自变量对因变量的影响最大?
问题描述
我有一个包含 5 个自变量和 1 个因变量的数据框。我所有的变量都是连续的,包括因变量。有没有一种方法可以计算我的哪些自变量在 python 中对我的因变量影响最大?有没有我可以运行的算法来为我做这件事?
我尝试了信息增益方法,但这是一种分类方法,因此必须使用标签编码器来转换我的因变量。在将数据集拆分为训练集和测试集后,我使用了以下代码
#encoding the dependant variable
lab_enc = preprocessing.LabelEncoder()
training_scores_encoded = lab_enc.fit_transform(y_train)
#SelectFromModel will select those features which importance is greater than the mean importance of all the features by default, but we can alter this threshold if we want.
#Firstly, I specify the random forest instance, indicating the number of trees.
#Then I use selectFromModel object from sklearn to automatically select the features.
sel = SelectFromModel(RandomForestClassifier(n_estimators = 100))
sel.fit(X_train, training_scores_encoded)
#We can now make a list and count the selected features.
selected_feat= X_train.columns[(sel.get_support())]
len(selected_feat)
#viewing the importances
import matplotlib.pyplot as plt
importances = sel.estimator_.feature_importances_
indices = np.argsort(importances)[::-1]
# X is the train data used to fit the model
plt.figure()
plt.title("Feature importances")
plt.bar(range(X_train.shape[1]), importances[indices],
color="r", align="center")
plt.xticks(range(X_train.shape[1]), indices)
plt.xlim([-1, X_train.shape[1]])
虽然我得到了结果,但我不确定这一点,因为我必须对我的(连续)因变量进行编码。这是正确的方法吗?如果不是,我该怎么办?
预先感谢您的帮助
解决方案
推荐阅读
- angular - p-table 中的primeng p-dropdown,在更改一个下拉列表时会更改表中的所有下拉值
- php - 比较JS中的两个布尔变量
- firefox - Firefox 仅使用 MediaRecorder 记录整个音频输入的开始部分
- javascript - javaScript中有什么方法可以通过我可以在客户端默认文件资源管理器上打开一个文件夹
- swift - 如何使用 Alamofire 检查 API 是否返回任何内容?
- css-animations - Scale() 关键帧在 div 框周围生成大量白线
- android - 如何将 android App Icon 创建为动画或像日历、时钟或天气这样的小部件或系统应用程序?
- yaml - Helm-Charts(yaml):正则表达式被破坏
- sql - Dynamic Pivot Sql Query 显示所有来自一张表
- python - Wired Pandas DataFrame Groupby 应用行为