Python——Numpy學習筆記

來源:互聯網
上載者:User

標籤:nump   div   擷取   color   array   數組   產生   切片   blog   

建立:np.array()

a = np.array([1,2,3,4])b = np.array([1,2,3,4][4,5,6,7][7,8,9,10])
a[1,2,3,4]b[[1,2,3,4],[4,5,6,7],[7,8,9,10]]

獲得:數組形狀各個軸的長度的元組 .shape()

>>a.shape (4,)>>b.shape(3,4)

修改軸的長短(記憶體位址沒變):.shape =

>>b.shape = 4,3>>barray([[1,2,3],       [4,4,5],       [6,7,7],       [8,9,10]])
>>b.shape =2,-1 %設定某個軸為-1表示自動計算長度
>>b
array([[1,2,3,4,4,5],
[6,7,7,8,9,10]])

用已有數組資料 新產生另一個的形狀數組:=  .reshape()

(此時兩者共用資料,即資料地址相同)

>>c = a.reshape(2,2)    %reshape((2,2))也可以>>carray([[1,2],       [3,4]])

元素類型:  .dtype

用整數下標建立的數組,預設32位長整型(32位python)

>>c.dtype
dtype(‘int32‘)

建立數組,並指定參數 

>>ai32 = np.array([1,2,3,4],dtype=np.int32)>>af = np.array([1,2,3,4],dtype=float)>>ac = np.array([1,2,3,4],dtype=complex)

>>a = np.int16(200)
>>a*a
-25536 %int16不夠,計算200*200溢出

存取數組

>>a = np.array(10)array([0 1 2 3 4 5 6 7 8 9 ])>>a[5]   %用整數作為下標可以擷取數組中某個元素
>>a[3:5] %用切片做下標,擷取數組一部分,包括a[3]不包括a[5]
>>a[:5] %從0開始到a[4],不包括a[5]
>>a[:-1] %下表用負數,表示從0開始,直到數組最後往前數1的不包括

 

a[5]5a[3:5][3 4]a[:5][0 1 2 3 4]a[:-1][0 1 2 3 4 5 6 7 8]

 

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.