Numpy tips: numpy.r_, numpy.c_, numpy.s_使用方法详解

numpy.r_是一个用于沿第一轴快速连接数组或切片对象的工具,尤其适用于创建多维数组。它不是函数,而是通过表达式构建数组的一种方式。可以使用'r'或'c'字符选项来控制输出是一维还是二维矩阵。虽然numpy.concatenate()也可实现类似功能,但numpy.r_允许直接使用切片对象,提供了更简洁的语法。
摘要由CSDN通过智能技术生成

目录

1. 前言

2. numpy.r_

2.1 基本例--输入是1-D的情况

2.2 基本例--输入是2-D的情况

3. 不是函数!

4. 字符选项控制输出 

5.  数字选项控制按哪个轴连接

6. Why not just use numpy.concatenate()?


1. 前言

        在读代码过程中看到np.r_[...],惊呆了!是不是写错了?一运行还真能运行,还有这样的函数啊(笑哭)。。。

        以下简单列举一些搜索到的信息和例子。想要更多更深地了解的小伙伴们自行查看以下连接吧。生命苦短,学海无涯。。。

2. numpy.r_

According to numpy documention[1]:

Translates slice objects to concatenation along the first axis.

This is a simple way to build up arrays quickly. There are two use cases.

将slice对象沿第一轴进行连接。这是创建一个数组的快捷方式,有以下两种使用场景:

  1. If the index expression contains comma separated arrays, then stack them along their first axis.

  2. If the index expression contains slice notation or scalars then create a 1-D array with a range indicated by the slice notation.

        简单通俗地来说,就是将一些slice object进行沿第一轴连接(通俗一点的说法是row-wise)连接。但是,row-wise这个说法其实是有点含糊,因为这个预设了矩阵这种形象。因为行和列仅在2维的情况下才有意义。在更广义的多维数组的话语中,已经没有行和列这种概念。所以说‘along the first axis’是严谨而通用的。

2.1 基本例--输入是1-D的情况

例1:       

V = np.array([1,2,3,4,5,6 ])
Y = np.array([7,8,9,10,11,12])
print(np.r_[V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]])

        输出:[ 1  2  7  4  8  9  5  6 11 12]

2.2 基本例--输入是2-D的情况

        其实以上“along the first axis”的说法的效果在输入本身就是1-D的情况下是看不出来的。只要在高维输入的条件下才能看出来。这里举一个2-D的例子来看看。

print('example4...')
a = np.arange(9).reshape(3,3)
b = np.arange(10,19).reshape(3,3)
print(np.r_[a[0:2,1:3], b[0:2,1:3]])

 输出:

        example4...
        [[ 1  2]
         [ 4  5]
         [11 12]
         [14 15]]

        从这个输出就可以看出,所谓的‘row-wise concatenation’(对应于2-D情况下的along the first axis)其实是按“纵向”进行连接,即等价于numpy.vstack()的功能,如下例所示。稍微有一点反直觉。。。需要注意。

c = np.vstack((a[0:2,1:3], b[0:2,1:3]))
print(c)

         运行以上代码会得到跟上面相同的结果。

3. 不是函数!

        numpy.r_不是函数!不是函数!不是函数!重要的事情说三遍^-^

        所以使用它的时候是numpy.r_[...], 而不是numpy.r_(...)

4. 字符选项控制输出 

        Optional character strings placed as the first element of the index expression can be used to change the output. The strings ‘r’ or ‘c’ result in matrix output. If the result is 1-D and ‘r’ is specified a 1 x N (row) matrix is produced. If the result is 1-D and ‘c’ is specified, then a N x 1 (column) matrix is produced. If the result is 2-D then both provide the same matrix result.

        用字符'r'或者'c'可以将1-D数组输出变换为2-D矩阵输出。如果原输出为1-D(其形状为(x,)),则追加'r'控制的话,输出将变为(1, N)的矩阵;追加'c'控制的话,输出将变为(N,1)的矩阵。如下例所示:

V = np.array([1,2,3,4,5,6 ])
Y = np.array([7,8,9,10,11,12])
print(np.r_[V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]])

print(np.r_[V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]].shape)
print(np.r_['r',V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]].shape)
print(np.r_['c',V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]].shape)

        输出:

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

        如果原输出本来就是2-D的,那加不加'r','c'没有影响。 

5.  数字选项控制按哪个轴连接

        A string integer specifies which axis to stack multiple comma separated arrays along. A string of two comma-separated integers allows indication of the minimum number of dimensions to force each entry into as the second integer (the axis to concatenate along is still the first integer).

        如下例所示,对于2-D数组输入,缺省条件下按纵向连接;但是用'-1'指定按最后一个轴(在此例中只有两个轴,因此就是第2轴)的话,就是横向连接,等价于numpy.hstack()。将'-1'改为'1'的话会得到相同的效果。

# Using string integers to control the axis along which to concatenate
print('example5...')
a = np.array([[0, 1, 2], [3, 4, 5]])
print(np.r_[a, a]) # concatenate along last axis
print(np.r_['-1', a, a]) # concatenate along last axis

 

        还有更复杂的连接选项的指定方式(估计非advanced user也不会用到),此处不再赘述,有兴趣的小伙伴可以直接参考numpy文档[1].

         

6. Why not just use numpy.concatenate()?

        同样还有hstack(), vstack()等等,那为什么要用numpy.r_[]这种怪怪的东西呢?或者说用numpy.r_[]有什么好处呢? 

Some people explained in [3], seems reasonable to me.

   np.r_ is implemented in the numpy/lib/index_tricks.py file. This is pure Python code, with no special compiled stuff. So it is not going to be any faster than the equivalent written with concatenatearange and linspace. It's useful only if the notation fits your way of thinking and your needs.

        也就是说,只是一种使得代码更加精简的方式。没有速度或者效率方面的差别。而且,np.r_的底层还是调用np.concatenate().

# example2
a = np.arange(9).reshape(3,3)
b = np.arange(10,19).reshape(3,3)
print(np.concatenate((a[1,1:3], b[1,1:3]), axis = 0))
print(np.r_[a[1,1:3], b[1,1:3]])

        如上所示,两者调用大同小异。

        唯一的差别(据说是)在于np.concatenate()只接受数组作为输入,而numpy.r_[]接受slice object作为输入。好吧,so what? 我不能分辨其中区别,只不过觉得后者确实更炫一点^-^

        类似地还有numpy.c_和numpy.s_...不再赘述。

Reference

[1] numpy.r_ — NumPy v1.21 Manual

[2] python - What does np.r_ do (numpy)? - Stack Overflow

[3] python why use numpy.r_ instead of concatenate - Stack Overflow

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笨牛慢耕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值