我如何更改功能重要性,使其不再是随机的?在Python中

发布于 2025-01-13 02:18:36 字数 741 浏览 0 评论 0原文

我正在使用基于决策树的特征重要性进行特征选择。

#Feature importance (source: https://towardsdatascience.com/feature-selection-techniques-in-machine-learning-with-python-f24e7da3f36e)
X = df.iloc[:,1:31]  #independent columns
y = df.iloc[:,0]    #target column = diagnosis

model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers

feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh', color="lightskyblue")
plt.show()

但每次我再次启动时结果都会改变,可能某个地方有一个随机数。有没有办法设置一次然后保持不变,以便每次重新启动内核时结果都不会改变?

谢谢。

更新:我想我使用以下方法来管理它: 模型 = ExtraTreesClassifier(random_state=1) 这是正确的做法吗?

I am doing feature selection using feature importance which is based on a decision tree.

#Feature importance (source: https://towardsdatascience.com/feature-selection-techniques-in-machine-learning-with-python-f24e7da3f36e)
X = df.iloc[:,1:31]  #independent columns
y = df.iloc[:,0]    #target column = diagnosis

model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers

feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh', color="lightskyblue")
plt.show()

But the results change everytime i start it again, probably somewhere there is a random number somewhere. Is there a way to set this once and then keep it the same so that the results don't change everytime the kernel is restarted?

Thank you.

UPDATE: I think I managed it using:
model = ExtraTreesClassifier(random_state=1)
is that the right way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-20 02:18:36

您可以将 ExtraTreesClassifierrandom_state 参数设置为某个值。在这种情况下,您将获得可重复的结果。
例如:

model = ExtraTreesClassifier(random_state=0)

You can set a random_state parameter of ExtraTreesClassifier to some value. In this case you will get reproducible results.
For example:

model = ExtraTreesClassifier(random_state=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文