numpy —科学计算库
结构核心:
ndarray对象:
内存是连续的
存储单一数据类型
多维数组
两种存储风格:
C风格 ---C语言风格----按行优先存储
F风格 ---fortran语言风格 ----按列优先存储
数组的创建
import numpy as np
arr = np.array([1,2,3,4])
print("arr:\n",arr)
print("arr的类型:\n",type(arr))
arr = np.arange(0, 5, 1)
arr = np.arange(5)
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.linspace(0, 5, 4)
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.logspace(0,2,3,base=2)
print("arr:\n",arr)
print("arr的类型:\n",type(arr))
arr = np.zeros(shape=(2,2))
print("arr:\n",arr)
print("arr的类型:\n",type(arr))
arr = np.ones(shape=(2,2))
print("arr:\n",arr)
print("arr的类型:\n",type(arr))
arr = np.diag(v=[1,2,3,4],k=0)
arr = np.diag(v=[1,2,3,4],k=1)
arr = np.diag(v=[1, 2, 3, 4], k=-1)
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("x:\n", x)
arr = np.diag(v=x, k=0)
arr = np.diag(v=x, k=1)
arr = np.diag(v=x, k=-1)
arr = np.diag(v=x, k=5)
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.eye(N=2)
arr = np.eye(N=2,M=3)
arr = np.eye(N=3,M=2)
arr = np.eye(N=3,M=2,k=0)
arr = np.eye(N=3,M=2,k=-1)
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.random.rand(10)
arr = np.random.rand(2,2,3)
arr = np.random.rand((2,3))
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.random.randn(2,3)
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.random.randint(low=0,high=1,size=10)
arr = np.random.randint(low=0,high=1,size=(2,3))
arr = np.random.randint(low=0, high=1, size=[2, 3])
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
arr = np.random.uniform(low=1,high=10,size=(5,5))
print("arr:\n", arr)
print("arr的类型:\n", type(arr))
数组的属性
import numpy as np
arr = np.array([1, 2, 3, 4])
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 0]])
arr = np.array([[[1, 2, 3, 4], [5, 6, 7, 0]],[[1, 2, 3, 4], [5, 6, 7, 0]]])
print("arr:\n", arr)
print("arr 的类型:\n", type(arr))
print("arr 的维度:\n", arr.ndim)
print("arr 的形状:\n", arr.shape)
print("arr 的元素的个数:\n", arr.size)
print("arr 的元素的类型:\n", arr.dtype)
print("arr 的每一个元素的大小:\n", arr.itemsize)
数组的类型及形状
import numpy as np
arr = np.array([1, 2, 3, 4], dtype=np.float32)
print("arr:\n", arr)
print("arr 的元素类型:\n", arr.dtype)
res = np.bool(0)
res = np.float32(5)
print("res:\n",res)
arr = arr.astype(np.int32)
print("arr:\n", arr)
print("arr 的数据类型:\n", arr.dtype)
arr = np.arange(16)
print("arr:\n", arr)
print("arr 的形状:\n", arr.shape)
arr.shape = (4, 4)
print("arr:\n", arr)
print("arr 的形状:\n", arr.shape)
arr = arr.reshape((4,4))
print("arr:\n", arr)
print("arr 的形状:\n", arr.shape)
arr = arr.reshape((-1,1))
print("arr:\n", arr)
print("arr 的形状:\n", arr.shape)
数组的索引
import numpy as np
arr = np.arange(1, 10)
print("arr:\n", arr)
print("获取arr里面的6:", arr[5])
print("获取arr里面的9:", arr[-1])
print("获取arr里面的6和9 :", arr[[5, -1]])
print("获取arr里面的6:", arr[5:6])
print("获取arr里面的9:", arr[-1:])
print("获取arr里面的6和9 :", arr[5::3])
arr = np.arange(1, 17).reshape((4, 4))
print("arr:\n", arr)
print("获取arr里面的6:", arr[1, 1])
print("获取arr里面的11 :", arr[2, 2])
print("获取arr 里面的6 和11 这两个元素:", arr[[1, 2], [1, 2]])
print("获取arr 里面的8 和15 这个两个元素:", arr[[1, 3], [3, 2]])
print("获取arr 里面的6 这个元素:", arr[1:2, 1:2])
print("获取arr里面的11 :", arr[2:3, 2:3])
print("获取arr 里面的6 7 和10 11 这两个元素:", arr[1:3, 1:3])
print("获取 11 12 15 16 这4个元素", arr[2:, 2:])
print("获取 11 12 15 16 这4个元素", arr[[2, 2, 3, 3], [2, 3, 2, 3]])
print("获取4 8 12 16 这一列", arr[:, 3:])
print("获取4 8 12 16 这一列", arr[:, 3])
arr = np.arange(1, 17).reshape((4, 4))
print("arr:\n", arr)
bool_index = np.array([0, 1, 1, 0], dtype=np.bool)
print('bool_index:\n', bool_index)
print(arr[bool_index, :])
print(arr[:, bool_index])
print(arr[bool_index, bool_index])
数组的展开
import numpy as np
arr = np.arange(1, 17).reshape((4, 4))
print("arr:\n", arr)
arr = arr.reshape((16,))
print("arr:\n", arr)
arr.shape = (16,)
print("arr:\n", arr)
np.ravel(arr,"C")
arr = arr.flatten('C')
arr = arr.flatten('F')
arr = arr.ravel('C')
arr = arr.ravel('F')
print("arr:\n", arr)
数组的合并与拆分
import numpy as np
arr1 = np.array([[9, 0], [8, 9]])
arr2 = np.arange(4).reshape((2, 2))
print("arr1:\n", arr1)
print("arr2:\n", arr2)
res = np.vstack((arr1, arr2))
print("res:\n",res)
res = np.hstack((arr1,arr2))
print("res:\n",res)
res = np.concatenate((arr1, arr2),axis=0)
res = np.concatenate((arr1, arr2),axis=1)
print("res:\n",res)
arr = np.arange(1, 17).reshape((4, 4))
print("arr:\n", arr)
res = np.hsplit(arr,2)
print("res:\n",res)
res = np.vsplit(arr,2)
print("res:\n",res)
res = np.split(arr, 2, axis=0)
res = np.split(arr, 2, axis=1)
print("res:\n", res)
res = np.split(arr, [1, 3], axis=0)
print("res:\n", res)
res1 = arr[:, :1]
res2 = arr[:, 1:]
print("res1:\n", res1)
print("res2:\n", res2)
矩阵的创建
import numpy as np
m1 = np.mat("1 2 3;4 5 6;7 8 9")
m1 = np.mat([[1, 2], [3, 4]])
m1 = np.mat(np.array([[1, 2], [3, 4]]))
print("m1:\n", m1)
print("m1 的类型:\n", type(m1))
print("m1 的维度:\n",m1.ndim)
print(m1[0,:])
m1 = np.matrix("1 2 3;4 5 6;7 8 9")
m1 = np.matrix([[1, 2], [3, 4]])
m1 = np.matrix(np.array([[1, 2], [3, 4]]))
print("m1:\n", m1)
print("m1 的类型:\n", type(m1))
m1 = np.bmat(np.array([[1, 2], [3, 4]]))
m1 = np.bmat([[1, 2], [3, 4]])
m1 = np.bmat("1 2 3;4 5 6;7 8 9")
print("m1:\n", m1)
print("m1 的类型:\n", type(m1))
m1 = np.mat(np.array([[1, 2], [2, 1]]))
m2 = np.mat(np.arange(4).reshape((2, 2)))
print("m1:\n", m1)
print("m2:\n", m2)
m = np.bmat('m1 m2;m2 m1')
m = np.bmat([[m1, m2], [m2, m1]])
print("m:\n", m)
矩阵的运算及属性
import numpy as np
m1 = np.mat(np.array([[1, 3], [4, 5]]))
m2 = np.mat(np.array([[1, 2], [1, 3]]))
print("m1:\n", m1)
print("m2:\n", m2)
m3 = m1 * 2
print("m3:\n", m3)
print("m3 的类型:\n", type(m3))
m3 = m1 + m2
m3 = m1 - m2
print("m3:\n", m3)
print("m3 的类型:\n", type(m3))
m3 = m1 * m2
m3 = np.matmul(m1, m2)
m3 = np.dot(m1, m2)
print("m3:\n", m3)
print("m3 的类型:\n", type(m3))
m3 = np.multiply(m1,m2)
print("m3:\n", m3)
print("m3 的类型:\n", type(m3))
print("m1 的转置为:\n",m1.T)
print("m1 的共轭转置为:\n",m1.H)
print("m1 的视图为:\n", m1.A)
print("m1 的视图的类型为:\n", type(m1.A))
print("m1 的逆矩阵为:\n",m1.I)
print("m1 * m1 的逆的值为:\n",np.matmul(m1,m1.I))
数组的全通用函数
import numpy as np
"""
数组的全通用函数---对数组的每一个元素都进行操作的函数 ----数组的形状必须是一样的
"""
arr1 = np.array([[2, 1], [5, 6]])
arr2 = np.array([[0, 1], [1, 0]])
print("arr1:\n", arr1)
print("arr2:\n", arr2)
print("*" * 100)
print("数组相加:\n", arr1 + arr2)
print("数组相减:\n", arr1 - arr2)
print("数组相乘:\n", arr1 * arr2)
print("数组相除:\n", arr2 / arr1)
print("数组相幂:\n", arr1 ** arr2)
print("arr1 > arr2 :\n", arr1 > arr2)
print("arr1 != arr2:\n", arr1 != arr2)
print(" arr1 >= arr2 :\n",arr1 >= arr2)
print(" arr1 == arr2 :\n", arr1 == arr2)
print(" arr1 < arr2 :\n", arr1 < arr2)
print(np.all(arr1 >= arr2))
print(np.all(arr1 > arr2))
print(np.any(arr1 == arr2))
print(np.any(arr1 < arr2))
数组的保存与读取
import numpy as np
arr = np.arange(16).reshape((4, 4))
print("arr:\n", arr)
arr1 = np.array([1, 2, 3, 4, 5])
print("arr1 :\n", arr1)
np.save("./arr",arr)
print("保存完毕")
res = np.load("./arr.npy")
print(res)
np.savez("./arr", arr, arr1)
print("保存完毕")
res = np.load("./arr.npz")
print("res:\n",res)
for tmp in res:
print(tmp)
a = res["arr_0"]
b = res["arr_1"]
print("a:\n",a)
print("b:\n",b)
np.savez("./arr_key", k1=arr, k2=arr1)
print("保存完毕")
res = np.load("./arr_key.npz")
for tmp in res:
print(tmp)
k1 = res["k1"]
k2 = res['k2']
print("k1:\n",k1)
print("k2:\n",k2)
np.savetxt("./arr.txt",arr, fmt='%d',delimiter=",")
print("保存完毕")
res = np.loadtxt("./arr.txt",dtype=int,delimiter=",")
print("res:\n",res)
res = np.genfromtxt("./arr.txt",dtype=int,delimiter=",")
print("res:\n",res)
数组的排序
import numpy as np
arr = np.array([5, 7, 2, 1, 0])
arr = np.array([[2, 1, 0], [5, 0, 1], [0, 2, -1]])
print(arr)
arr.sort(axis=1)
arr.sort(axis=0)
print("arr排序之后的结果:\n", arr)
res = np.argsort(arr,axis=0)
print("res:\n",res)
a = np.array([3, 2, 6, 4, 5])
b = np.array([50, 30, 40, 20, 10, ])
c = np.array([400, 300, 600, 100, 200])
res = np.lexsort([a,b,c],axis=0)
res = np.lexsort([a,c,b],axis=0)
res = np.lexsort([b, c, a], axis=0)
print(res)
b_sorted = [b[i] for i in res]
print('b_sorted:\n', b_sorted)