-
数组的创建
-
数组的访问
-
数组的合并
-
数组的分割
数组创建
>>> import numpy as np
创建一维数组
>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
创建二维数组
>>> X = np.arange(10).reshape(2, 5)
>>> X
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
查看数组为维度
>>> x.ndim
1
>>> X.ndim
2
查看数组的形状
>>> X.shape
(2, 5)
数组访问
>>> X
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> X[0]
array([0, 1, 2, 3, 4])
>>> X[1,1]
6
>>> X[0:4]
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> X[0:1]
array([[0, 1, 2, 3, 4]])
>>> X[0:2]
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> X[:2, :2]
array([[0, 1],
[5, 6]])
>>> X[:, 1]
array([1, 6])
>>> X[1, :]
array([5, 6, 7, 8, 9])
创建子数组
>>> subX = X[:2, :2]
>>> subX
array([[0, 1],
[5, 6]])
子数组修改
>>> subX[0, 0] = 100
>>> subX
array([[100, 1],
[ 5, 6]])
>>> X
array([[100, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9]])
如何使子数组的修改不影响原数组
>>> subX = X[:2, :2].copy()
>>> subX
array([[100, 1],
[ 5, 6]])
>>> subX[0, 1] = 200
>>> subX
array([[100, 200],
[ 5, 6]])
>>> X
array([[100, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9]])
数组形状
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x.reshape(2, 5)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> x.reshape(5, 2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> A = x.reshape(5, 2)
>>> A
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> x.reshape(10, -1)
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
>>> x.reshape(-1, 10)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
数组合并
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> a,b
(array([1, 2, 3]), array([4, 5, 6]))
>>> np.concatenate([a,b])
array([1, 2, 3, 4, 5, 6])
>>> c = np.array([7,8,9])
>>> np.concatenate([a,b,c])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> A = np.array([[1,2,3],[4,5,6]])
>>> np.concatenate([A, A])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
>>> np.concatenate([A, A], axis=0)
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
>>> np.concatenate([A, A], axis=1)
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
不能合并两个维度不同的数组
>>> np.concatenate([A, a])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
如何忽略维度问题
>>> np.concatenate([A, a.reshape(1, -1)])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
>>> A,a
(array([[1, 2, 3],
[4, 5, 6]]), array([1, 2, 3]))
>>> A.shape, a.shape
((2, 3), (3,))
>>> np.vstack([A, a])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
>>> a = np.array([[6],[6]])
>>> a
array([[6],
[6]])
>>> np.hstack([A, a])
array([[1, 2, 3, 6],
[4, 5, 6, 6]])
数组分割
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x1,x2,x3 = np.split(x, [3,7])
>>> x1,x2,x3
(array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9]))
>>> A = np.arange(16).reshape(4,4)
>>> A
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> A1,A2 = np.split(A, [2])
>>> A1,A2
(array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]]))
>>> A1,A2 = np.split(A,[2],axis=1)
>>> A1,A2
(array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]]))
>>> A1, A2 = np.vsplit(A, [2])
>>> A1,A2
(array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]]))
>>> A1,A2 = np.hsplit(A,[2])
>>> A1,A2
(array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]]))