1、创建数组
import numpy as np
>>> arrayTest=np.array([[1,2,3], [4,5,6]])
>>> print(arrayTest)
[[1 2 3]
[4 5 6]]
>>> print('dim: ', arrayTest.ndim)
dim: 2
>>> print('shape: ', arrayTest.shape)
shape: (2, 3)
>>> print('size: ', arrayTest.size)
size: 6
>>> a = np.array([4,5,6], dtype = np.int)
>>> print(a.dtype)
int32
>>> a = np.array([4,5,6], dtype = np.float32)
>>> print(a.dtype)
float32
>>> a = np.array([4,5,6], dtype = np.float64)
>>> print(a.dtype)
float64
>>> a = np.array([1,2,3], dtype = np.float)
>>> print(a.dtype)
float64
>>> a = np.zeros( (2,3) )
>>> print(a)
[[ 0. 0. 0.]
[ 0. 0. 0.]]
>>> a = np.ones((2,3), dtype = np.int16)
>>> print (a)
[[1 1 1]
[1 1 1]]
>>> a = np.empty((2,3))
>>> print(a)
[[ 0. 0. 0.]
[ 0. 0. 0.]]
>>> a = np.arange(10,20,2)
>>> print(a)
[10 12 14 16 18]
>>> a = np.arange(12).reshape( (3,4) )
>>> print(a)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
>>> a = np.linspace(1,10,6)
>>> print(a)
[ 1. 2.8 4.6 6.4 8.2 10. ]
>>> a = np.array([6,7,8,9,10])
>>> b = np.arange(5)
>>> c = a-b
>>> print(a,b,c)
[ 6 7 8 9 10] [0 1 2 3 4] [6 6 6 6 6]
>>> c=a+b
>>> print(a,b,c)
[ 6 7 8 9 10] [0 1 2 3 4] [ 6 8 10 12 14]
>>> c=a**3
>>> print(a,c)
[ 6 7 8 9 10] [ 216 343 512 729 1000]
>>> c=a*b
>>> print(a,b,c)
[ 6 7 8 9 10] [0 1 2 3 4] [ 0 7 16 27 40]
>>> c=10*np.sin(a)
>>> print(a,c)
[ 6 7 8 9 10] [-2.79415498 6.56986599 9.89358247 4.12118485 -5.44021111]
>>> print(b<3)
[ True True True False False]
>>> a=np.array([[1,2],[4,5]])
>>> b = np.arange(4).reshape(2,2)
>>> c = a * b
>>> c_dot = np.dot(a, b)
或者(>>> c_dot = a.dot(b))
>>> print(a, b, c, c_dot)
[[1 2]
[4 5]]
[[0 1]
[2 3]]
[[ 0 2]
[ 8 15]]
[[ 4 7]
[10 19]]
>>> a = np.random.random((2,4))
>>> print(a)
[[ 0.78855417 0.12055971 0.07196497 0.36303178]
[ 0.30091437 0.43272544 0.88599988 0.38490545]]
>>> print(np.sum(a))
3.34865577967
>>> print(np.sum(a,axis=1))
[ 1.34411063 2.00454515]
>>> print(np.sum(a,axis=0))
[ 1.08946854 0.55328515 0.95796485 0.74793724]
>>>
>>> print(np.min(a))
0.0719649740461
>>> print(np.min(a,axis=0))
[ 0.30091437 0.12055971 0.07196497 0.36303178]
>>> print(np.min(a,axis=1))
[ 0.07196497 0.30091437]
>>>
>>> print(np.max(a))
0.885999877641
>>> print(np.max(a,axis=0))
[ 0.78855417 0.43272544 0.88599988 0.38490545]
>>> print(np.max(a,axis=1))
[ 0.78855417 0.88599988]
>>> import numpy as np
>>> a = np.arange(2,14).reshape(3,4)
>>> print(a)
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
>>> print(np.argmin(a))
0
>>> print(np.argmax(a))
11
>>> print(np.mean(a))
7.5
>>> print(a.mean())
7.5
>>> print(a.average()) error
>>> print(np.average(a))
7.5
>>> print(np.median(a))
7.5
>>> print(np.cumsum(a))
[ 2 5 9 14 20 27 35 44 54 65 77 90]
>>> print(np.diff(a))
[[1 1 1]
[1 1 1]
[1 1 1]]
>>> print(np.nonzero(a))
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
>>> a = np.array([[1,3,2], [4,5,6], [9,8,7]])
>>> print (a)
[[1 3 2]
[4 5 6]
[9 8 7]]
>>> print(np.transpose(a))
[[1 4 9]
[3 5 8]
[2 6 7]]
>>> print(a.T)
[[1 4 9]
[3 5 8]
[2 6 7]]
>>> print((a.T).dot(a))
[[98 95 89]
[95 98 92]
[89 92 89]]
>>> print(np.mean(a,axis=0))
[ 4.66666667 5.33333333 5. ]
>>> print(np.mean(a,axis=1))
[ 2. 5. 8.]
>>> a = np.array([0, 1,2,3,4,5])
>>> print(a)
[0 1 2 3 4 5]
>>> print(a[3])
3
>>> a = np.array([0, 1,2,3,4,5]).reshape(2,3)
>>> print(a)
[[0 1 2]
[3 4 5]]
>>> print(a[1])
[3 4 5]
>>> print(a[1][1])
4
>>> print(a[1,1])
4
>>> print(a[1,:])
[3 4 5]
>>> print(a[:,1])
[1 4]
>>> print(a[1,1:2])
[4]
>>> for row in a:
print(row)
[0 1 2]
[3 4 5]
>>> for col in a.T:
print(col)
[0 3]
[1 4]
[2 5]
>>> print(a.flatten())
[0 1 2 3 4 5]
>>> for item in a.flat:
print(item)
0
1
2
3
4
5
>>> a = np.array([1,1,1])
>>> b = np.array([2,2,2])
>>> print(np.vstack((a,b)))
[[1 1 1]
[2 2 2]]
>>> print(a.shape, b.shape)
(3,) (3,)
>>> c = np.vstack((a,b))
>>> d = np.hstack((a,b))
>>>
>>> print(c)
[[1 1 1]
[2 2 2]]
>>> print(d)
[1 1 1 2 2 2]
>>> print(a.shape, d.shape)
(3,) (6,)
>>> print(a[:,np.newaxis])
[[1]
[1]
[1]]
>>> a = np.array([1,1,1])[:,np.newaxis]
>>> b = np.array([2,2,2])[:,np.newaxis]
>>>
>>> c = np.concatenate((a,b,b,a),axis=1)
>>> print(a,b,c)
[[1]
[1]
[1]]
[[2]
[2]
[2]]
[[1 2 2 1]
[1 2 2 1]
[1 2 2 1]]
>>> d = np.concatenate((a,b,b,a),axis=0)
>>> print(d)
[[1]
[1]
[1]
[2]
[2]
[2]
[2]
[2]
[2]
[1]
[1]
[1]]