本文为学习莫烦Python中numpy部分所做积累
如需更详细的学习numpy,可以参照numpy官方文档
导入numpy
import numpy as np
创建array
- np.array([?,?,...], dtype=?)
- np.zeros()
- np.ones()
- np.empty()
- np.arange()
- np.linspace()
利用list创建array
array = np.array([[1,2,3],[4,5,6]])
print("array:\n" + str(array))
print("dimension: " + str(array.ndim))
print("shape: " + str(array.shape))
print("size: " + str(array.size))
array:
[[1 2 3]
[4 5 6]]
dimension: 2
shape: (2, 3)
size: 6
控制输出格式,利用format
mat = "{:10}"
print(mat.format("dtype") + mat.format("values"))
print("--------------------------")
a = np.array([2,23,4], dtype=np.int)
print(mat.format("int: ") + str(a))
a = np.array([2,23,4], dtype=np.int32)
print(mat.format("int32: ") + str(a))
a = np.array([2,23,4], dtype=np.float)
print(mat.format("{float: ") + str(a))
a = np.array([2,23,4], dtype=np.float32)
print(mat.format("float32: ") + str(a))
dtype values
--------------------------
int: [ 2 23 4]
int32: [ 2 23 4]
{float: [ 2. 23. 4.]
float32: [ 2. 23. 4.]
创建array的特殊函数(zeros/empty/ones)
print("array")
print(np.array([[2,23,4], [2,32,4]]))
print("zeros")
print(np.zeros((3,4)))
print("empty")
print(np.empty((3,4))) # 空数组,从内存中读取,没有具体的值
print("ones")
print(np.ones((3,4), dtype=np.float))
array
[[ 2 23 4]
[ 2 32 4]]
zeros
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
empty
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
ones
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
其他便捷创建array的函数(arange/linspace & reshape)
print(np.arange(10,20,2))
print(np.arange(10,20))
print(np.arange(5))
[10 12 14 16 18]
[10 11 12 13 14 15 16 17 18 19]
[0 1 2 3 4]
print(np.arange(12).reshape((3,4)))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
print(np.linspace(0,10,51))
[ 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. 2.2
2.4 2.6 2.8 3. 3.2 3.4 3.6 3.8 4. 4.2 4.4 4.6
4.8 5. 5.2 5.4 5.6 5.8 6. 6.2 6.4 6.6 6.8 7.
7.2 7.4 7.6 7.8 8. 8.2 8.4 8.6 8.8 9. 9.2 9.4
9.6 9.8 10. ]
print(np.linspace(1,20,20).reshape((4,5)))
[[ 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]
[ 11. 12. 13. 14. 15.]
[ 16. 17. 18. 19. 20.]]
基本运算
- + - * /
- True Flase
- np.sum()
- np.mean()
- np.cumsum()
- np.var()
- np.std()
- np.argmin()
- np.sort()
a = np.arange(4)
b = np.arange(2,6)
print("a", a)
print("b", b)
print("a+b", a+b)
print("a-b", a-b)
print("a*b", a*b)
print("multiply", np.multiply(a,b.T))
print("dot", np.dot(a,b.T))
print("a**2", a**2)
print("sin(a)", np.sin(a))
a [0 1 2 3]
b [2 3 4 5]
a+b [2 4 6 8]
a-b [-2 -2 -2 -2]
a*b [ 0 3 8 15]
multiply [ 0 3 8 15]
dot 26
a**2 [0 1 4 9]
sin(a) [ 0. 0.84147098 0.90929743 0.14112001]
布尔型
a>1
array([False, False, True, True], dtype=bool)
总和(sum)
print(np.random.random((2,4)))
[[ 0.90737031 0.06079399 0.41431436 0.79081197]
[ 0.73904745 0.39152876 0.11318868 0.69744151]]
a = np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.sum(a,axis=0))
[[ 0.84202053 0.2546691 0.57559723 0.96252647]
[ 0.63965579 0.8549101 0.66205939 0.44804623]]
5.23948484206
[ 1.48167632 1.10957921 1.23765662 1.4105727 ]
标准差(std)与方差(var)
print(np.std(a))
print(np.var(a))
0.217714836034
0.0473997498293
argmin
A = np.arange(2,14).reshape(3,4)
print(A)
print(np.argmin(A)) # 寻找最小值所在位置
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
0
mean/average与cumsum
print(np.mean(A)) # 均值
print(np.average(A)) # 均值
print(np.cumsum(A)) # 累积加和
7.5
7.5
[ 2 5 9 14 20 27 35 44 54 65 77 90]
排序sort
A = np.arange(14, 2, -1).reshape((3,4))
print(A)
print(np.sort(A))
print(np.sort(A, axis = 0))
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 3]]
[[11 12 13 14]
[ 7 8 9 10]
[ 3 4 5 6]]
[[ 6 5 4 3]
[10 9 8 7]
[14 13 12 11]]
索引
数组中:
- 按行输出
- 按列输出
- 挨个输出
A = np.arange(14, 2, -1).reshape((3,4))
print(np.diff(A, axis=0))
print(np.nonzero(A))
[[-4 -4 -4 -4]
[-4 -4 -4 -4]]
(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))
按行输出
for row in A:
print(row)
[14 13 12 11]
[10 9 8 7]
[6 5 4 3]
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 3]]
按列输出
for column in A.T:
print(column)
[14 10 6]
[13 9 5]
[12 8 4]
[11 7 3]
挨个输出
for item in A.flat:
print(item, end="\\")
14\13\12\11\10\9\8\7\6\5\4\3\
print(A.flatten())
[14 13 12 11 10 9 8 7 6 5 4 3]
合并
- np.newaxis
- np.concatenate()
hstack&vstack
A = np.array([[1,1,1]])
B = np.array([[2,2,2]])
print(np.hstack((A, B)))
print(np.vstack((A, B)))
[1 1 1 2 2 2]
[[1 1 1]
[2 2 2]]
newaxis
print(A.T)
print(A.T.shape)
print(A[:,np.newaxis])
print(A[:,np.newaxis].shape)
[1 1 1]
(3,)
[[1]
[1]
[1]]
(3, 1)
concatenate
print(np.concatenate((A,B), axis = 1))
print(np.concatenate((A,B), axis = 0))
[[1 1 1 2 2 2]]
[[1 1 1]
[2 2 2]]
a = np.array([[1,2],[3,4]])
b = np.array([[5,6]])
np.concatenate((a,b), axis = 0)
array([[1, 2],
[3, 4],
[5, 6]])
分割
- np.split()
- np.array_split()
split分割(必须均等,整除)
A = np.arange(12).reshape((3,4))
print(A)
print(np.split(A,2,axis = 1))
print(np.split(A,3,axis = 0))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
array_split分割(可以不均等,不整除)
print(np.array_split(A,3,axis = 1))
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
复制
- 浅复制
- 深复制
a = np.arange(4)
b = a # 浅复制
c = np.copy(a) # 深复制
a[0] = 11111
print(a)
print(b)
print(c)
[11111 1 2 3]
[11111 1 2 3]
[0 1 2 3]