【CS231n學習筆記】2. python numpy 之numpy

來源:互聯網
上載者:User

標籤:sha   nes   http   學習筆記   計算   pre   port   數組   廣播   

Numpy數組的建立
import numpy as npa = np.full((3, 3), 1)print(a)a = np.random.random((3, 3))print(a)a = np.eye(3)print(a)a = np.array([[1, 2, 3, 4],              [5, 6, 7, 8],              [9, 10, 11, 12],              [13, 14, 15, 16]])print(a)print(a.shape)
輸出:[[1 1 1] [1 1 1] [1 1 1]][[ 0.09670856  0.44868154  0.43326738] [ 0.57400445  0.47124464  0.76310375] [ 0.72557452  0.98591433  0.97147127]][[ 1.  0.  0.] [ 0.  1.  0.] [ 0.  0.  1.]][[ 1  2  3  4] [ 5  6  7  8] [ 9 10 11 12] [13 14 15 16]](4, 4)

 

數組的存取方法
import numpy as npa = np.array([[1, 2, 3, 4],              [5, 6, 7, 8],              [9, 10, 11, 12],              [13, 14, 15, 16]])print(a)print(a.shape)print(a[1:3])print(a[1:-1, 1:-1])print(a[0, 1])print(a[1:3, 2])print(a[2, 1:3])print(a[[0, 1, 3, 3], [2, 3, 2, 2]])  # print a[0,2],a[1,3],a[3,2],a[3,2]
輸出:[[ 1  2  3  4] [ 5  6  7  8] [ 9 10 11 12] [13 14 15 16]](4, 4)[[ 5  6  7  8] [ 9 10 11 12]][[ 6  7] [10 11]]2[ 7 11][10 11][ 3  8 15 15]
蜜汁用法
import numpy as npa = np.array([[1, 2, 3, 4],              [5, 6, 7, 8],              [9, 10, 11, 12],              [13, 14, 15, 16]])print(np.arange(4))print(np.full([1, 4], 1))print(a[np.arange(4), 1])a[np.arange(4), [2, 3, 2, 3]] += 100print(a)
[0 1 2 3][[1 1 1 1]][ 2  6 10 14][[  1   2 103   4] [  5   6   7 108] [  9  10 111  12] [ 13  14  15 116]]

 

布爾
import numpy as npa = np.array([[1, 2, 3, 4],              [5, 6, 7, 8],              [9, 10, 11, 12],              [13, 14, 15, 16]])b = a > 5  # 還有這種操作???print(b)print(a[a > 6])
[[False False False False] [False  True  True  True] [ True  True  True  True] [ True  True  True  True]][ 7  8  9 10 11 12 13 14 15 16]

 

數組計算
import numpy as npa = np.array([1, 2])b = np.array([3, 4])print(a + b)print(a - b)print(a * b)print(a / b)print(a * 2)print(a + 3)print(a ** 0.5)
[4 6][-2 -2][3 8][ 0.33333333  0.5       ][2 4][4 5][ 1.          1.41421356]

 

矩陣乘法&轉置
import numpy as npa = np.array([1, 2])b = np.array([3, 4])print(a.dot(b))  # 相當於自動把b豎起來,相當於兩個向量內積a = np.array([[1, 2, 3],              [4, 5, 6]])b = np.array([[1, 2, 3],              [4, 5, 6]])print(b.T)  # 轉置print(a.dot(b.T))  # 矩陣乘法
11[[1 4] [2 5] [3 6]][[14 32] [32 77]]

 

求和
import numpy as npa = np.array([[1, 2, 3],              [4, 5, 6]])print(a.sum())  # 求和
21

各種函數 http://link.zhihu.com/?target=http%3A//docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html

 

廣播

秩不同的矩陣能一起運算

import numpy as npa = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])b = np.array([1, 1, 0])print(a + b)v = np.array([1, 2, 3])w = np.array([4, 5])v.reshape([3, 1])print(v.reshape(3, 1) + w)print(w + v.reshape(3, 1))
[[2 3 3] [2 3 3] [2 3 3]][[5 6] [6 7] [7 8]][[5 6] [6 7] [7 8]]

 

【CS231n學習筆記】2. python numpy 之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.