Python h2o 框架到 np 数组重塑

Python h2o frame to np array reshape

我是 python 的新手。

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

我使用了这个代码:

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

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

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

从你的代码来看,你似乎在尝试重塑水帧的每一行,使用 1 x 25,维度进入 5 x 5 numpy 数组,这应该导致 1000 x 5 x 5 数组,因为有 1000 行。这是一个执行此操作的示例,您可以 modify/apply 它适合您的具体情况。

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) 

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

如果它是 (1X1) h2o 数据帧,则展平工作

$y_pred = reg.predict(df_h2o)


$y_pred
  
predict
---------
65.4295

[1 row x 1 column]


$y_pred.flatten()