对numpy.c_文档和示例代码感到困惑 [英] confused about numpy.c_ document and sample code
问题描述
我多次阅读有关numpy.c_的文档,但仍然感到困惑.据说-将切片对象沿第二个轴平移为串联".在以下文档中.谁能在下面的示例中阐明什么是切片对象,什么是第二轴?我看到它们都是一维的,并且混淆了第二轴的位置.
I read the document about numpy.c_ many times but still confused. It is said -- "Translates slice objects to concatenation along the second axis." in the following document. Could anyone clarify in the example below, what is slice objects, and what is 2nd axis? I see they are all one dimension and confused where the 2nd axis coming from.
在Windows上使用Python 2.7.
Using Python 2.7 on Windows.
http://docs.scipy.org/doc/numpy-1.6.0/reference/generated/numpy.c_.html#numpy.c_
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])
推荐答案
np.c_
是进行数组连接的另一种方法
np.c_
is another way of doing array concatenate
In [701]: np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
Out[701]: array([[1, 2, 3, 0, 0, 4, 5, 6]])
In [702]: np.concatenate([np.array([[1,2,3]]), [[0]], [[0]], np.array([[4,5,6]])],
axis=1)
Out[702]: array([[1, 2, 3, 0, 0, 4, 5, 6]])
在两种情况下,输出形状均为(1,8);串联在第1轴= 1上.
The output shape is (1,8) in both cases; the concatenation was on axis=1, the 2nd axis.
c_
负责将0
的尺寸扩展到np.array([[0]])
,这是连接所需的2d(1,1).
c_
took care of expanding the dimensions of the 0
to np.array([[0]])
, the 2d (1,1) needed to concatenate.
np.c_
(和np.r_
)实际上是具有__getitem__
方法的类对象,因此它可以使用[]
语法工作. numpy/lib/index_tricks.py
源文件仅供参考.
np.c_
(and np.r_
) is actually a class object with a __getitem__
method, so it works with the []
syntax. The numpy/lib/index_tricks.py
source file is instructive reading.
请注意,row
版本使用:slice语法,可生成一个1d(8,)数组(相同的数字,但在1d中)
Note that the row
version works with the : slice syntax, producing a 1d (8,) array (same numbers, but in 1d)
In [706]: np.r_[1:4,0,0,4:7]
Out[706]: array([1, 2, 3, 0, 0, 4, 5, 6])
In [708]: np.concatenate((np.arange(4),[0],[0],np.arange(4,7)))
Out[708]: array([0, 1, 2, 3, 0, 0, 4, 5, 6])
In [710]: np.hstack((np.arange(4),0,0,np.arange(4,7)))
Out[710]: array([0, 1, 2, 3, 0, 0, 4, 5, 6])
np.c_
是一种方便,但是您不是必须了解的内容.我认为能够直接与concatenate
一起使用会更有用.它迫使您明确考虑输入的维度.
np.c_
is a convenience, but not something you are required to understand. I think being able to work with concatenate
directly is more useful. It forces you to think explicitly about the dimensions of the inputs.
[[1,2,3]]
实际上是一个列表-包含一个列表的列表. np.array([[1,2,3]])
是形状为(1,3)的2d数组. np.arange(1,4)
产生一个具有相同数字的(3,)数组. np.arange(1,4)[None,:]
使其成为(1,3)数组.
[[1,2,3]]
is actually a list - a list containing one list. np.array([[1,2,3]])
is a 2d array with shape (1,3). np.arange(1,4)
produces a (3,) array with the same numbers. np.arange(1,4)[None,:]
makes it a (1,3) array.
slice(1,4)
是切片对象. np.r_
和np.c_
可以通过实际使用np.arange
将切片对象转换为数组.
slice(1,4)
is a slice object. np.r_
and np.c_
can turn a slice object into a array - by actually using np.arange
.
In [713]: slice(1,4)
Out[713]: slice(1, 4, None)
In [714]: np.r_[slice(1,4)]
Out[714]: array([1, 2, 3])
In [715]: np.c_[slice(1,4)] # (3,1) array
Out[715]:
array([[1],
[2],
[3]])
In [716]: np.c_[1:4] # equivalent with the : notation
Out[716]:
array([[1],
[2],
[3]])
回到原始示例(可能不是最好的示例):
And to get back to the original example (which might not be the best):
In [722]: np.c_[[np.r_[1:4]],0,0,[np.r_[4:7]]]
Out[722]: array([[1, 2, 3, 0, 0, 4, 5, 6]])
==========
==========
In [731]: np.c_[np.ones((5,3)),np.random.randn(5,10)].shape
Out[731]: (5, 13)
对于np.c_
两者的第一维都必须匹配.
For np.c_
the 1st dimension of both needs to match.
在learn
示例中,n_samples
是X
(行)的第一个暗角,并且randn
也需要具有这么多行.
In the learn
example, n_samples
is the 1st dim of X
(rows), and the randn
also needs to have that many rows.
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
np.concatenate([(X, randn(n_samples...)], axis=1)
在这里应该也能正常工作.有点呆板,但功能上是相同的.
np.concatenate([(X, randn(n_samples...)], axis=1)
should work just as well here. A little wordier, but functionally the same.
这篇关于对numpy.c_文档和示例代码感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!