MLP Keras 预测的循环程序
looping program for MLP Keras prediction
我(有点像初学者)在一个时间序列数据应用程序上尝试使用 Keras,我在其中创建了一个回归模型,然后将其保存到 运行 的另一个 Python 脚本中。
我正在处理的时间序列数据是每小时数据,我正在使用 Keras 中保存的模型来预测数据集中每个小时的值。 (data
= CSV 文件被读入 pandas)有了一年的时间序列数据,有 8760(一年中的几个小时)预测,最后我试图对预测值求和结束。
在下面的代码中,我没有展示模型架构是如何重新创建的(keras 要求保存模型)并且代码运行起来非常慢。这种方法对于 200 次预测似乎没问题,但对于 8760 次,代码似乎陷入了太多无法完成的困境。
我没有任何数据库方面的经验,但与在 Python 列表中存储 8760 个 keras 预测相比,这是更好的方法吗?感谢您提供的任何提示,我仍在学习中..
#set initial loop params & empty list to store modeled data
row_num = 0
total_estKwh = []
for i, row in data.iterrows():
params = row.values
if (params.ndim == 1):
params = np.array([params])
estimatedKwh = load_trained_model(weights_path).predict(params)
print('Analyzing row number:', row_num)
total_estKwh.append(estimatedKwh)
row_num += 1
df = pd.DataFrame.from_records(total_estKwh)
total = df.sum()
totalStd = np.std(df.values)
totalMean = df.mean()
似乎你的生活 非常 没有明显的原因......
对于初学者,您不需要为每一行 加载 模型 - 这太过分了!您绝对应该将 load_trained_model(weights_path)
移出 for
循环的 ,例如
model = load_trained_model(weights_path) # load ONCE
并将循环中的相应行替换为
estimatedKwh = model.predict(params)
其次,逐行调用模型进行预测同样效率不高;最好先将 params
准备为数组,然后将其提供给模型以获取批量预测。也忘记 print
语句..
总而言之,试试这个:
params_array = []
for i, row in data.iterrows():
params = row.values
if (params.ndim == 1):
params = np.array([params]) # is this if really necessary??
params_array.append(params)
params_array = np.asarray(params_array, dtype=np.float32)
total_estKwh = load_trained_model(weights_path).predict(params_array)
df = pd.DataFrame.from_records(total_estKwh)
total = df.sum()
totalStd = np.std(df.values)
totalMean = df.mean()
我(有点像初学者)在一个时间序列数据应用程序上尝试使用 Keras,我在其中创建了一个回归模型,然后将其保存到 运行 的另一个 Python 脚本中。
我正在处理的时间序列数据是每小时数据,我正在使用 Keras 中保存的模型来预测数据集中每个小时的值。 (data
= CSV 文件被读入 pandas)有了一年的时间序列数据,有 8760(一年中的几个小时)预测,最后我试图对预测值求和结束。
在下面的代码中,我没有展示模型架构是如何重新创建的(keras 要求保存模型)并且代码运行起来非常慢。这种方法对于 200 次预测似乎没问题,但对于 8760 次,代码似乎陷入了太多无法完成的困境。
我没有任何数据库方面的经验,但与在 Python 列表中存储 8760 个 keras 预测相比,这是更好的方法吗?感谢您提供的任何提示,我仍在学习中..
#set initial loop params & empty list to store modeled data
row_num = 0
total_estKwh = []
for i, row in data.iterrows():
params = row.values
if (params.ndim == 1):
params = np.array([params])
estimatedKwh = load_trained_model(weights_path).predict(params)
print('Analyzing row number:', row_num)
total_estKwh.append(estimatedKwh)
row_num += 1
df = pd.DataFrame.from_records(total_estKwh)
total = df.sum()
totalStd = np.std(df.values)
totalMean = df.mean()
似乎你的生活 非常 没有明显的原因......
对于初学者,您不需要为每一行 加载 模型 - 这太过分了!您绝对应该将 load_trained_model(weights_path)
移出 for
循环的 ,例如
model = load_trained_model(weights_path) # load ONCE
并将循环中的相应行替换为
estimatedKwh = model.predict(params)
其次,逐行调用模型进行预测同样效率不高;最好先将 params
准备为数组,然后将其提供给模型以获取批量预测。也忘记 print
语句..
总而言之,试试这个:
params_array = []
for i, row in data.iterrows():
params = row.values
if (params.ndim == 1):
params = np.array([params]) # is this if really necessary??
params_array.append(params)
params_array = np.asarray(params_array, dtype=np.float32)
total_estKwh = load_trained_model(weights_path).predict(params_array)
df = pd.DataFrame.from_records(total_estKwh)
total = df.sum()
totalStd = np.std(df.values)
totalMean = df.mean()