Python numpy產生矩陣、串聯矩陣代碼分享,pythonnumpy

來源:互聯網
上載者:User

Python numpy產生矩陣、串聯矩陣代碼分享,pythonnumpy

import numpy

產生numpy矩陣的幾個相關函數:

numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()

串聯產生numpy矩陣的幾個相關函數:

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

總結

以上就是本文關於Python numpy產生矩陣、串聯矩陣代碼分享的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.