gpt4 book ai didi

Python h2o 框架到 np 数组 reshape

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:05 25 4
gpt4 key购买 nike

我是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 的数组轴

最佳答案

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

从您的代码来看,您似乎正在尝试将 h2o 框架的每一行 reshape 为 1 x 25,尺寸为 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)

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

关于Python h2o 框架到 np 数组 reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48317921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com