python怎么实现随机森林
发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,这篇文章主要介绍了python怎么实现随机森林的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python怎么实现随机森林文章都会有所收获,下面我们一起来看看吧。背景介绍随
千家信息网最后更新 2024年09月21日python怎么实现随机森林
这篇文章主要介绍了python怎么实现随机森林的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python怎么实现随机森林文章都会有所收获,下面我们一起来看看吧。
背景介绍
随机森林是一组决策树的商标术语。在随机森林中,我们收集了决策树(也称为"森林")。为了基于属性对新对象进行分类,每棵树都有一个分类,我们称该树对该类"投票"。森林选择投票最多的类别(在森林中的所有树木上)。
每棵树的种植和生长如下:
如果训练集中的案例数为N,则随机抽取N个案例样本,但要进行替换。 该样本将成为树木生长的训练集。
如果有M个输入变量,则指定数字m << M,以便在每个节点上从M个中随机选择m个变量,并使用对这m个变量的最佳分割来分割节点。在森林生长期间,m的值保持恒定。
每棵树都尽可能地生长。没有修剪。
入门示例
python代码实现:
'''
The following code is for the Random Forest
Created by - ANALYTICS VIDHYA
'''
# importing required libraries
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# read the train and test dataset
train_data = pd.read_csv('train-data.csv')
test_data = pd.read_csv('test-data.csv')
# view the top 3 rows of the dataset
print(train_data.head(3))
# shape of the dataset
print('\nShape of training data :',train_data.shape)
print('\nShape of testing data :',test_data.shape)
# Now, we need to predict the missing
# target variable in the test data
# target variable - Survived
# seperate the independent and target variable on training data
train_x = train_data.drop(columns=['Survived'],axis=1)
train_y = train_data['Survived']
# seperate the independent and target variable on testing data
test_x = test_data.drop(columns=['Survived'],axis=1)
test_y = test_data['Survived']
'''
Create the object of the Random Forest model
You can also add other parameters and test your code here
Some parameters are : n_estimators and max_depth
Documentation of sklearn RandomForestClassifier:
https://scikit-learn.org/stable/modules/
generated/sklearn.ensemble.RandomForestClassifier.html
'''
model = RandomForestClassifier()
# fit the model with the training data
model.fit(train_x,train_y)
# number of trees used
print('Number of Trees used : ', model.n_estimators)
# predict the target on the train dataset
predict_train = model.predict(train_x)
print('\nTarget on train data',predict_train)
# Accuray Score on train dataset
accuracy_train = accuracy_score(train_y,predict_train)
print('\naccuracy_score on train dataset : ', accuracy_train)
# predict the target on the test dataset
predict_test = model.predict(test_x)
print('\nTarget on test data',predict_test)
# Accuracy Score on test dataset
accuracy_test = accuracy_score(test_y,predict_test)
print('\naccuracy_score on test dataset : ', accuracy_test)
运行结果:
Shape of training data : (712, 25)
Shape of testing data : (179, 25)
Number of Trees used : 10
Target on train data [0 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0
1 0 0 0 1 0 1 0 1 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 0 1 0 01 0 0 0 0 0
0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 0 1 0 0 1 0 0 0 1 1 0 0 1 0 1 11 0 1 0 0 0
0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 00 1 0 1 1 0
0 0 0 1 1 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 0 0 00 0 1 0 0 1
0 1 1 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 0 1 1 0 00 1 0 0 0 0
0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 01 0 0 0 1 0
0 1 1 0 1 1 1 0 1 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 1 0 0 1 10 0 0 0 0 0
0 0 1 1 0 1 1 0 1 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 00 0 0 0 0 1
1 0 0 1 1 0 1 0 0 0 1 0 1 0 0 0 1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 0 1 1 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 0]
accuracy_score on test dataset : 0.8156424581005587
关于"python怎么实现随机森林"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"python怎么实现随机森林"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
森林
变量
知识
内容
树木
样本
篇文章
节点
决策
分类
投票
训练
选择
恒定
代码
价值
商标
对象
尽可能
属性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
监控服务器需要怎么设置
绝地手游无法连接服务器
日照市网络安全和信息化委员会
软件开发常见比赛
网络技术中什么是物联网
怎样写网络安全评估报告
农行 天津 软件开发
魔兽正式服数据库2020
有关网络安全的事故警示视频
软件开发 链
山东服务器电源要多少钱
计算机网络技术女孩子的出路
有什么好的软件开发客户
数据库 批量写
刑事侦查和网络安全执法
软件开发 五行
福建服务器机房厂家现货云主机
上海弈兴网络技术
如何搭建我的世界服务器
pubg体验服服务器连接失败
广东送货单软件开发
福彩的服务器
数据库数据标准bim
怎么做一个带简易数据库的软件
战仙服务器
服务器主机选择
延庆区智能网络技术诚信经营
360网神防火墙是服务器吗
保障工程建设信息网络安全
网络技术作业答案