数据处理与分析技术-题库 (2)

更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
请补全以下基于随机森林的交通数据分类任务代码中的缺失部分,完成数据预处理、模型训练与评估流程:

# 1. 导入数据处理库

import (1) ______ as pd

# 2. 读取数据集(csv名为DATASET-B.csv)

data = pd.read_csv( (2) ______ )

# 3. 转换特定列的数据类型为整数

for c in ['rowid', 'colid', 'time_id']:

data[c] = data[c].astype(int)

# 4. 按指定字段排序并重置索引

data = data. (3) ______ (['date', 'rowid', 'colid', 'time_id']).reset_index(drop=True)

# 5. 转换日期格式并提取星期信息

data['datetime'] = pd.to_datetime(data.date, format='%Y%m%d')

data['dayofweek'] = data.datetime.dt. (4) ______

# 6. 随机抽样5000条数据

data = data. (5) ______ (5000, random_state=233)

# 7. 导入数据集划分工具

from sklearn. (6) ______ import train_test_split

# 8. 划分训练集(70%)和测试集(30%)

train, test = train_test_split(data, test_size=0.3, random_state=233)

# 9. 导入随机森林分类器

from sklearn. (7) ______ import RandomForestClassifier

# 10. 初始化随机森林模型

rf = RandomForestClassifier(

n_estimators=256,

max_depth=9,

min_samples_leaf=30,

n_jobs=-1,

random_state=233

)

# 11. 指定特征列

features = [

'rowid', 'colid', 'time_id', 'dayofweek',

'aveSpeed', 'gridAcc', 'volume', 'speed_std', 'stopNum'

]

# 12. 训练模型

rf.fit(train[features], train['labels'])

# 13. 计算训练集精度

rf_train_score = rf.score( (8) ______ , train['labels'])

# 14. 计算测试集精度

rf_test_score = rf.score( (9) ______ , test['labels'])

# 15. 打印模型精度结果

print(f'随机森林模型精度:训练集:{rf_train_score:.3f};测试集:{rf_test_score:.3f}')