【问题标题】:Python h2o frame to np array reshapePython h2o 框架到 np 数组重塑
【发布时间】:2018-04-12 03:02:35
【问题描述】:

我是python的新手。

我有一个包含 1000 行和 25 列的 h2o 框架表,我想将此表转换为 numpy 数组并重塑为 (5,5)

我使用了这个代码:

mynarray=np.array([np.array(nrows).astype(np.float32).reshape(5,5) for nrows in myh2oframe])

我收到的错误是无法将大小为 1604 的序列复制到维度为 1 的数组轴

【问题讨论】:

标签: python h2o


【解决方案1】:

让我先做一点澄清。您不能将 1000 x 25 数组重塑为 5 x 5 数组。原始数组和重构数组中的元素数量必须相同。

从您的代码看来,您正在尝试使用1 x 25 将每一行的 h2o 框架重塑为 5 x 5 numpy 数组,这应该会导致 1000 x 5 x 5 数组,因为有 1000 行。这是一个示例,您可以修改/将其应用于您的特定情况。

import h2o
import numpy as np

# initialize h2o
h2o.init()

# create a (1000, 25) h2o frame with real values (no missing values)
hf = h2o.create_frame(rows=1000, cols=25, real_fraction=1, missing_fraction=0) 

# First convert to a pandas df, then to a numpy array
num_array = hf.as_data_frame().as_matrix()

# Reshape the array
reshaped_array = num_array.reshape(1000, 25, 25)

# Check the dimensions of the reshaped array
reshaped_array.shape
# The output should be: (1000, 5, 5) 

希望这对您的问题有所帮助。

【讨论】:

    【解决方案2】:

    如果是 (1X1) h2o 数据框,则 flatten 有效

    $y_pred = reg.predict(df_h2o)

    $y_pred

    预测

    65.4295

    [1 行 x 1 列]

    $y_pred.flatten()

    65.42946292877197

    【讨论】:

      猜你喜欢
      • 2020-10-17
      • 2019-01-09
      • 2021-02-25
      • 2019-07-17
      • 2021-05-31
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多