我有包含大约 370 个特征的数据,并且我建立了一个随机森林模型来获取重要特征,但是当我绘制时我无法弄清楚要考虑的特征,因为 370 个特征在 x 轴上看起来非常笨拙。
谁能帮我在python中绘制图形,就像varImpPlot()
绘制的图形一样在 R 中绘图。
最佳答案
在 R 中的 randomForest 包中,varImpPlot() 绘制了最重要的前 30 个变量,您可以在 Python 中使用 sklearn help page 中的示例进行同样的操作。 :
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
X, y = make_classification(n_samples=1000,
n_features=370,
n_informative=16,
n_classes=2,
random_state=0)
forest = RandomForestClassifier(random_state=0)
forest.fit(X, y)
为了绘制它,我们可以将重要性分数放入 pd.Series 并绘制前 30 个:importances = pd.Series(forest.feature_importances_,index=X.columns)
importances = importances.sort_values()
importances[-30:].plot.barh()
关于python - 如何在 python 中绘制图形,如 R 中的 varImpPlot() 方法图,用于绘制随机森林中的重要变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46210371/