Consider the following simple example:
请考虑以下简单示例:
X = numpy.zeros([10, 4]) # 2D array
x = numpy.arange(0,10) # 1D array
X[:,0] = x # WORKS
X[:,0:1] = x # returns ERROR:
# ValueError: could not broadcast input array from shape (10) into shape (10,1)
X[:,0:1] = (x.reshape(-1, 1)) # WORKS
Can someone explain why numpy has vectors of shape (N,) rather than (N,1) ? What is the best way to do the casting from 1D array into 2D array?
有人可以解释为什么numpy有形状(N,)而不是(N,1)的向量?从1D阵列到2D阵列的最佳铸造方法是什么?
Why do I need this? Because I have a code which inserts result x
into a 2D array X
and the size of x changes from time to time so I have X[:, idx1:idx2] = x
which works if x
is 2D too but not if x is 1D.
我为什么需要这个?因为我有一个代码将结果x插入到2D数组X中,并且x的大小不时变化所以我有X [:,idx1:idx2] = x如果x也是2D也可以工作,但如果x是1D则不行。
3 个解决方案
#1
4
Do you really need to be able to handle both 1D and 2D inputs with the same function? If you know the input is going to be 1D, use
您真的需要能够使用相同的功能处理1D和2D输入吗?如果您知道输入将是1D,请使用
X[:, i] = x
If you know the input is going to be 2D, use
如果您知道输入将是2D,请使用
X[:, start:end] = x
If you don't know the input dimensions, I recommend switching between one line or the other with an if
, though there might be some indexing trick I'm not aware of that would handle both identically.
如果您不知道输入维度,我建议使用if在一行或另一行之间切换,尽管可能有一些索引技巧我不知道它会同时处理两者。
Your x
has shape (N,)
rather than shape (N, 1)
(or (1, N)
) because numpy isn't built for just matrix math. ndarrays are n-dimensional; they support efficient, consistent vectorized operations for any non-negative number of dimensions (including 0). While this may occasionally make matrix operations a bit less concise (especially in the case of dot
for matrix multiplication), it produces more generally applicable code for when your data is naturally 1-dimensional or 3-, 4-, or n-dimensional.
你的x有形状(N,)而不是形状(N,1)(或(1,N))因为numpy不是为矩阵数学而构建的。 ndarray是n维的;它们支持任何非负数维度(包括0)的高效,一致的矢量化操作。虽然这有时可能使矩阵运算不那么简洁(特别是在矩阵乘法的点的情况下),但是当数据自然是1维或3维,4维或n维时,它会产生更普遍适用的代码。
#2
0
I think you have the answer already included in your question. Numpy allows the arrays be of any dimensionality (while afaik Matlab prefers two dimensions where possible), so you need to be correct with this (and always distinguish between (n,) and (n,1)). By giving one number as one of the indices (like 0 in 3rd row), you reduce the dimensionality by one. By giving a range as one of the indices (like 0:1 in 4th row), you don't reduce the dimensionality.
我想你的答案已经包含在你的问题中了。 Numpy允许数组具有任何维度(虽然afaik Matlab在可能的情况下更喜欢两个维度),因此您需要对此进行更正(并始终区分(n,)和(n,1))。通过将一个数字作为索引之一(如第3行中的0),可以将维度减少一个。通过将范围作为索引之一(如第4行中的0:1),您不会降低维数。
Line 3 makes perfect sense for me and I would assign to the 2-D array this way.
第3行对我来说非常有意义,我将以这种方式分配给2-D阵列。
#3
0
Here are two tricks that make the code a little shorter.
这里有两个技巧,使代码更短。
X = numpy.zeros([10, 4]) # 2D array
x = numpy.arange(0,10) # 1D array
X.T[:1, :] = x
X[:, 2:3] = x[:, None]
#1
4
Do you really need to be able to handle both 1D and 2D inputs with the same function? If you know the input is going to be 1D, use
您真的需要能够使用相同的功能处理1D和2D输入吗?如果您知道输入将是1D,请使用
X[:, i] = x
If you know the input is going to be 2D, use
如果您知道输入将是2D,请使用
X[:, start:end] = x
If you don't know the input dimensions, I recommend switching between one line or the other with an if
, though there might be some indexing trick I'm not aware of that would handle both identically.
如果您不知道输入维度,我建议使用if在一行或另一行之间切换,尽管可能有一些索引技巧我不知道它会同时处理两者。
Your x
has shape (N,)
rather than shape (N, 1)
(or (1, N)
) because numpy isn't built for just matrix math. ndarrays are n-dimensional; they support efficient, consistent vectorized operations for any non-negative number of dimensions (including 0). While this may occasionally make matrix operations a bit less concise (especially in the case of dot
for matrix multiplication), it produces more generally applicable code for when your data is naturally 1-dimensional or 3-, 4-, or n-dimensional.
你的x有形状(N,)而不是形状(N,1)(或(1,N))因为numpy不是为矩阵数学而构建的。 ndarray是n维的;它们支持任何非负数维度(包括0)的高效,一致的矢量化操作。虽然这有时可能使矩阵运算不那么简洁(特别是在矩阵乘法的点的情况下),但是当数据自然是1维或3维,4维或n维时,它会产生更普遍适用的代码。
#2
0
I think you have the answer already included in your question. Numpy allows the arrays be of any dimensionality (while afaik Matlab prefers two dimensions where possible), so you need to be correct with this (and always distinguish between (n,) and (n,1)). By giving one number as one of the indices (like 0 in 3rd row), you reduce the dimensionality by one. By giving a range as one of the indices (like 0:1 in 4th row), you don't reduce the dimensionality.
我想你的答案已经包含在你的问题中了。 Numpy允许数组具有任何维度(虽然afaik Matlab在可能的情况下更喜欢两个维度),因此您需要对此进行更正(并始终区分(n,)和(n,1))。通过将一个数字作为索引之一(如第3行中的0),可以将维度减少一个。通过将范围作为索引之一(如第4行中的0:1),您不会降低维数。
Line 3 makes perfect sense for me and I would assign to the 2-D array this way.
第3行对我来说非常有意义,我将以这种方式分配给2-D阵列。
#3
0
Here are two tricks that make the code a little shorter.
这里有两个技巧,使代码更短。
X = numpy.zeros([10, 4]) # 2D array
x = numpy.arange(0,10) # 1D array
X.T[:1, :] = x
X[:, 2:3] = x[:, None]