一、赛题描述
赛题:温室温度预测挑战赛
主办方:科大讯飞+中国农业大学
主页:http://challenge.xfyun.cn/topic/info?type=temperature
背景
随着计算机技术的发展,我国逐渐实现了从传统农业到现代农业的转变,正逐步迈向智慧农业。温室是现代农业技术应用的典型场景,其内部环境具有可操作性,能人为形成适宜植物生长的小型封闭生态系统,提升农产品的产量和质量,因此被广泛应用于农业生产中。在温室的各项环境因子中,作物对温度最为敏感。温度的高低影响植株细胞的酶活性,从而影响作物的生长速度、产量和质量,因此温度对作物生长发育影响极大。为了保证农产品的产量和质量,应保证作物正常生长,需对温室温度进行精确的调控。
任务
温室温度调控需要对温室温度进行精准的预测,本次大赛提供了中国农业大学涿州实验站的温室温度数据作为样本,参赛选手需基于提供的样本构建模型,预测温室温度变化情况。
二、数据集说明
本次比赛为参赛选手提供了温室内外的部分传感器数据,包括温室内的温度、湿度、气压以及温室外的温度、湿度、气压。
本次比赛分为初赛和复赛两个阶段,初赛阶段提供约30天的传感器数据,其中前20天的数据作为训练数据,后10天的数据用于做温度预测;复赛阶段提供约15天的传感器数据,其中前10天的数据作为训练数据,后5天的数据用于做温度预测。
注1:训练集的数据,每1分钟1条数据记录;初赛测试集的数据,每30分钟1条数据记录;复赛测试集的数据,每2小时1条数据记录。
注2:选手不能利用“未来的实际数据”预测“过去的数据”,例如,假设要预测2020/6/18 08:08:08的室内温度,就不能利用这个时间点及这个时间点以后的真实数据进行预测。
特别说明,温室内的湿度和气压以及温室外的温度、湿度和气压会对温室内的温度产生一定的影响。
数据集版权许可协议
GPL2 license
三、解决方案样例
安装
安装传统机器学习开发库。
【版本说明】
本样例运行环境的关键版本信息:
python 3.12.3
lightgbm 3.3.0
xgboost 1.6.0
导入相关系统库
import pandas as pd
import lightgbm as lgb
import xgboost as xgb
from catboost import CatBoostRegressor
from sklearn.linear_model import SGDRegressor, Ridge
from sklearn.preprocessing import MinMaxScaler
import numpy as np
from tqdm import tqdm
from sklearn.metrics import mean_squared_error
import warnings
读入数据集
train_df = pd.read_csv('./data/train.csv')
test_df = pd.read_csv('./data/test.csv')
sub = pd.DataFrame(test_df['time'])
分析数据集概况
train_df.info()
train_df.shape
定义各种模型
def single_model(clf, train_x, train_y, test_x, clf_name, class_num=1):
train = np.zeros((train_x.shape[0], class_num))
test = np.zeros((test_x.shape[0], class_num))
nums = int(train_x.shape[0] * 0.80)
if clf_name in ['sgd','ridge']:
print('MinMaxScaler...')
for col in features:
ss = MinMaxScaler()
ss.fit(np.vstack([train_x[[col]].values, test_x[[col]].values]))
train_x[col] = ss.transform(train_x[[col]].values).flatten()
test_x[col] = ss.transform(test_x[[col]].values).flatten()
trn_x, trn_y, val_x, val_y, test_y = train_x[:nums], train_y[:nums], train_x[nums:], train_y[nums:], test_x[nums:]
if clf_name == "lgb":
train_matrix = clf.Dataset(trn_x, label=trn_y)
valid_matrix = clf.Dataset(val_x, label=val_y)
data_matrix = clf.Dataset(train_x, label=train_y)
params = {
'boosting_type': 'gbdt',
'objective': 'mse',
'min_child_weight': 5,
'num_leaves': 2 ** 8,
'feature_fraction': 0.5,
'bagging_fraction': 0.5,
'bagging_freq': 1,
'learning_rate': 0.001,
'seed': 2020,
'force_col_wise': True
}
model = clf.train(params, train_matrix, 50000, valid_sets=[train_matrix, valid_matrix], verbose_eval=500, early_stopping_rounds=1000)
model2 = clf.train(params, data_matrix, model.best_iteration)
val_pred = model.predict(val_x, num_iteration=model2.best_iteration).reshape(-1, 1)
test_pred = model.predict(test_x, num_iteration=model2.best_iteration).reshape(-1, 1)
if clf_name == "xgb":
train_matrix = clf.DMatrix(trn_x , label=trn_y, missing=np.nan)
valid_matrix = clf.DMatrix(val_x , label=val_y, missing=np.nan)
test_matrix = clf.DMatrix(test_x, label=test_y, missing=np.nan)
params = {'booster': 'gbtree',
'eval_metric': 'mae',
'min_child_weight': 5,
'max_depth': 8,
'subsample': 0.5,
'colsample_bytree': 0.5,
'eta': 0.001,
'seed': 2020,
'nthread': 36
}
watchlist = [(train_matrix, 'train'), (valid_matrix, 'eval')]
model = clf.train(params, train_matrix, num_boost_round=50000, evals=watchlist, verbose_eval=500, early_stopping_rounds=1000)
val_pred = model.predict(valid_matrix, ntree_limit=model.best_ntree_limit).reshape(-1, 1)
test_pred = model.predict(test_matrix , ntree_limit=model.best_ntree_limit).reshape(-1, 1)
if clf_name == "cat":
params = {'learning_rate': 0.001, 'depth': 5, 'l2_leaf_reg': 10, 'bootstrap_type': 'Bernoulli',
'od_type': 'Iter', 'od_wait': 50, 'random_seed': 11, 'allow_writing_files': False}
model = clf(iterations=20000, **params)
model.fit(trn_x, trn_y, eval_set=(val_x, val_y), cat_features=[], use_best_model=True, verbose=500)
val_pred = model.predict(val_x)
test_pred = model.predict(test_x)
if clf_name == "sgd":
params = {
'loss': 'squared_error',
'penalty': 'l2',
'alpha': 0.00001,
'random_state': 2020,
}
model = SGDRegressor(**params)
model.fit(trn_x, trn_y)
val_pred = model.predict(val_x)
test_pred = model.predict(test_x)
if clf_name == "ridge":
params = {
'alpha': 1.0,
'random_state': 2020,
}
model = Ridge(**params)
model.fit(trn_x, trn_y)
val_pred = model.predict(val_x)
test_pred = model.predict(test_x)
print("%s_mse_score:" % clf_name, mean_squared_error(val_y, val_pred))
return val_pred, test_pred
训练模型
使用多种模型进行训练:
features = train_df[:1].drop(drop_columns, axis=1).columns
x_train = train_df[features]
x_test = test_df[features]
y_train = train_df['temperature'].values - train_df['outdoorTemp'].values
lr_train, lr_test = ridge_model(x_train, y_train, x_test)
sgd_train, sgd_test = sgd_model(x_train, y_train, x_test)
lgb_train, lgb_test = lgb_model(x_train, y_train, x_test)
xgb_train, xgb_test = xgb_model(x_train, y_train, x_test)
cat_train, cat_test = cat_model(x_train, y_train, x_test)
源码开源协议
GPL-3.0 license
https://github.com/datawhalechina/competition-baseline/blob/master/LICENSE
怎么下载数据集和代码呢?
淘宝店铺:AI智能案例学习套装/传统机器学习-多行业-A-淘宝网
甲壳虫AI竞赛精选 https://shop293023998.taobao.com/
或点击下载:AiS-ML-Eco-iFLYTEK2024-Environmental-Air-Quality-Assessment.zip