1. array结构
test_array = np.array([1,2,3,4])
out: array([3, 4, 5, 6, 7])
print(type(array))
out:<class 'numpy.ndarray'>
test_array .dtype
out:dtype('int32')
print(type(array.dtype))
out:<class 'numpy.dtype'>
test_array .shape
out:(4,)
test_array .itemsize
test_array .size
test_array .ndim
out: 1
test_array .fill(0)
out: array([0, 0, 0, 0, 0])
test_array = np.arange(1,10)
test2_array = np.asarray(test_array, dtype=np.float32)
out: array([1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)
test2_array = test_array.astype(np.float32)
2. 数组的生成
np.array([1,2,3])
test_array = np.arange(1,10)
out: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.linspace(0,10,11)
out: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
test_array = np.zeros((2,3))
out: array([[0., 0., 0.],
[0., 0., 0.]])
np.ones_like(test_array)
out: array([[1., 1., 1.],
[1., 1., 1.]])
test_array.fill(8)
out: array([[8., 8., 8.],
[8., 8., 8.]])
np.identity(3)
out: array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
3. 数组的索引
test_array = np.array([[1,2,3],[4,5,6]])
out:array([[1, 2, 3],
[4, 5, 6]])
test_array[0]
out:array([1, 2, 3])
test_array[0,1]
out: 2
test_array [:1, 1:]
out:array([[2, 3]])
test_array = np.arange(1,10)
out: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
mask = np.array([1,0,3,0,1,0,2,0,0 ], dtype=bool)
out: array([ True, False, True, False, True, False, True, False, False])
array[mask]
out: array([1, 3, 5, 7])
test_array [mask] = 10
out: array([10, 2, 10, 4, 10, 6, 10, 8, 9])
test_array = np.arange(1,10)
test_array > 5
out: array([False, False, False, False, False, True, True, True, True])
test_array = np.arange(1,10)
mask = np.array([1,2,3])
test_array[mask]
out: array([2, 3, 4])
mask = test_array > 5
np.where(mask)
(array([5, 6, 7, 8], dtype=int64),)
4. 数组形状的改变
test_array = np.arange(1,7,1)
test_array = test_array.reshape(2,3)
out: array([[1, 2, 3],
[4, 5, 6]])
test_array = test_array[np.newaxis,...]
test_array.shape
out: (1, 2, 3)
test_array = test_array.squeeze()
test_array.shape
out: (2, 3)
test_array.T
out: array([[1, 4],
[2, 5],
[3, 6]])
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[11,22,33],[44,55,66]])
np.concatenate((a,b))
out: array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 22, 33],
[44, 55, 66]])
np.concatenate((a,b), axis=1)
out: array([[ 1, 2, 3, 11, 22, 33],
[ 4, 5, 6, 44, 55, 66]])
a.flatten()
array([1, 2, 3, 4, 5, 6])
5. 运算
test1_array = test_array
test2_array = test_array.copy()
test_array = np.arange(1,10)
test_array[0] = 10
out: array([10, 2, 3, 4, 5, 6, 7, 8, 9])
test_array[0:5] = 8
out: array([8, 8, 8, 8, 8, 6, 7, 8, 9])
test_array = np.array([[1,2,3],[4,5,6]])
np.sum(test_array)
out: 21
np.sum(test_array, axis=0)
out: array([5, 7, 9])
np.sum(test_array, axis=1)
out: array([ 6, 15])
test_array.prod()
out: 720
test_array.prod(axis=0)
out: array([ 4, 10, 18])
test_array.prod(axis=1)
out:test_array.prod(axis=1)
test_array.min()
out: 1
test_array.min(axis=0)
out: array([1, 2, 3])
test_array.min(axis=1)
out: array([1, 4])
test_array = np.array([[4,2,3],[1,5,6]])
out: 3
test_array.argmin(axis=0)
out: array([1, 0, 0], dtype=int64)
test_array.argmin(axis=1)
out: array([1, 0], dtype=int64)
test_array.mean()
out: 3.5
test_array.mean(axis=0)
out: array([2.5, 3.5, 4.5])
test_array.std()
out: 1.707825127659933
test_array.std(axis=0)
out: array([1.5, 1.5, 1.5])
test_array.clip(2, 4)
out: array([[4, 2, 3],
[2, 4, 4]])
test_array = np.array([1.22, 1.33, 1.55, 1.66])
test_array.round()
out: array([1., 1., 2., 2.])
test_array.round(decimals=1)
array([1.2, 1.3, 1.6, 1.7])
```python
a = np.arange(1,7).reshape(2,3)
out: array([[1, 2, 3],
[4, 5, 6]])
b = np.arange(2,8).reshape(2,3)
out: array([[2, 3, 4],
[5, 6, 7]])
a + b
out: array([[ 3, 5, 7],
[ 9, 11, 13]])
a * b
array([[ 2, 6, 12],
[20, 30, 42]])
a = np.ones((3,4))
b = np.ones((2,1,4))
a = np.ones((3,3))
b = np.zeros(3)
np.logical_and(a,b)
out: array([[False, False, False],
[False, False, False],
[False, False, False]])
a = np.array([[2],[3]])
b = np.array([[4,5]])
np.dot(a,b)
out: array([[ 8, 10],
[12, 15]])
6. 随机模块
np.random.rand(3,2)
np.random.randint(2, 5,size = (2))
out: array([4, 3])
mean, sigma= 0, 0.01
np.random.normal(mean, sigma, 5)
out: array([ 3.96969242e-03, -7.93685844e-05, -1.83804326e-03, 3.73820907e-03, -3.60027281e-03])
np.set_printoptions(precision = 2)
np.random.rand(3)
out: array([0.64, 0.12, 0.51])
np.random.seed(1)
np.random.rand(3)
out: array([4.17e-01, 7.20e-01, 1.14e-04])
np.random.seed(1)
np.random.rand(3)
out: array([4.17e-01, 7.20e-01, 1.14e-04])
test_array = test_array.reshape(5,2)
out: array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
np.random.shuffle(test_array)
test_array
out: array([[4, 5],
[0, 1],
[2, 3],
[6, 7],
[8, 9]])
7. 排序
test_array = np.array([[2,8,6],[9, 5, 2]])
out: array([[2, 8, 6],
[9, 5, 2]])
np.sort(test_array)
out: array([[2, 6, 8],
[2, 5, 9]])
np.sort(test_array, axis=0)
out: array([[2, 5, 2],
[9, 8, 6]])