【问题标题】:looping program for MLP Keras prediction用于 MLP Keras 预测的循环程序
【发布时间】:2019-08-19 21:55:51
【问题描述】:

我(有点像初学者)在一个时间序列数据应用程序上试验 Keras,我在其中创建了一个回归模型,然后将其保存以在不同的 Python 脚本上运行。

我正在处理的时间序列数据是每小时数据,我使用 Keras 中保存的模型来预测数据集中每小时的值。 (data = CSV 文件被读入 pandas)拥有多年的时间序列数据,有 8760 个(一年中的小时)预测,最后我试图在最后总结预测的值。

在下面的代码中,我没有展示如何重新创建模型架构(保存模型的 keras 要求),并且代码的运行速度非常慢。这种方法对于 200 次以下的预测似乎很好,但对于 8760 来说,代码似乎停滞不前,无法完成。

我对数据库没有任何经验,但与将 8760 个 keras 预测存储在 Python 列表中相比,这会是更好的方法吗?感谢您提供的任何提示,我仍在学习曲线中..

#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()

【问题讨论】:

    标签: machine-learning python keras regression prediction


    【解决方案1】:

    似乎你让你的生活变得非常很困难,没有明显的原因......

    对于初学者,您不需要为每一行加载模型 - 这太过分了!您绝对应该将 load_trained_model(weights_path) out 移出 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()
    

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 2017-09-05
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 2021-04-21
      • 2018-11-13
      • 2017-11-26
      • 2021-03-29
      相关资源
      最近更新 更多