# 1D Array
a = np.array([0, 1, 2, 3, 4])
b = np.array((0, 1, 2, 3, 4))
c = np.arange(5)
d = np.linspace(0, 2*np.pi, 5)
# MD Array,
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[[0, 1], [2, 3]])
a[[0, 2], ...]
a[[0, 2], 1:-1:2]
# Array properties
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)) # >>><class 'numpy.ndarray'>
print(a.dtype) # >>>int64
print(a.size) # >>>25
print(a.shape) # >>>(5, 5)
print(a.itemsize) # >>>8
print(a.ndim) # >>>2
print(a.nbytes) # >>>200
# Basic Operators
a = np.arange(25)
a = a.reshape((5, 5))
b = np.array([10, 62, 1, 14, 2, 56, 79, 2, 1, 45,
4, 92, 5, 55, 63, 43, 35, 6, 53, 24,
56, 3, 56, 44, 78])
b = b.reshape((5,5))
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** 2)
print(a < b) print(a > b)
print(a.dot(b))
print(a.sum())
print(a.min())
print(a.max())
print(a.cumsum())
普通乘除都是元素相乘相除,dot是矩阵乘法
含有 > 、< 返回的就是布尔矩阵
这里的 sum() 等是方法,上面的 size、shape 是属性
a = np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
b = np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
# a为一维数组,b为二维数组
import numpy as np
b = a[a > 5]
mark = np.logical_and(a >= 5, a <= 10)
c = a[mark]
本文章使用limfx的vsocde插件快速发布