栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python随机森林代码

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python随机森林代码

from time import time
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.metrics import mean_squared_error

data = np.loadtxt('C:/Users/Administrator.DESKTOP-BT0S13O/Desktop/数据1.csv',delimiter=',')     
(括号内是路径文件)

RON = data[:,0]
factors = data[:,1:]
t0 = time()
forest= RandomForestRegressor(n_estimators=500,random_state=0,max_features=100,n_jobs=2)

X_train,X_test,y_train,y_test = train_test_split(factors,RON,test_size=0.2,shuffle=True,random_state=0)
#print("done in %0.3fs"(time()- t0))
forest.fit(X_train,y_train)
y_pred = forest.predict(X_train)

mse = mean_squared_error(y_train,y_pred)
print(mse)
print("Traing Score:%f" %forest.score(X_train, y_train))
print("Testing Score:%f" %forest.score(X_test, y_test))

importances = forest.feature_importances_ 
print('每个维度对应的重要性因子:n',importances)
print(sorted(importances,reverse=True))
np.argsort(-importances)
print(sorted(importances*100,reverse=True))
indices = np.argsort(importances)[::-1]# a[::-1]让a逆序输出
print('得到按维度重要性因子排序的维度的序号:n',indices)
most_import = indices[:100]#取最重要的100个
print(X_train,most_import)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339860.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号