机器学习算法系列(3)随机森林

本文介绍了随机森林的算法原理,包括森林的概念和随机性的双重特点。在气温预测的案例中,阐述了数据预处理、模型构建和调参的具体流程,强调了特征选择的重要性。随机森林具备解决分类和回归问题、抗过拟合等优点,但也存在过拟合风险、计算成本高和训练时间长等缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、算法原理

随机森林: 森林:多个决策树并行运行;随机:两重随机性,每个决策树特征个数和样本个数按一定比例随机选择,最后投票选出最终结果。
在这里插入图片描述
二、案例:气温预测

#导入库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use("fivethirtyeight")
%matplotlib inline

import warnings
warnings.filterwarnings("ignore")#忽略警告
#读取数据
df = pd.read_csv('temps_extended.csv')
df.head()

在这里插入图片描述

print('数据维度',df.shape)

在这里插入图片描述

df.describe()

在这里插入图片描述
1.数据预处理

(1)标准时间格式

import datetime
years = df["year"]
months = df["month"]
days = df["day"]
dates = [str(int(year)) + "-" + str(int(month)) + "-" + str(int(day)) for year,month,day in zip(years,months,days)]
dates = [datetime.datetime.strptime(date,"%Y-%m-%d") for date in dates]
dates[0:5]

在这里插入图片描述
(2)变量时序图

fig,((ax1,ax2),(ax3,ax4),(ax5,ax6),(ax7,ax8)) = plt.subplots(4,2,figsize = (15,20))
fig.autofmt_xdate(rotation = 45 )

ax1.plot(dates,df["actual"])
ax1.set_xlabel("");ax1.set_ylabel("Temperature");ax1.set_title("Max Temp")

ax2.plot(dates,df["ws_1"])
ax2.set_xlabel("");ax2.set_ylabel("Temperature");ax2.set_title("Previous Wind Speed")

ax3.plot(dates,df["prcp_1"])
ax3.set_xlabel("");ax3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值