Python numpy生成矩陣、串聯(lián)矩陣代碼分享
import numpy
生成numpy矩陣的幾個(gè)相關(guān)函數(shù):
numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()
串聯(lián)生成numpy矩陣的幾個(gè)相關(guān)函數(shù):
numpy.array()
numpy.row_stack()
numpy.column_stack()
numpy.reshape()
>>> import numpy
>>> numpy.eye(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> numpy.zeros(3)
array([ 0., 0., 0.])
>>> numpy.ones(3)
array([ 1., 1., 1.])
>>> x1 = numpy.array((1, 2, 3))
>>> x1
array([1, 2, 3])
>>> x2 = numpy.array([4, 5, 6])
>>> x2
array([4, 5, 6])
>>> x3 = numpy.array((x1, x2))
>>> x3
array([[1, 2, 3],
[4, 5, 6]])
>>> x4 = x3.reshape(2, 3)
>>> x4
array([[1, 2, 3],
[4, 5, 6]])
>>> x4 = x3.reshape(3, 2)
>>> x4
array([[1, 2],
[3, 4],
[5, 6]])
>>> x5 = numpy.row_stack(x1, x2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vstack() takes exactly 1 argument (2 given)
>>> x5 = numpy.row_stack((x1, x2))
>>> x5
array([[1, 2, 3],
[4, 5, 6]])
>>> x6 = numpy.row_stack([x1, x2])
>>> x6
array([[1, 2, 3],
[4, 5, 6]])
>>> x7 = numpy.row_stack((x6, x2))
>>> x7
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6]])
>>> x7[0]
array([1, 2, 3])
>>> x7[1]
array([4, 5, 6])
>>> x7[2]
array([4, 5, 6])
>>> x8 = numpy.column_stack([x1, x2, x1, x2])
>>> x8
array([[1, 4, 1, 4],
[2, 5, 2, 5],
[3, 6, 3, 6]])
>>> x8[0]
array([1, 4, 1, 4])
>>> x8[1]
array([2, 5, 2, 5])
>>> x8[2]
array([3, 6, 3, 6])
>>> x8[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 3 is out of bounds for axis 0 with size 3
>>> x8[0][3]
4
>>>
python生成1行四列全2矩陣
print np.ones((1,4))*2
總結(jié)
以上就是本文關(guān)于Python numpy生成矩陣、串聯(lián)矩陣代碼分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- Python numpy 提取矩陣的某一行或某一列的實(shí)例
- Python編程給numpy矩陣添加一列方法示例
- python中numpy的矩陣、多維數(shù)組的用法
- 基于Python Numpy的數(shù)組array和矩陣matrix詳解
- Python的numpy庫(kù)中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法
- Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作示例
- Python創(chuàng)建對(duì)稱矩陣的方法示例【基于numpy模塊】
- Python中矩陣庫(kù)Numpy基本操作詳解
- Python中的Numpy矩陣操作
- Python numpy中矩陣的基本用法匯總
相關(guān)文章
使用Python開(kāi)發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊
這篇文章主要介紹了使用Python開(kāi)發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊,這樣我們要想實(shí)現(xiàn)手游腳本開(kāi)發(fā)的第一步,就是下載Android模擬器,然后在對(duì)安卓模擬器進(jìn)行鼠標(biāo)和鍵盤的模擬,以此來(lái)實(shí)現(xiàn)自動(dòng)化游戲腳本,需要的朋友可以參考下2021-11-11
pandas?實(shí)現(xiàn)?in?和?not?in?的用法及使用心得
pandas按條件篩選數(shù)據(jù)時(shí),除了使用query()方法,還可以使用isin和對(duì)isin取反進(jìn)行條件篩選,今天通過(guò)本文給大家介紹pandas?實(shí)現(xiàn)?in?和?not?in?的用法及使用心得,感興趣的朋友跟隨小編一起看看吧2023-01-01
關(guān)于自動(dòng)化測(cè)試框架pytest的Fixture固件
這篇文章主要介紹了關(guān)于自動(dòng)化測(cè)試框架pytest的Fixture固件,Fixture它其實(shí)就是一些函數(shù),會(huì)在執(zhí)行測(cè)試方法/測(cè)試函數(shù)前后加載運(yùn)行它們,需要的朋友可以參考下2023-03-03
Python+OpenCV實(shí)現(xiàn)圖像融合的原理及代碼
這篇文章主要介紹了Python+OpenCV實(shí)現(xiàn)圖像融合的原理及代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
Python使用progressbar模塊實(shí)現(xiàn)的顯示進(jìn)度條功能
這篇文章主要介紹了Python使用progressbar模塊實(shí)現(xiàn)的顯示進(jìn)度條功能,簡(jiǎn)單介紹了progressbar模塊的安裝,并結(jié)合實(shí)例形式分析了Python使用progressbar模塊顯示進(jìn)度條的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python中dumps與dump及l(fā)oads與load的區(qū)別
這篇文章主要介紹了Python中dumps與dump、loads與load的區(qū)別,json模塊提供了一種很簡(jiǎn)單的方式來(lái)編碼和解碼JSON數(shù)據(jù)。其中兩個(gè)主要的函數(shù)是json.dumps()和json.loads(),需要的朋友可以參考下2022-04-04

