A = np.arange(12).reshape((3, 4)) # 水平方向的长度是4
print(np.split(A, 4, axis=1)) # 参数必须是可均分的,vertically均分成四块,分垂直风向,相当于竖着切举证
print(np.array_split(A, 3, axis=1)) # 可以不必是均分的 2+1+1
print(np.vsplit(A, 3)) # vertical 分垂直方向
print(np.hsplit(A, 4)) # 分水平方向
a = np.arange(4)
b = a # a, b, c指向同样的地址内容
c = b
print(c is a) # True
b = a.copy() # b是一个新的存储区域
print(b is a) # False
a = np.arange(0, 8, 2)
print(a) # [0 2 4 6]
print(a.shape) # (4,)
print(a[3]) # 6
b = np.arange(6).reshape(2, 3)
print(b) # [[0 1 2] [3 4 5]]
print(b.shape) # (2, 3)
print(b[1][0]) # 3
print(b[1, 0]) # 3
c = np.arange(24).reshape(2, 3, 4)
print(c)
> [[[ 0 1 2 3]
> [ 4 5 6 7]
> [ 8 9 10 11]]
> [[12 13 14 15]
> [16 17 18 19]
> [20 21 22 23]]]
print(c.shape) # (2, 3, 4)
print(c[0, 1, 2]) # 6
print(c[0][1][2]) # 6
print(np.ones(2))
print(np.ones((2)))
print(np.ones((2,))) # 一维时括号和逗号都可以省略
print(np.ones((2, 3)))
print(np.ones((2, 3, 4))) # 全1
print(np.full((2, 2), 3)) # 全3矩阵,[[3 3] [3 3]]
print(np.eye(2))
print(np.eye((2))) # 单位矩阵,只有对角非零且全为1的方正矩阵,括号可以省略
print(np.empty((2,3)))
print(np.empty_like(c)) # 空矩阵的内容由缓存状态决定
print(np.linspace(0, 10, num=5)) # [ 0. 2.5 5. 7.5 10. ]
my_random_array = np.random.random(5)
my_random_array = np.random.random((5))
my_random_array = np.random.random((5,)) # 一维时括号和逗号都可以省略
print(my_random_array) # [0.26326548 0.92779013 0.81319706 0.62325733 0.61494552]
my_random_array = np.random.random((1, 2))
print(my_random_array) # [[0.73278507 0.12801832]]
print([[0, 1], [2, 3]][0][0]) # 0
print([[0, 1], [2, 3]][0]) # [0, 1]
print([[0, 1], [2, 3]]) # [[0, 1], [2, 3]]
print(np.array([[0, 1], [2, 3]][0][0])) # 0
print(np.array([[0, 1], [2, 3]][0])) # [0 1]
print(np.array([[0, 1], [2, 3]])) # [[0 1] [2 3]]
print(type([[0, 1, 2], [3, 4, 5]][0][0])) #
print(type([[0, 1, 2], [3, 4, 5]][0])) #
print(type([[0, 1, 2], [3, 4, 5]])) #
print(type(np.array([[0, 1, 2], [3, 4, 5]])[0][0])) #
print(type(np.array([[0, 1, 2], [3, 4, 5]])[0])) #
print(type(np.array([[0, 1, 2], [3, 4, 5]]))) #
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])[0][0])) # ()
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])[0])) # (3,)
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]]))) # (2, 3)
print(np.random.random((2, 2))) # [[0.09242512 0.31837721] [0.13707168 0.31265585]]
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]])
print(my_array) # [[1 2 3 4 5] [6 7 8 9 0]]
print(my_array.shape) # (2, 5)
print(my_array[0]) # [1 2 3 4 5]
print(my_array[1]) # [6 7 8 9 0]
print(my_array[0][3]) # 4
print(my_array[0, :]) # [1 2 3 4 5] 上面的方法和这种表示方法效果相同
print(my_array[1, :]) # [6 7 8 9 0]
print(my_array[0, 3]) # 4
print(my_array[:][0]) # [1 2 3 4 5] [:]不起作用,需要忽略
print(my_array[:][1]) # [6 7 8 9 0]
print(my_array[0][:]) # [1 2 3 4 5] [:]不起作用,需要忽略
print(my_array[1][:]) # [6 7 8 9 0]
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 14, 14, 15]])
print(my_array[0])
print(my_array[1])
print(my_array[0, ])
print(my_array[1, ])
print(my_array[0, ...]) # [1 2 3 4 5]
print(my_array[1, ...]) # [6 7 8 9 0]
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 14, 14, 15]])
print(my_array[[0], ])
print(my_array[[1], ])
print(my_array[[0], :])
print(my_array[[1], :])
print(my_array[[0], ...]) # [[1 2 3 4 5]]
print(my_array[[1], ...]) # [[6 7 8 9 0]]
print(my_array[0, 0]) # 1
print(my_array[[0], [0]]) # [1]
print(my_array[[0], [0, 2]]) # [1 3]
print(my_array[[0, 1], [2, 0]]) # [3 6]
a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])
sum = a + b # [[ 6. 8.] [10. 12.]] 对应相加
difference = a - b # [[-4. -4.] [-4. -4.]]
product = a * b # [[ 5. 12.] [21. 32.]] 对应相乘
quotient = a / b # [[0.2 0.33333333] [0.42857143 0.5 ]]
matrix_product = a.dot(b) # [[19. 22.] [43. 50.]] 矩阵的乘法
a = np.array([0, 1, 2, 3, 4]) # [0 1 2 3 4]
b = np.array((0, 1, 2, 3, 4)) # [0 1 2 3 4]
c = np.arange(5) # [0 1 2 3 4]
d = np.linspace(0, 2*np.pi, 5) # [0. 1.57079633 3.14159265 4.71238898 6.28318531]
print(np.arange(11, 36).reshape(5, 5))
a = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
print(a) # [[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]
[31 32 33 34 35]]
print(a[2, 4]) # 25
print(a[0, 1:4]) # [12 13 14]
print(a[1:4, 0]) # [16 21 26]
print(a[::2, ::2]) # [[11 13 15]
[21 23 25]
[31 33 35]]
print(a[:, 1]) # [12 17 22 27 32]
a = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
print(type(a)) # 类型
print(a.dtype) # int64 一个元素64位
print(a.size) # 25 一共25个元素
print(a.shape) # (5, 5)
print(a.itemsize) # 8 一个元素大小为8个字节
print(a.ndim) # 2 2维数组
print(a.nbytes) # 200 数组大小为200字节=25个*8字节
a = np.arange(4) # [0 1 2 3]
a = a.reshape(2, 2) # [[0 1] [2 3]]
b = np.array([3, 2, 1, 0]) # [3 2 1 0]
b = b.reshape((2, 2)) # [[3 2] [1 0]]
print(b+1) # [[4 3] [2 1]]
print(a + b) # [[3 3] [3 3]]
print(a - b) # [[-3 -1][ 1 3]]
print(a * b) # [[0 2] [2 0]]
print(a / (b+1)) # [[0. 0.33333333] [1. 3.]]
print(a ** 2) # [[0 1] [4 9]]
print(a <= b) # [[ True True] [False False]]
print(a.dot(b)) # [[1 0] [9 4]]
a = np.arange(10)
print(a) # [0 1 2 3 4 5 6 7 8 9]
print(a.sum()) # 45
print(a.min()) # 0
print(a.max()) # 9
print(a.cumsum()) # [ 0 1 3 6 10 15 21 28 36 45]
a = np.arange(0, 100, 10)
indices = [1, 2, 5, -1]
b = a[indices]
print(a) # [ 0 10 20 30 40 50 60 70 80 90]
print(b) # [10 20 50 90]
import matplotlib.pyplot as plt
import numpy as np
a = np.linspace(0, 2 * np.pi, 11)
b = np.sin(a)
plt.plot(a, b)
mask = b >= 0
print(mask) # [ True True ..... False False ]
print(a[mask]) # [0. 0.12822827 ...... 2.94925025 3.07747852]
print(b[mask]) # [0. 0.12787716 ...... 0.19115863 0.06407022]
plt.plot(a[mask], b[mask], 'bo')
mask = (b >= 0) & (a <= np.pi / 2)
plt.plot(a[mask], b[mask], 'go')
plt.show()
a = np.arange(0, 100, 10) # [ 0 10 20 30 40 50 60 70 80 90]
b = a[:5] # [ 0 10 20 30 40]
c = a[a >= 50] # [50 60 70 80 90]
a = np.arange(0, 100, 10)
b = np.where(a < 50)
c = np.where(a < 50)[0]
print(a) # [ 0 10 20 30 40 50 60 70 80 90]
print(b) # (array([0, 1, 2, 3, 4]),)
print(b[0]) # [0 1 2 3 4]
print(c) # [0 1 2 3 4]
END
posted @ 2019-02-23 22:58 YangZhaonan 阅读(...) 评论(...) 编辑 收藏