定义数组
arr1 = np.array([1, 2, 3])
print("arr1:", arr1)
arr2 = np.array([[1, 2], [2, 4], [3, 5]])
print("arr2:", arr2)
arr3 = np.array([1, 2, 3, 4, 5], ndmin=3)
print("arr3", arr3)
arr4 = np.array([1, 2, 3], dtype='float32')
print("arr4", arr4)
dt1 = np.dtype(np.int32)
print("dt1", dt1)
dt2 = np.dtype('i1')
print("dt2", dt2)
dt3 = np.dtype('<i8')
print("dt3", dt3)
student = np.dtype([('stuId', 'i1'), ('stuName', 'U20'), ('age', 'i1')])
print('student', student)
stuarr = np.array([(1, "肖迷笛", 20), (2, "吾爱人", 22)], dtype=student)
print("stuarr", stuarr)
a = np.array([[[1, 2], [1, 4]], [[1, 3], [1, 0]], [[1, 2], [1, 4]]], dtype=np.int)
print('a ndim', a.ndim)
print('a shape', a.shape)
b = a.reshape(2, 6)
print(b, b.dtype, b.size, type(b))
相关基础
zero = np.zeros((3, 4), dtype=np.int)
print(zero)
a = np.ones((2, 3), dtype=int)
print("填充1的数组", a)
range = np.arange(10, 100, 5)
print('range', range)
rangeF = np.arange(0, 10, 2.5)
print('rangeF', rangeF)
linspace = np.linspace(0, 2, 9)
print(linspace)
from numpy import pi
x = np.linspace(0, 2 * pi, 10)
print(x)
f = np.sin(x)
print(f)
arr = np.arange(10000)
arrReshape = arr.reshape(100, 100)
强制打印整个数组,不进行省略
import sys
np.set_printoptions(threshold=sys.maxsize)
print(arrReshape)
基本操作
a = np.array([20, 30, 40, 50])
b = np.arange(4)
print(b, a + b, a - b)
print(10 * a, b ** 2, 10 * np.sin(a))
print(a < 32)
A = np.array([[1, 1], [0, 1]])
B = np.array([[2, 0], [3, 4]])
print(A * B)
print(A @ B)
print(A.dot(B))
a = np.random.random((2, 3))
print(a.sum(), a.min(), a.max(), a)
b = np.arange(12).reshape(3, 4)
print(b, "axis", b.sum(axis=0))
print(b.max(axis=1))
print(b.min(axis=0))