基于模糊神经网络的能源消费预测模型及应用
基于模糊神经网络的能源消费预测模型及应用
1. 引言
能源是国民经济发展的重要物质基础,准确预测能源消费趋势对于制定能源政策、优化能源结构、保障能源安全具有重要意义。近年来,随着机器学习技术的快速发展,模糊神经网络作为一种强大的非线性建模工具,在能源消费预测领域得到了广泛应用。
2. 数据预处理
本文使用的数据集包含了某地区多年的能源消费数据,包括煤炭、石油、天然气和其他能源的消费量。首先,我们对数据进行预处理,包括:
- 特征工程: 使用LCC (Lagged Change in Consumption) 方法构建特征矩阵,即使用相邻年份的消费量之差作为特征。* 数据归一化: 使用MinMaxScaler对特征数据进行归一化,将数据缩放到0到1之间,消除不同特征之间量纲的影响。pythonimport numpy as npimport pandas as pdfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.model_selection import train_test_split
成分数据矩阵data = np.array([[0.758, 0.171, 0.049, 0.022], [0.758, 0.172, 0.047, 0.023], # ... 省略部分数据 ... [0.562, 0.179, 0.175, 0.084]])
转换为特征矩阵(LCC方法)feature_matrix = np.zeros((len(data) - 1, len(data[0])))for i in range(len(data) - 1): feature_matrix[i] = data[i + 1] - data[i]
构建特征矩阵的DataFramedf = pd.DataFrame(feature_matrix, columns=['Coal', 'Petroleum', 'Others', 'Gas'])
目标变量target = data[1:, 1] # 使用第二列作为目标变量
数据归一化scaler = MinMaxScaler()df_scaled = scaler.fit_transform(df)
划分训练集和测试集X_train, X_test, y_train, y_test = train_test_split(df_scaled, target, test_size=0.13, random_state=42)
3. TSK模糊神经网络模型
本文采用Takagi-Sugeno-Kang (TSK)模糊神经网络模型进行能源消费预测。TSK模糊系统是一种基于规则的模糊模型,其规则库由一系列'If-Then'规则组成,每条规则对应一个线性函数。模型的输出是所有规则输出的加权平均值,权重由模糊隶属度函数确定。pythonimport skfuzzy as fuzz
class TSK_FS(): def init(self, n_cluster=20, C=0.1): self.n_cluster = n_cluster self.lamda = C self.trained = False
def fit(self, X_train, y_train): n_samples, n_features = X_train.shape n_cluster = self.n_cluster assert (n_samples == len(y_train)), 'X_train and y_train samples num must be same' centers, delta = self.__fcm__(X_train, n_cluster) self.centers = centers self.delta = delta xg = self.__gaussian_feature__(X_train, centers, delta) xg1 = np.dot(xg.T, xg) pg = np.linalg.pinv(xg1 + self.lamda * np.eye(xg1.shape[0])).dot(xg.T).dot(y_train) self.pg = pg self.trained = True
def predict(self, X_test): assert(self.trained), 'Error when predict, use fit first!' xg_test = self.__gaussian_feature__(X_test, self.centers, self.delta) y_pred = xg_test.dot(self.pg) return y_pred
def fcm(self, data, n_cluster): return self.__fcm__(data, n_cluster)
def gaussian_feature(self, data, centers, delta): return self.__gaussian_feature__(data, centers, delta)
def __fcm__(self, data, n_cluster): n_samples, n_features = data.shape centers, mem, _, _, _, _, _ = fuzz.cmeans( data.T, n_cluster, 2.0, error=1e-5, maxiter=200) delta = np.zeros([n_cluster, n_features]) for i in range(n_cluster): d = (data - centers[i, :]) ** 2 delta[i, :] = np.sum(d * mem[i, :].reshape(-1, 1), axis=0) / np.sum(mem[i, :]) return centers, delta
def __gaussian_feature__(self, data, centers, delta): n_cluster = self.n_cluster n_samples = data.shape[0] mu_a = np.zeros([n_samples, n_cluster]) for i in range(n_cluster): tmp_k = 0 - np.sum((data - centers[i, :]) ** 2 / delta[i, :], axis=1) mu_a[:, i] = np.exp(tmp_k) mu_a = mu_a / np.sum(mu_a, axis=1, keepdims=True) data_1 = np.concatenate((data, np.ones([n_samples, 1])), axis=1) zt = [] for i in range(n_cluster): zt.append(data_1 * mu_a[:, i].reshape(-1, 1)) data_fs = np.concatenate(zt, axis=1) data_fs = np.where(data_fs != data_fs, 1e-5, data_fs) return data_fs
4. 模型训练与预测
将预处理后的数据输入到TSK模糊神经网络模型中进行训练,并使用训练好的模型对测试集进行预测。python# 创建TSK模型对象model = TSK_FS(n_cluster=20, C=0.1)# 拟合模型model.fit(X_train, y_train)# 在测试集上进行预测y_pred = model.predict(X_test)
5. 模型评估
使用多种指标对模型的预测性能进行评估,包括均方根误差(RMSE)、平均绝对百分比误差(MAPE)和平均绝对误差(MAE)等。pythonfrom sklearn.metrics import mean_squared_error, r2_score
计算RMSErmse = np.sqrt(mean_squared_error(y_test, y_pred))print('RMSE:', rmse)
计算MAPEmape = np.mean(np.abs((y_test - y_pred) / y_test)) * 100print('MAPE:', mape)
计算MAEmae = np.mean(np.abs(y_test - y_pred))print('MAE:', mae)
6. 未来预测与置信区间
使用训练好的模型对未来十年的能源消费进行预测,并计算预测结果的置信区间。python# 构建未来10年的特征矩阵future_features = np.zeros((10, len(data[0])-1))current_data = data[-1, :-1]
根据模型预测未来十年的特征for i in range(10): feature = model.predict(current_data.reshape(1, -1)) future_features[i] = feature current_data = np.concatenate((current_data[1:], feature))
归一化未来的特征数据future_scaled = scaler.transform(future_features.reshape(-1, len(data[0])-1))
使用TSK_FLS模型预测未来十年的目标变量future_pred = model.predict(future_scaled)
计算置信区间residuals = y_test - y_predmean_residuals = np.mean(residuals)std_residuals = np.std(residuals)n_samples = len(future_pred)z_score = 1.96 # 对应于95%的置信区间margin_of_error = z_score * std_residuals / np.sqrt(n_samples)
lower_bound = future_pred - margin_of_errorupper_bound = future_pred + margin_of_error
输出未来十年的预测置信区间print('未来十年的预测置信区间:')for i in range(len(future_pred)): print(f'年份: {i+1}, 下界: {lower_bound[i]}, 上界: {upper_bound[i]}')
7. 结论
本文介绍了一种基于TSK模糊神经网络的能源消费预测模型,并通过实例验证了模型的有效性。该模型能够有效地学习历史数据中的非线性关系,并对未来能源消费进行预测,为能源规划和决策提供参考。需要注意的是,预测结果受多种因素影响,实际应用中应结合其他信息进行综合分析。
原文地址: https://www.cveoy.top/t/topic/bcBH 著作权归作者所有。请勿转载和采集!