gpt4 book ai didi

python - 将 numpy (n,) 向量 reshape 为 (n,1) 向量

转载 作者:太空狗 更新时间:2023-10-30 00:59:40 28 4
gpt4 key购买 nike

所以当我需要做一些线性代数时,我更容易将向量视为列向量。因此,我更喜欢 (n,1) 这样的形状。

形状 (n,) 和 (n,1) 之间是否存在显着的内存使用差异?

什么是首选方式?

以及如何将 (n,) 向量 reshape 为 (n,1) 向量。不知何故 b.reshape((n,1)) 并不能解决问题。

a = np.random.random((10,1))
b = np.ones((10,))
b.reshape((10,1))
print(a)
print(b)

[[ 0.76336295]
[ 0.71643237]
[ 0.37312894]
[ 0.33668241]
[ 0.55551975]
[ 0.20055153]
[ 0.01636735]
[ 0.5724694 ]
[ 0.96887004]
[ 0.58609882]]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

最佳答案

使用 python 语法糖更简单的方法是使用

b.reshape(-1,1)

系统将自动计算正确的形状而不是“-1”

关于python - 将 numpy (n,) 向量 reshape 为 (n,1) 向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549331/

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