Python h2o 框架到 np 数组重塑

Ang*_*dan 2 python h2o

我是Python的新手。

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

我使用了这段代码:

mynarray=np.array([np.array(nrows).astype(np.float32).reshape(5,5) for nrows in myh2oframe])
Run Code Online (Sandbox Code Playgroud)

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

kar*_*rap 5

让我先做一个小小的澄清。您无法将1000 x 25数组重新整形为5 x 5数组。原始数组和重构数组中的元素数量必须相同。

从您的代码来看,您似乎正在尝试将 h2o 框架的每一行,1 x 25尺寸重塑为5 x 5numpy 数组,这应该会产生一个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) 
Run Code Online (Sandbox Code Playgroud)

希望这有助于解决您的问题。