#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
import tensorflow as tf
os.environ["CUDA_VISIBLE_DEVICES"] = '3' #use GPU with ID=0
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# In[2]:
###### import numpy as np
import pandas as pd
import keras
# import tensorflow as tf
from datetime import datetime
from sklearn import datasets, linear_model
from matplotlib import pyplot as plt
from matplotlib import cm
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.layers import BatchNormalization, Input, Embedding, Concatenate, Conv1D, MaxPooling1D, Flatten, merge
from keras.layers import merge, Concatenate, Permute, RepeatVector, Reshape,Multiply
from keras.models import Sequential, Model
import keras.backend as K
import statsmodels.formula.api as smf
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
# prevent tensorflow from allocating the entire GPU memory at once
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
# In[3]:
import pandas as pd
import math
import keras
import numpy as np
# In[4]:
data=pd.read_csv('station_all.csv')
# print(data.head)
# In[5]:
station=data.iloc[:,11:]#天气+十天发电量
trend=data['trend']
std=data['std']
train=data['detrended']#y_train
# print(train)
len_train=math.ceil(len(station)*0.7)
len_val=math.ceil(len(station)*0.2)
len_test=math.ceil(len(station)*0.1)
#print(std.head)
# # 划分训练集,验证集,测试集
# In[25]:
#最后每个训练集的最后一个
i_train=len_train
i_val=len_val+len_train
i_test=len_test+len_val
#训练数据
station_train = station.iloc[:i_train,:]
y_train=train[:i_train]#y_train==detrend
trend_train = trend[:i_train]
std_train=std[:i_train]
print(np.shape(station_train))#(433991,14)
print(np.shape(y_train))#(433991,)
print(np.shape(station_val))
print(np.shape(y_val))
#验证数据
station_val = station.iloc[i_train:i_val]
y_val = train[i_train:i_val]
trend_val = trend[i_train:i_val]
std_val =std[i_train:i_val]
#测试数据
station_test = station.iloc[i_val:]
y_test=train[i_val:]
trend_test = trend[i_val:]
std_test=std[i_val:]
# len_train = 365*2#2017-2019
# len_val = 365*3#
# len_test = -1 # 2020-现在
# # 评价函数
# In[7]:
def compute_error(trues, predicted):# 回归评价函数:MAE、RAE、RMSE、RRSE、MAPE、R2
corr = np.corrcoef(predicted, trues)[0,1]#计算皮尔逊积矩相关系数 r
# 皮尔逊相关系数是一种度量两个变量间(线性)相关程度的方法。它是一个介于 1 和 -1 之间的值,
# 其中,1 表示变量完全正相关, 0 表示无关,-1 表示完全负相关。
mae = np.mean(np.abs(predicted - trues))#平均绝对值误差(MAE)
# """
# numpy.mean(a, axis, dtype, out,keepdims ) 求取均值
# 经常操作的参数为axis,以m * n矩阵举例:
# axis 不设置值,对 m*n 个数求均值,返回一个实数
# axis = 0:压缩行,对各列求均值,返回 1* n 矩阵
# axis =1 :压缩列,对各行求均值,返回 m *1 矩阵
# 操作对象为:数组或矩阵
# 数组:
# a = np.array([[1, 2], [3, 4]])
# >>>np.mean(a, axis=0) # axis=0,计算每一列的均值
# array([ 2., 3.])
# >>> np.mean(a, axis=1) # 计算每一行的均值
# array([ 1.5, 3.5])
# """
rae = np.sum(np.abs(predicted - trues)) / np.sum(np.abs(trues - np.mean(trues)))
# """
# sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue)
# a:用于进行加法运算的数组形式的元素
# axis的取值有三种情况:1.None(在默认/缺省的情况下),2.整数, 3.整数元组。
# 如果axis取None,即将数组/矩阵中的元素全部加起来,得到一个和。
# 如果axis为整数,axis的取值不可大于数组/矩阵的维度,且axis的不同取值会产生不同的结果。
# 如果axis为整数元组(x,y),则是求出axis=x和axis=y情况下得到的和。
# """
rmse = np.sqrt(np.mean((predicted - trues)**2))#均方根误差(RMSE)
rrse = np.sqrt(np.sum((predicted - trues)**2) / np.sum((trues - np.mean(trues))**2)) #根相对平方误差(RRSE)
mape = np.mean(np.abs((predicted - trues) / trues)) * 100#平均绝对百分误差(MAPE)
r2 = max(0, 1 - np.sum((predicted - trues)**2) / np.sum((trues - np.mean(trues))**2))#R-平方 ,max 返回给定参数的最大值
return corr, mae, rae, rmse, rrse, mape, r2
def compute_error_filtered(trues, predicted, filt):#计算错误已过滤 filt过滤器
trues = trues[filt]
predicted = predicted[filt]
corr = np.corrcoef(predicted, trues)[0,1]
mae = np.mean(np.abs(predicted - trues))
mse = np.mean((predicted - trues)**2)
rae = np.sum(np.abs(predicted - trues)) / np.sum(np.abs(trues - np.mean(trues)))
rmse = np.sqrt(np.mean((predicted - trues)**2))
r2 = max(0, 1 - np.sum((trues-predicted)**2) / np.sum((trues - np.mean(trues))**2))
return corr, mae, rae, rmse, rrse, mape, r2
# In[8]:
NUM_LAGS=10#滞后时间
# # FN+station+W
# In[27]:
def build_model(num_inputs, num_lags, num_preds):# MLP 多层感知机
#input_lags = Input(shape=(num_lags,))
input_lags = Input(shape=(14,))
x = input_lags
x = BatchNormalization()(x)
# print('输出的x:',x)
#BN算法可以省去这两项或者只需要小的L2正则化约束。原因,BN算法后,参数进行了归一化,
# 原本经过激活函数没有太大影响的神经元,分布变得明显,经过一个激活函数以后,
# 神经元会自动削弱或者去除一些神经元,就不用再对其进行dropout
# 另外就是L2正则化,由于每次训练都进行了归一化,就很少发生由于数据分布不同导致的参数变动过大,带来的参数不断增大。
# 可以吧训练数据集打乱,防止训练发生偏移
# 在卷积中,会出现每层卷积层中有(L)多个特征图。AxAxL特征矩阵。我们只需要以每个特征图为单元求取一对γ、β。
# 然后在对特征图进行神经元的归一化。
#定义基本的网络层
x = Dense(units=100, activation="tanh", kernel_regularizer=keras.regularizers.l2(0.05))(x)
# units:该层有几个神经元
# activation:该层使用的激活函数
# use_bias:是否添加偏置项
# kernel_initializer:权重初始化方法
# bias_initializer:偏置值初始化方法
# kernel_regularizer:权重规范化函数
# bias_regularizer:偏置值规范化方法
# activity_regularizer:输出的规范化方法
# kernel_constraint:权重变化限制函数
# bias_constraint:偏置值变化限制函数
x = Dropout(0.5)(x)#这防止过拟合,按照0.5的比例去掉一些神经元。
preds = Dense(units=num_preds)(x)
model = Model(input_lags, preds)
model.compile(loss="mse", optimizer="adam")#loss被称为损失函数,optimizer为优化器(编译神经网络的两个参数)
return model, input_lags, preds
checkpoint = keras.callbacks.ModelCheckpoint("weights2.best.hdf5", monitor='val_loss', verbose=0, save_best_only=True, mode='min')
# 该回调函数将在每个epoch后保存模型到filepath,filename:字符串,保存模型的路径
# monitor:需要监视的值,通常为:val_acc 或 val_loss 或 acc 或 loss
# verbose:信息展示模式,0或1。为1表示输出epoch模型保存信息,默认为0表示不输出该信息
# save_best_only:当设置为True时,将只保存在验证集上性能最好的模型
# mode:‘auto’,‘min’,‘max’之一,在save_best_only=True时决定性能最佳模型的评判准则,例如,当监�
没有合适的资源?快使用搜索试试~ 我知道了~
Python基于tensorflow实现光伏电站发量预测项目源代码+电站生产数据+天气数据
共14个文件
csv:10个
keep:2个
py:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 24 浏览量
2024-05-07
11:21:54
上传
评论
收藏 513KB ZIP 举报
温馨提示
Python基于tensorflow实现光伏电站发量预测项目源代码+电站生产数据+天气数据
资源推荐
资源详情
资源评论
收起资源包目录
光伏预测python代码.zip (14个子文件)
光伏预测python代码
电站数据集
station_6.csv 427KB
station_3.csv 430KB
station_8.csv 429KB
station_9.csv 442KB
station_2.csv 442KB
station_4.csv 426KB
station_7.csv 441KB
.keep 0B
station_1.csv 428KB
station_10.csv 443KB
模型建立.ipynb 200KB
模型.py 12KB
jinan_weather_by_day.csv 36KB
.keep 0B
共 14 条
- 1
资源评论
程序员柳
- 粉丝: 6465
- 资源: 1388
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功