一、简介
这是一个使用天气数据集,建立随机森林模型,对数据集进行训练,从而对天气最高温度进行预测,
步骤:
1、载入数据
2、数据预处理
3、划分数据集为训练数据集,测试数据集
4、建立模型
5、预测,调优
二、具体步骤,
1、载入数据:
import pandas as pd
# 载入数据
features = pd.read_csv('data/temps.csv')
# 数据前五行
features.head(5)
# 数据特征
print('The shape of our features is:', features.shape)
features.describe()
数据前五行:
2、数据预处理与数据集切分:
# one-hot 编码
features = pd.get_dummies(features)
features.head(5)
# 标签与数据划分
import numpy as np
# 标签
labels = np.array(features['actual'])
# 数据
features= features.drop('actual', axis = 1)
# 转变成列表
feature_list = list(features.columns)
# 转变成np.array格式
features = np.array(features)
# 训练集与测试集划分
from sklearn.model_selection import train_test_split
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size = 0.25,
random_state = 42)
3、建立随机森林模型
from sklearn.ensemble import RandomForestRegressor
# 模型建立
rf = RandomForestRegressor(n_estimators= 1000, random_state=42)
# 训练
rf.fit(train_features, train_labels)
4、预测
# 使用随机森林带的预测方法进行预测
predictions = rf.predict(test_features)
# 计算绝对误差
errors = abs(predictions - test_labels)
# 绝对误差,保留两位小数
print('Mean Absolute Error:', round(np.mean(errors), 2), 'degrees.')
# 计算 平均绝对百分误差mean absolute percentage error (MAPE)
mape = 100 * (errors / test_labels)
# 准确率,这里使用了平均绝对百分误差来计算准确率
accuracy = 100 - np.mean(mape)
print('Accuracy:', round(accuracy, 2), '%.')
计算结果是:
Accuracy: 93.99 %.
5、特征重要性:
# 数字特征重要性
importances = list(rf.feature_importances_)
feature_importances = [(feature, round(importance, 2)) for feature, importance in zip(feature_list, importances)]
# 排序
feature_importances = sorted(feature_importances, key = lambda x: x[1], reverse = True)
[print('Variable: {:20} Importance: {}'.format(*pair)) for pair in feature_importances];
结果:
从而可知最重要的特征是temp1,接下来我们根据此特征重新构建模型
6、使用最重要的特征构建模型
# 使用最重要的两个特征构建模型
rf_most_important = RandomForestRegressor(n_estimators= 1000, random_state=42)
# 获取这两个最重要的特征,划分数据集
important_indices = [feature_list.index('temp_1'), feature_list.index('average')]
train_important = train_features[:, important_indices]
test_important = test_features[:, important_indices]
# 训练
rf_most_important.fit(train_important, train_labels)
# 预测
predictions = rf_most_important.predict(test_important)
# 误差
errors = abs(predictions - test_labels)
print('Mean Absolute Error:', round(np.mean(errors), 2), 'degrees.')
mape = np.mean(100 * (errors / test_labels))
accuracy = 100 - mape
# 准确率
print('Accuracy:', round(accuracy, 2), '%.')
结果是:
Mean Absolute Error: 3.9 degrees.
Accuracy: 93.8 %.
可见,虽然使用了最重要的两个特征来构建模型,但准确率反而略微下降了一些
构建随机森林完整代码:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
# 载入数据,one-hot化
original_features = pd.read_csv('data/temps.csv')
original_features = pd.get_dummies(original_features)
# 标签
original_labels = np.array(original_features['actual'])
# 去除标签值
original_features= original_features.drop('actual', axis = 1)
# 特征
original_feature_list = list(original_features.columns)
# 转变成numpy.array格式
original_features = np.array(original_features)
# 切分数据集
original_train_features, original_test_features, original_train_labels, original_test_labels = train_test_split(original_features, original_labels, test_size = 0.25, random_state = 42)
# 模型建立
rf = RandomForestRegressor(n_estimators= 1000, random_state=42)
# 模型训练
rf.fit(original_train_features, original_train_labels);
# 预测
predictions = rf.predict(original_test_features)
# 计算误差
errors = abs(predictions - original_test_labels)
# 计算mean absolute error (mae)
print('Average model error:', round(np.mean(errors), 2), 'degrees.')
# 计算mean absolute percentage error (MAPE)
mape = 100 * (errors / original_test_labels)
# 计算准确率
accuracy = 100 - np.mean(mape)
print('Accuracy:', round(accuracy, 2), '%.')