本文简要介绍 python 语言中 numpy.r_
的用法。
用法:
numpy.r_ = <numpy.lib.index_tricks.RClass object>
沿第一个轴将切片对象转换为串联。
这是一种快速构建数组的简单方法。有两个用例。
如果索引表达式包含逗号分隔的数组,则将它们沿其第一个轴堆叠。
如果索引表达式包含切片符号或标量,则创建一个一维数组,其范围由切片符号指示。
如果使用切片表示法,则语法
start:stop:step
等效于括号内的np.arange(start, stop, step)
。但是,如果step
是一个虚数(即 100j),则其整数部分被解释为所需的 number-of-points,并且开始和停止都包括在内。换句话说,start:stop:stepj
被解释为括号内的np.linspace(start, stop, step, endpoint=1)
。在扩展切片符号之后,所有逗号分隔的序列被连接在一起。作为索引表达式的第一个元素放置的可选字符串可用于更改输出。字符串 ‘r’ 或 ‘c’ 导致矩阵输出。如果结果是一维并且指定了‘r’,则生成一个 1 x N(行)矩阵。如果结果是一维且指定了‘c’,则生成一个 N x 1(列)矩阵。如果结果是二维的,那么两者都提供相同的矩阵结果。
字符串整数指定沿哪个轴堆叠多个逗号分隔的数组。一个由两个逗号分隔的整数组成的字符串允许指示强制每个条目作为第二个整数进入的最小维数(要连接的轴仍然是第一个整数)。
具有三个逗号分隔整数的字符串允许指定要连接的轴、强制输入的最小维数以及哪个轴应包含小于指定维数的数组的开头。换句话说,第三个整数允许您指定 1 应放置在已升级其形状的数组的形状中的位置。默认情况下,它们被放置在形状元组的前面。第三个参数允许您指定数组的开始位置。因此,‘0’ 的第三个参数会将 1 放在数组形状的末尾。负整数指定在新形状元组中升级后的数组的最后一维应该放置的位置,因此默认值为“-1”。
- Not a function, so takes no parameters:
- 连接的 ndarray 或矩阵。
参数:
返回:
例子:
>>> np.r_[np.array([1,2,3]), 0, 0, np.array([4,5,6])] array([1, 2, 3, ..., 4, 5, 6]) >>> np.r_[-1:1:6j, [0]*3, 5, 6] array([-1. , -0.6, -0.2, 0.2, 0.6, 1. , 0. , 0. , 0. , 5. , 6. ])
字符串整数指定要连接的轴或强制进入的最小维数。
>>> a = np.array([[0, 1, 2], [3, 4, 5]]) >>> np.r_['-1', a, a] # concatenate along last axis array([[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5]]) >>> np.r_['0,2', [1,2,3], [4,5,6]] # concatenate along first axis, dim>=2 array([[1, 2, 3], [4, 5, 6]])
>>> np.r_['0,2,0', [1,2,3], [4,5,6]] array([[1], [2], [3], [4], [5], [6]]) >>> np.r_['1,2,0', [1,2,3], [4,5,6]] array([[1, 4], [2, 5], [3, 6]])
使用 ‘r’ 或 ‘c’ 作为第一个字符串参数会创建一个矩阵。
>>> np.r_['r',[1,2,3], [4,5,6]] matrix([[1, 2, 3, 4, 5, 6]])
相关用法
- Python numpy recarray.dot用法及代码示例
- Python numpy random.mtrand.RandomState.wald用法及代码示例
- Python numpy recarray.itemset用法及代码示例
- Python numpy random.mtrand.RandomState.multivariate_normal用法及代码示例
- Python numpy random.standard_exponential用法及代码示例
- Python numpy recarray.view用法及代码示例
- Python numpy random.mtrand.RandomState.gumbel用法及代码示例
- Python numpy random.mtrand.RandomState.multinomial用法及代码示例
- Python numpy random.rand用法及代码示例
- Python numpy random.mtrand.RandomState.logistic用法及代码示例
- Python numpy random.mtrand.RandomState.shuffle用法及代码示例
- Python numpy random.triangular用法及代码示例
- Python numpy recarray.tolist用法及代码示例
- Python numpy recarray.setflags用法及代码示例
- Python numpy random.noncentral_f用法及代码示例
- Python numpy roll用法及代码示例
- Python numpy recarray.flat用法及代码示例
- Python numpy random.mtrand.RandomState.poisson用法及代码示例
- Python numpy rollaxis用法及代码示例
- Python numpy recarray用法及代码示例
- Python numpy recarray.sort用法及代码示例
- Python numpy random.lognormal用法及代码示例
- Python numpy random.mtrand.RandomState.seed用法及代码示例
- Python numpy random.mtrand.RandomState.triangular用法及代码示例
- Python numpy random.gumbel用法及代码示例
注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.r_。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。