本文簡要介紹 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_。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。