|
- import os
- import scipy.io as scio
- import pandas as pd
- import numpy as np
- import lightgbm as lgb
- from sklearn.metrics import mean_absolute_error
-
- from os.path import join as osp_join
-
-
- def create_dataset(data_dir=r"F:\波形数据\dataset", mode='train'):
- dataset_dir = osp_join(data_dir, mode)
- id_list = [int(id_) for id_ in os.listdir(dataset_dir)]
- sample_time = 1024
- cal_pts = [1, 2, 4, 6, 7, 8, 10, 12]
- sample_pts_idx = [p - 1 for p in cal_pts]
- n = len(id_list)
- num_pts = len(sample_pts_idx)
- size = n * num_pts
- input_feature = np.zeros(shape=(n * num_pts, 10), dtype=np.float32)
- output_tp = np.zeros(shape=(n * num_pts, sample_time), dtype=np.float32)
-
- air_all_mat_path = osp_join(data_dir, r"..\rawdata_air_all")
- air_all_mat = scio.loadmat(air_all_mat_path)
-
- build_all_mat_path = osp_join(data_dir, r"..\rawdata_build_all")
- build_all_mat = scio.loadmat(build_all_mat_path)
-
- # 正压冲量Imp, 超压峰值Pre, 爆炸当量Q, 爆心距离R
- Imp, Pre, Q, R = air_all_mat['Imp'], air_all_mat['Pre'], air_all_mat['Q'], air_all_mat['R']
-
- # 建筑长度l, 建筑长度方向的街道宽度sl,
- # 建筑宽度方向的街道宽度sw, 街道宽度w
- l, sl, sw, w = build_all_mat['l'], build_all_mat['sl'], build_all_mat['sw'], build_all_mat['w']
- global_feature = np.concatenate([Imp / 1e5, Pre / 1e5, Q / 1e2, R / 1e2,
- l / 1e1, sl / 1e1, sw / 1e1, w / 1e1], axis=1).astype(np.float32)
- record = 0
- for i, data_id in enumerate(id_list):
- id_dir = osp_join(dataset_dir, str(data_id))
- build_mat_path = osp_join(id_dir, "build.mat")
- build_mat = scio.loadmat(build_mat_path)
- mat_id = data_id - 1 if data_id < 1395 else data_id - 2
-
- node_coor = build_mat['node_coor'].astype(np.float32)
-
- time = np.shape(build_mat['DATP'])[0]
-
- # 对输出曲线进行采样
- sample_list = [int(time / sample_time * i)
- for i in range(sample_time)]
- sampled_tp = np.take(build_mat['DATP'], sample_list, axis=0) / 1e5
- # sampled_tp = np.take(build_mat['DATP'], sample_list, axis=0) / 1e3
- sampled_tp = sampled_tp.transpose()
-
- for p in sample_pts_idx:
- input_feature[record, :] = np.concatenate([global_feature[mat_id, :], node_coor[p, :]], axis=0)
- output_tp[record, :] = sampled_tp[p, :].transpose().astype(np.float32)
- record += 1
- print(f"{mode} dataset size :{size}")
- return input_feature, output_tp
-
-
- train_X, train_Y = create_dataset(r"C:\Users\86133\Desktop\24\波形数据\dataset",mode='train')
- test_X, test_Y = create_dataset(r"C:\Users\86133\Desktop\24\波形数据\dataset",mode='test')
-
-
- # 调用LightGBM模型,使用训练集数据进行训练(拟合)
- # Add verbosity=2 to print messages while running boosting
- my_model = lgb.LGBMRegressor(objective='regression', num_leaves=31, learning_rate=0.05, n_estimators=20,
- verbosity=2)
- my_model.fit(train_X, train_Y)
-
- # 6.使用模型对测试集数据进行预测
- predictions = my_model.predict(test_X)
-
- # 7.对模型的预测结果进行评判(平均绝对误差)
- print("Mean Absolute Error : " + str(mean_absolute_error(predictions, test_Y)))
|