python - reshape 在每行中维护 N 个样本

标签 python numpy reshape

我已经想出了如何做到这一点,但我想检查是否有更有效的方法。

我想要的是 reshape 向量并在第一行中保留样本 0 到 7,然后在第二行中保留样本 8 到 15,依此类推。

这是输出的示例,

array([[   0,    1,    2, ..., 1221, 1222, 1223],
       [   8,    9,   10, ..., 1229, 1230, 1231],
       [  16,   17,   18, ..., 1237, 1238, 1239],
       ...,
       [  40,   41,   42, ..., 1261, 1262, 1263],
       [  48,   49,   50, ..., 1269, 1270, 1271],
       [  56,   57,   58, ..., 1277, 1278, 1279]])

输入数据看起来像这样,

array([   0,    1,    2, ..., 1277, 1278, 1279])

除此之外还有没有更有效的方法

data = np.arange(0, 128*10)

# Reshape for S/H
temp = data.reshape(8*8, -1, order='F')
out = np.zeros((8, int(len(data)/8)))
for idx in range(0, 8):
    out[idx] = temp[idx*8: idx*8+8, :].ravel(order = 'F')

最佳答案

抱歉,我的第一个回答是我误读了问题。

这是一个基于纯 numpy 转置和 reshape 的解决方案:

np.transpose( data.reshape(8, 8, -1, order='F'), (1,2,0)).reshape(8,-1)

针对下面的解决方案进行基准测试得出:

enter image description here

关于python - reshape 在每行中维护 N 个样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76565912/

相关文章:

python - Pandas :将重复行的列分组到列表列中

python - 等价于 =+ 的字符串,但将现有字符串附加到新字符串而不是添加到新字符串之前

python - GraphRbacManagementClient.applications.create() 返回访问 token 丢失或格式错误

python - Pycharm 无法导入 numpy

reshape 数据框以将因子转换为 R 中的列

python - 使用 polyfit 求 30 个点的多项式函数

python - 通过 Scipy 和 Numpy 使用 Python 将数据拟合到 ODE 系统

python - 计算 DataFrame 中的行增量值

将数据框 reshape 为平均值堆栈

R:将行转为列,并使用 N/A 表示缺失值