重塑一个可变的NumPy数组

huangapple go评论63阅读模式
英文:

Reshape a variable numpy array

问题

假设我有一个形状已知的NumPy数组u,以及u中总条目数的除数d。如何快速地将u重新塑造为形状为(something,d)的数组?

u只是一个双精度数时,也应包括在内 -> (1,1)

u为空时,应变成一个形状为(0,d)的数组。

英文:

Suppose i have a numpy array u with a given shape, a a divisor d of the total number of entries in u. How can i fastly reshape u to be shaped (something,d) ?

The case where u is just a double should be included as well -> (1,1)

The case where u is empty should become a (0,d) shaped array

答案1

得分: 1

你想要使用reshape

u.reshape(-1, d)

Python 中没有double,你是不是想说float

简而言之:

import numpy as np

def div_reshape(arr, div):
    if arr.size == 0:
        return np.empty(shape=(0, div))
    elif arr.size == 1:
        return arr.reshape(1, 1)
    else:
        return arr.reshape(-1, d)
英文:

You want to use reshape

u.reshape(-1, d)

There is no double in Python you do you mean float ?

In short :

import numpy as np

def div_reshape(arr, div):
    if arr.size == 0:
        return np.empty(shape=(0, div))
    elif arr.size == 1:
        return arr.reshape(1, 1)
    else:
        return arr.reshape(-1, d)

huangapple
  • 本文由 发表于 2020年1月6日 20:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定