下划线表达式‘ c _’具体做什么?

似乎是某种水平连接,但我在网上找不到任何文档。下面是一个最简单的工作示例:

In [1]: from numpy import c_
In [2]: a = ones(4)
In [3]: b = zeros((4,10))
In [4]: c_[a,b]
Out[4]:
array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
92779 次浏览

Use IPython's ? syntax to get more information:

In [2]: c_?
Type:       CClass
Base Class: <class 'numpy.lib.index_tricks.CClass'>
String Form:<numpy.lib.index_tricks.CClass object at 0x9a848cc>
Namespace:  Interactive
Length:     0
File:       /usr/lib/python2.7/dist-packages/numpy/lib/index_tricks.py
Docstring:
Translates slice objects to concatenation along the second axis.


This is short-hand for ``np.r_['-1,2,0', index expression]``, which is
useful because of its common occurrence. In particular, arrays will be
stacked along their last axis after being upgraded to at least 2-D with
1's post-pended to the shape (column vectors made out of 1-D arrays).


For detailed documentation, see `r_`.


Examples
--------
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])

Actually it is not a function, it is an object of class CClass.

it is "not a function, so takes no parameters

Above is what the official document said. you can check this question for details.

numpy.r_ is not a function. What is it?

It took me a lot of time to understand but it seems I finally got it.

All you have to do is add along second axis.

let's take :

np.c_[np.array([1,2,3]), np.array([4,5,6])]

But there isn't second axis. So we mentally add one.

so shape of both array becomes (3,1).

So resultant shape would be (3,1+1) which is (3,2). which is the shape of result -

array([[1, 4],
[2, 5],
[3, 6]])

Another ex.:

np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]

shapes:

np.array([[1,2,3]]) = 1,3

np.array([[4,5,6]]) = 1,3

0 so we can think of it as [[0]] = 1,1

So result 1,3+1+1+3 = 1,8

which is the shape of result : array([[1, 2, 3, 0, 0, 4, 5, 6]])

I would explain this as follow. It concats your first array into the last dimension (axis) of your last array in the function.

For example:

# both are 2 dimensional array
a = array([[1, 2, 3], [4, 5, 6]])
b = array([[7, 8, 9], [10, 11, 12]])

Now, let's take a look at np.c_(a, b):

First, let's look at the shape:

The shape of both a and b are (2, 3). Concating a (2, 3) into the last axis of b (3), while keeping other axises unchanged (1) will become

(2, 3 + 3) = (2, 6)

That's the new shape.

Now, let's look at the result:

In b, the 2 items in the last axis are:

1st: [7, 8, 9]
2nd: [10, 11, 12]

Adding a to it means:

1st item: [1,2,3] + [7,8,9] = [1,2,3,7,8,9]
2nd item: [4,5,6] + [10,11,12] = [4,5,6,10,11,12]

So, the result is

[
[1,2,3,7,8,9],
[4,5,6,10,11,12]
]

It's shape is (2, 6)

It converts several 1D arrays into a 2D array, with the single dimension of the original arrays preserved as the 1st dimension of the 2D array. The multiple input arrays are used as the 2nd dimension.

Think of it this way: If you have data series of 30 records apiece collected into separate 1D arrays, np.c_ combines these series as you would in an excel table: side-by-side in separate columns of 30, rather than extending the first series.

For example, 2 starting arrays:

>>> arrayX = np.array([X1,X2...,X30])
array([X1, X2..., X30])
>>> arrayY = np.array([Y1,Y2...,Y30])
array([Y1, Y2..., Y30])

Let's see how np.c_ combines them:

>>>arrayC = np.c_(arrayX, arrayY)
array([[X1, Y1],
[X2, Y2],
...
[X30, Y30]])

See how it's still 30 records long? You can now use the 2nd dimension to navigate between data series.

The documentation somewhat cryptically states: "Translates slice objects to concatenation along the second axis." Second axis of what? The resulting 2D array, they mean. It's unclear if you don't know that this a variation of np.r_, which concatenates along the first axis; and also if you don't think of a 1D array as having another dimension. But syntactically, it kinda does.

Query the shape of the arrays to illustrate this:

>>> np.shape(arrayX)
(30,)
>>> np.shape(arrayY)
(30,)
>>> np.shape(arrayC)
(30,2)

You can see a 2nd dimension, or axis, is created by the np.c_ method, and the concatenation takes place there. By contrast:

>>> arrayR = np.r_[array1,array2]
array([X1, X2..., X30, Y1, Y2..., Y30])
>>> np.shape(arrayR)
(60,)

The np.r_ method concatenates within the first dimension, or along the first axis.

np.c_[[1,4,5,4,5,3],[1,4,2,4,5,3],[2,4,6,8,9,0]]..This Concats The Arrays Horizontal or Say By Axis 0.So The Array Will be

array([[1, 1, 2],
[4, 4, 4],
[5, 2, 6],
[4, 4, 8],
[5, 5, 9],
[3, 3, 0]])

To Determine The Rows, Total Elements/Total no Of Arrays

(i.e 18/3) [{len([1,4,5,4,5,3])}+{len([1,4,5,4,5,3])}+{len([2,4,6,8,9,0])}]//len([[1,4,5,4,5,3],[1,4,2,4,5,3],[2,4,6,8,9,0]])

So The Aboove One Gives 6..SO 6 rows will be there.

For No of Colums, Total Elements/len(any one array inside the array)

for eg 18/len([1,4,5,4,5,3])

So The Columns will be 18/6 and That is 3.

A short technique for concatenation of arrays using numpy is np.c_ and np.r_

np.c_[] concatenates arrays along second axis but, np.r_[] concatenates arrays along first axis

Suppose:

import numpy as np
a = np.array([[1,2,3],
[11,22,33]]
)
b = np.array([[4,5,6],
[44,55,66]]
)

a's shape: (2,3) i.e. (no. of rows, no. of columns) also = (1st axis, 2nd axis)

b's shape: (2,3) i.e.(1st axis, 2nd axis)

np.r_ concatenates along the first axis so np.r_[a,b] gives:

array([[ 1,  2,  3],
[11, 22, 33],
[ 4,  5,  6],
[44, 55, 66]])

i.e. concatination along the rows(first axis), so number of rows will increase here.

While np.c_[a,b] concatenates along the Second axis i.e. columns here as below:

array([[ 1,  2,  3,  4,  5,  6],
[11, 22, 33, 44, 55, 66]])

For example:

I have 2 arrays with values:
a = [x1,x2,x3]
b = [y1,y2,y3]

and we want to merge them together that we have something like: [[x1, y1],[x2, y2],[x3, y3]].

Therefore we use numpy.c_[a, b]

import numpy


a = [1,2,3]
b = [4,5,6]
value = numpy.c_[a,b]


print(value)
# [
#  [1, 4],
#  [2, 5],
#  [3, 6]
# ]