本Python笔记本显示和分析了如何处理NASA获得的电池充电/放电数据集。
对于这个模型的训练阶段,需要安装Python 3.x以及以下库:
Tensorflow 2.0
Numpy
Pandas
Scipy
Sci-kit learn
Matplot
Seaborn
对于该模型的预测阶段,除了Matplot和Seaborn之外,需要使用相同的库。
1.数据集的准备
需要下载数据集,然后将其解压缩到特定的目录中。
%tensorflow_version 2.x%matplotlib inline!pip show tensorflow!wget -cq https://ti.arc.nasa.gov/c/5 -O naza.zip!unzip -qqo naza.zip -d battery_data
在此部分中,所有处理数据集所需的库都很重要。
import datetimeimport numpy as npimport pandas as pdfrom scipy.io import loadmatfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.metrics import mean_squared_errorfrom sklearn import metricsimport matplotlib.pyplot as pltimport seaborn as sns
2.将数据集加载到内存
数据存储在多个“.mat”文件中。每个文件对应于特定的电池,每个文件的数据结构如下:
在Python中创建了一个函数,负责从"mat"文件中读取这些数据,并将它们存储在内存中以供以后访问,加载数据集后,使用panda函数对数据进行描述,以验证数据加载是否正确。
def load_data(battery): mat = loadmat('battery_data/' + battery + '.mat') print('Total data in dataset: ', len(mat[battery][0, 0]['cycle'][0])) counter = 0 dataset = [] capacity_data = [] for i in range(len(mat[battery][0, 0]['cycle'][0])): row = mat[battery][0, 0]['cycle'][0, i] if row['type'][0] == 'discharge': ambient_temperature = row['ambient_temperature'][0][0] date_time = datetime.datetime(int(row['time'][0][0]), int(row['time'][0][1]), int(row['time'][0][2]), int(row['time'][0][3]), int(row['time'][0][4])) + datetime.timedelta(seconds=int(row['time'][0][5])) data = row['data'] capacity = data[0][0]['Capacity'][0][0] for j in range(len(data[0][0]['Voltage_measured'][0])): voltage_measured = data[0][0]['Voltage_measured'][0][j] current_measured = data[0][0]['Current_measured'][0][j] temperature_measured = data[0][0]['Temperature_measured'][0][j] current_load = data[0][0]['Current_load'][0][j] voltage_load = data[0][0]['Voltage_load'][0][j] time = data[0][0]['Time'][0][j] dataset.append([counter + 1, ambient_temperature, date_time, capacity, voltage_measured, current_measured, temperature_measured, current_load, voltage_load, time]) capacity_data.append([counter + 1, ambient_temperature, date_time, capacity]) counter = counter + 1 print(dataset[0]) return [pd.DataFrame(data=dataset, columns=['cycle', 'ambient_temperature', 'datetime', 'capacity', 'voltage_measured', 'current_measured', 'temperature_measured', 'current_load', 'voltage_load', 'time']), pd.DataFrame(data=capacity_data, columns=['cycle', 'ambient_temperature', 'datetime', 'capacity'])]dataset, capacity = load_data('B0005')pd.set_option('display.max_columns', 10)print(dataset.head())dataset.describe()
下图显示了随着充电周期的推进,电池的老化过程。水平线表示与电池生命周期结束相关的阈值。
plot_df = capacity.loc[(capacity['cycle']>=1),['cycle','capacity']]sns.set_style("darkgrid")plt.figure(figsize=(12, 8))plt.plot(plot_df['cycle'], plot_df['capacity'])#Draw thresholdplt.plot([0.,len(capacity)], [1.4, 1.4])plt.ylabel('Capacity')# make x-axis ticks legibleadf = plt.gca().get_xaxis().get_major_formatter()plt.xlabel('cycle')plt.title('Discharge B0005')
还需计算电池的SOH值:
attrib=['cycle', 'datetime', 'capacity']dis_ele = capacity[attrib]C = dis_ele['capacity'][0]for i in range(len(dis_ele)): dis_ele['SoH']=(dis_ele['capacity'])/Cprint(dis_ele.head(5))
和以前所作的一样,每个周期都绘制一个SOH图表,水平线代表70%的阈值,即电池已经达到其使用寿命,因此建议进行更换。
plot_df = dis_ele.loc[(dis_ele['cycle']>=1),['cycle','SoH']]sns.set_style("white")plt.figure(figsize=(8, 5))plt.plot(plot_df['cycle'], plot_df['SoH'])#Draw thresholdplt.plot([0.,len(capacity)], [0.70, 0.70])plt.ylabel('SOH')# make x-axis ticks legibleadf = plt.gca().get_xaxis().get_major_formatter()plt.xlabel('cycle')plt.title('Discharge B0005')
3.SOH计算的训练阶段
准备了数据集,以便Tensorflow可以在训练阶段使用,为此创建两个结构,对应于预期的输入和输出。数据集的相关特征是:
电池容量、电压、电流、温度、负载电压、负载电流、时间。
对于输出数据,计算电池的SOH,以及在两种情况下的输入和输出,这些值被归一化到[0-1]之间的值。
C = dataset['capacity'][0]soh = []for i in range(len(dataset)): soh.append([dataset['capacity'][i] / C])soh = pd.DataFrame(data=soh, columns=['SoH'])attribs=['capacity', 'voltage_measured', 'current_measured', 'temperature_measured', 'current_load', 'voltage_load', 'time']train_dataset = dataset[attribs]sc = MinMaxScaler(feature_range=(0,1))train_dataset = sc.fit_transform(train_dataset)print(train_dataset.shape)print(soh.shape)
import tensorflow as tffrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densefrom tensorflow.keras.layers import Dropoutfrom tensorflow.keras.layers import Flattenfrom tensorflow.keras.layers import LSTMfrom tensorflow.keras.optimizers import Adam
总训练参数:27;
可训练参数:27。
对该模型进行训练,epoch=50;
model.fit(x=train_dataset, y=soh.to_numpy(), batch_size=25, epochs=50)
第二节传送门:
深度学习模型的准备和使用教程,LSTM用于锂电池SOH预测(第二节)(附Python的jypter源代码)_新能源姥大的博客-CSDN博客