NumPy Creation of arrays
import NumPy as NPA = Np.full ((3, 3), 1< Span style= "COLOR: #000000" >) print (a) a = Np.random.random ((3, 3 print Span style= "COLOR: #000000" > (a) a = Np.eye (3 print (a) a = Np.array ([[1, 2, 3, 4], [ 5, 6, 7, 8 9, ten, one, 1213, +, 16 print (a) print (A.shape)
output: [[1 1 1] [1 1 1] [1 1 10.09670856 0.44868154 0.43326738 0.57400445 0.47124464 0.763103750.72557452 0.98591433 0.971471271. 0. 0.] [0. 1. 0.] [0. 0. 11 2 3 45 6 7 89 Ten[ (4, 4)
Access Methods for arrays
ImportNumPy 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 45 6 7 89 [all][](4, 4 5 6 7 89 Ten6 7 ] [ten]]27 each] [3 8 15 15]
Honey Juice Usage
import NumPy as NPA = Np.array ([[1, 2, 3 , 4 ", [ 5, 6, 7, 8 9, Ten, one, 12, +, 16]]) print (Np.arange (4 print (Np.full ([1, 4], 1 print (A[np.arange (4), 1 4), [2, 3, 2, 3]] + = 100 print (a)
[0 1 2 3][[1 1 1 12 6][[ 1 2 103 4] [5 6 7 108
] [ 9 111
15 116]]
Boolean
Import= Np.array ([[1, 2, 3, 4], [5, 6, 7, 8], [9, ten, one, and],
[, a > 5 # and this kind of operation??? print(b)print(A[a > 6])
[[False false false] [false true True] [true True True ] [true true] True 7 8 9 10 11 12 13 14 15 16]
Array calculations
import NumPy as NPA = Np.array ([1, 2< Span style= "COLOR: #000000" >]) 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] [38 0.33333333 0.5 ][2 4][4 5 1. 1.41421356]
Matrix Multiplication & Transpose
import NumPy as NPA = Np.array ([1, 2< Span style= "COLOR: #000000" >]) b = Np.array ([3, 4 print (A.dot (b)) # a = Np.array ([[1, 2, 34, 5, 6]]) b = Np.array ([[1, 2, 3], [4, 5, 6 print (b.t) # transpose print (A.dot (b . T)) # matrix multiplication
One[[1 4] [2 5] [3 6]][[] [32 77]]
Sum
Import= Np.array ([[1, 2, 3], [4, 5, 6]])print(A.sum ()) # sum
21st
Various functions http://link.zhihu.com/?target=http%3A//docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html
Broadcasting
A matrix with different rank can be calculated together
import NumPy as NPA = Np.array ([[1, 2, 3 ], [1, 2, 3], [1, 2, 3 = Np.array ([1, 1, 0]) print (A + b) v = Np.array ([1, 2, 3 = 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 Study Notes" 2. Python NumPy's NumPy