物联191高锐,1908070103
复盘过程代码:
import numpy as np c = np.arange(1,13).reshape(6,2) array([[ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10], [11, 12]]) np.vsplit(c,3) [array([[1, 2], [3, 4]]), array([[5, 6], [7, 8]]), array([[ 9, 10], [11, 12]])] d = c.T array([[ 1, 3, 5, 7, 9, 11], [ 2, 4, 6, 8, 10, 12]]) np.hsplit(d,3) [array([[1, 3], [2, 4]]), array([[5, 7], [6, 8]]), array([[ 9, 11], [10, 12]])] import numpy as np a = np.array((1,2,4,6)) b = np.array((7,8,9,20)) e = np.dstack((a,b)) ay([[[ 1, 7], [ 2, 8], [ 4, 9], [ 6, 20]]]) np.dsplit(e,2) [array([[[1], [2], [4], [6]]]), array([[[ 7], [ 8], [ 9], [20]]])] inistate =np.array([1,2,3,4]) pre_inistate = inistate[0:3] pre_inistate array([1, 2, 3]) import numpy as np a = np.array([1,1,1,1]) b = np.array([[1],[1],[1],[1]]) a+b array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) c = np.array([[1,1,1,1]]) c+b array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) W = np.array([[1,1,1],[2,2,2]]) W[:,1] array([1, 2]) W[1] array([2, 2, 2]) W[:,1] = np.array([3,3]) W array([[1, 3, 1], [2, 3, 2]]) import numpy as np matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] p1 = np.delete(matrix, 1, 0) # 第0维度(行)第1行被删除(初始行为0行) print('>>>>p1>>>>\n',p1) >>>>p1>>>> [[ 1 2 3 4] [ 9 10 11 12]] p2 = np.delete(matrix, 1, 1) # 第1维度(列)第1行被删除 print('>>>>p2>>>>\n',p2) >>>>p2>>>> [[ 1 3 4] [ 5 7 8] [ 9 11 12]] p3 = np.delete(matrix, 1) # 拉平后删除第1个元素(初始为第0个) print('>>>>p3>>>>\n',p3) >>>>p3>>>> [ 1 3 4 5 6 7 8 9 10 11 12] p4 = np.delete(matrix, [0,1], 1) # 第1维度(列)第0、1行被删除 print('>>>>p4>>>>\n',p4) >>>>p4>>>> [[ 3 4] [ 7 8] [11 12]] import numpy as np matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] q1 = np.insert(matrix, 1, [1,1,1,1], 0) # 第0维度(行)第1行添加[1,1,1,1] print('>>>>q1>>>>\n',q1) >>>>q1>>>> [[ 1 2 3 4] [ 1 1 1 1] [ 5 6 7 8] [ 9 10 11 12]] q2 = np.insert(matrix, 0, [1,1,1], 1) # 第1维度(列)第0列添加1,1,1 print('>>>>q2>>>>\n',q2) >>>>q2>>>> [[ 1 1 2 3 4] [ 1 5 6 7 8] [ 1 9 10 11 12]] q3 = np.insert(matrix, 2, [1,2,3], 1) # 第1维度(列)第2行添加[1,2,3] print('>>>>q3>>>>\n',q3) >>>>q3>>>> [[ 1 2 1 3 4] [ 5 6 2 7 8] [ 9 10 3 11 12]] import numpy as np c = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] m1 = np.append(c,[[2,2,2,2]],axis=0) print('m1----\n',m1) m1---- [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [ 2 2 2 2]] m2 = np.append(matrix,[[1],[1],[100]],axis=1) print('m2-----\n',m2) m2----- [[ 1 2 3 4 1] [ 5 6 7 8 1] [ 9 10 11 12 100]] m3 = np.append(matrix,[1,1,1,1]) print('m3-----\n',m3) m3----- [ 1 2 3 4 5 6 7 8 9 10 11 12 1 1 1 1] import numpy as np a = np.random.choice(8,4) # 从0~8中随机选择4个数组成一维数组 array([4, 5, 7, 7]) b = np.random.choice([0,1,2,23,34,45,56,89,7,55],4) # 从给定list中随机选择4个数组成一维数组 array([34, 56, 1, 7]) c = np.random.choice(np.array([0,1,2,3,4,5,6,7,8,9,10]),4) # 将list换成array数组依然可以运行,效果一致 array([0, 0, 1, 5]) d = np.random.choice([0,1,2,3,4,75,6,55,45,89],5,replace=False) # 上述均有重复,将replace设置为False,即可按要求没有重复的选取 array([89, 2, 55, 1, 6]) f = np.random.choice(np.array([0,1,2,3,4,5,6]),5,p=[0.1,0.1,0.1,0.1,0.1,0.1,0.4]) array([0, 3, 6, 2, 5]) 1 import numpy as np a = np.array([[1,1,1],[2,2,2],[5,3,6],[25,5,4]]) array([[ 1, 1, 1], [ 2, 2, 2], [ 5, 3, 6], [25, 5, 4]]) b1 = np.argmax(a) # 将数组a拉平,最大值索引为12(初始索引为0) b2 = np.argmax(a, axis=0) # 按列选取最大值的索引 array([3, 3, 2], dtype=int64) b3 = np.argmax(a, axis=1) # 按行选取最大值的索引 array([0, 0, 2, 0], dtype=int64) import numpy as np y1 = np.linspace(-5.0,5.0) # 默认生成50个数据 array([-5. , -4.79591837, -4.59183673, -4.3877551 , -4.18367347, -3.97959184, -3.7755102 , -3.57142857, -3.36734694, -3.16326531, -2.95918367, -2.75510204, -2.55102041, -2.34693878, -2.14285714, -1.93877551, -1.73469388, -1.53061224, -1.32653061, -1.12244898, -0.91836735, -0.71428571, -0.51020408, -0.30612245, -0.10204082, 0.10204082, 0.30612245, 0.51020408, 0.71428571, 0.91836735, 1.12244898, 1.32653061, 1.53061224, 1.73469388, 1.93877551, 2.14285714, 2.34693878, 2.55102041, 2.75510204, 2.95918367, 3.16326531, 3.36734694, 3.57142857, 3.7755102 , 3.97959184, 4.18367347, 4.3877551 , 4.59183673, 4.79591837, 5. ]) y2 = np.linspace(1,9,7) # 生成7个数据,包括首尾 array([1. , 2.33333333, 3.66666667, 5. , 6.33333333, 7.66666667, 9. ]) y3 = np.linspace(1,10,7,endpoint=False) # 不包括尾部数据 array([1. , 2.28571429, 3.57142857, 4.85714286, 6.14285714, 7.42857143, 8.71428571]) y4= np.linspace(1, 10, 6, retstep=True) # 将步长与结果的数组放入一个list、 (array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ]), 1.8) import numpy as np x = np.array([[1,2,3],[4,5,6],[1,2,3]]) x.flatten()#行拉平 array([1, 2, 3, 4, 5, 6, 1, 2, 3]) x.ravel() array([1, 2, 3, 4, 5, 6, 1, 2, 3]) x.ravel('F')#列拉平 array([1, 4, 1, 2, 5, 2, 3, 6, 3]) x.flatten('F') array([1, 4, 1, 2, 5, 2, 3, 6, 3]) x.flatten()[1] = 20 array([[1, 2, 3], [4, 5, 6], [1, 2, 3]]) x.ravel()[1] = 20 array([[ 1, 20, 3], [ 4, 5, 6], [ 1, 2, 3]]) x.reshape(1,-1) # 注意结果仍然是二维 array([[ 1, 20, 3, 4, 5, 6, 1, 2, 3]]) x = np.array([1,2,3,6,7,8]) # 注意操作的是数组,原x是数组 x[None,:] # 转成行向量(二维矩阵) array([[1, 2, 3, 6, 7, 8]]) x[:,None] array([[1], [2], [3], [6], [7], [8]]) x[np.newaxis, :] # np.newaxis与None用法一致 array([[1, 2, 3, 6, 7, 8]]) x = np.array([[1,2,3],[2,3,4]]) np.prod(x) np.prod(x,axis=1)#行相乘 array([ 6, 24]) np.prod(x,axis=0)#列相乘 array([ 2, 6, 12]) import numpy as np x = np.array([[1,2,3],[-3,2,4],[5,-2,9]]) array([[ 1, 2, 3], [-3, 2, 4], [ 5, -2, 9]]) 1 y1 = np.maximum(2,x) # 把小于2的元素置2,比改变x的值 array([[2, 2, 3], [2, 2, 4], [5, 2, 9]]) y2 = np.minimum(3,x) # 把大于3的元素置3,不改变x的值 y2 array([[ 1, 2, 3], [-3, 2, 3], [ 3, -2, 3]]) x1 = x.copy() array([[ 1, 2, 3], [-3, 2, 4], [ 5, -2, 9]]) x1[x1 < 2] = 0 # 把小于2的元素置0,改变x1的值 array([[0, 2, 3], [0, 2, 4], [5, 0, 9]]) x2 = x.copy() x2[x2 > 3] = 2 # 把大于3的元素置2,改变x2的值 array([[ 1, 2, 3], [-3, 2, 2], [ 2, -2, 2]]) import numpy as np x = np.array([[1,2,3],[-3,1,4],[1,-2,9]]) array([[ 1, 2, 3], [-3, 1, 4], [ 1, -2, 9]]) x1 = x.copy() # copy(),开辟新地址 x1[x1 > 2] = 555#把大于2的置555,改变x1的值 array([[ 1, 2, 555], [ -3, 1, 555], [ 1, -2, 555]]) array([[ 1, 2, 3], [-3, 1, 4], [ 1, -2, 9]]) x2 = x # 直接等于,未开辟新地址,x2与x相关联 array([[ 1, 2, 3], [-3, 1, 4], [ 1, -2, 9]]) x2[x2>0] = 0#大于零的变为0,x2改变值 array([[ 0, 0, 0], [-3, 0, 0], [ 0, -2, 0]]) array([[ 0, 0, 0], [-3, 0, 0], [ 0, -2, 0]]) x = np.array([[1,2,3],[-3,2,4],[5,-2,9]]) x3 = x[2] # 取x的第3行 array([ 5, -2, 9]) x3[1] = 666 # 将x3第2个元素置666 array([[ 1, 2, 3], [ -3, 2, 4], [ 5, 666, 9]]) port numpy as np x = np.array([[1,2,3],[4,5,6],[1,1,1]]) np.zeros_like(x) # 生成一个和x大小相同的全零矩阵 array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) import numpy as np n = np.random.rand(3,3)#生成一个3行3列的随机矩阵,并且服从【0,1】 array([[0.41149638, 0.61946996, 0.76353632], [0.15538046, 0.9615752 , 0.11827965], [0.62096548, 0.11298423, 0.28300111]]) y = np.multiply(0.1,np.random.randn(2,3))+0.5 # 一般正太分布 array([[0.48038695, 0.40829938, 0.43245035], [0.36174277, 0.51192861, 0.5244684 ]]) import numpy as np z = np.random.randint(2,9,(2,3)) array([[8, 2, 4], [7, 8, 4]]) m = np.random.randint(9,size = (2,3)) array([[7, 1, 2], [1, 3, 3]]) x = 'Who the hell are U?' type(x) assert type(x)==str, 'x is not str' x = [1,2,3,67,89] type(x) import numpy as np A = np.arange(95,104).reshape(3,3) array([[ 95, 96, 97], [ 98, 99, 100], [101, 102, 103]]) np.pad(A,((1,2),(1,3)),'constant',constant_values = (0,0)) array([[ 0, 0, 0, 0, 0, 0, 0], [ 0, 95, 96, 97, 0, 0, 0], [ 0, 98, 99, 100, 0, 0, 0], [ 0, 101, 102, 103, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]]) b = np.array([[[1,2],[3,4]],[[3,4],[7,8]],[[4,5],[1,2]]]) array([[[1, 2], [3, 4]], [[3, 4], [7, 8]], [[4, 5], [1, 2]]]) np.pad(b, ((2,2),(1,1),(3,3)), 'constant', constant_values = 0) array([[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 2, 0, 0, 0], [0, 0, 0, 3, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 3, 4, 0, 0, 0], [0, 0, 0, 7, 8, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 5, 0, 0, 0], [0, 0, 0, 1, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]]) import numpy as np x = np.empty([3,3], dtype = str) print (x) [['' '' ''] ['' '' ''] ['' '' '']] import numpy as np c = np.array([[1,2],[3,4]]) array([[1, 2], [3, 4]]) c.astype(np.float32) array([[1., 2.], [3., 4.]], dtype=float32) import numpy as np x = np.array([1,3,5]) y = np.array([4,6]) XX,YY = np.meshgrid(x,y) array([[1, 3, 5], [1, 3, 5]]) array([[4, 4, 4], [6, 6, 6]]) import numpy as np x = np.array([[3,4,5],[1,3,4],[1,1,1]]) y = np.array([[1,1,1],[2,2,2],[2,2,2]]) np.hstack((x,y)) # 水平堆叠 array([[3, 4, 5, 1, 1, 1], [1, 3, 4, 2, 2, 2], [1, 1, 1, 2, 2, 2]]) np.vstack((x,y)) # 竖直堆叠 array([[3, 4, 5], [1, 3, 4], [1, 1, 1], [1, 1, 1], [2, 2, 2], [2, 2, 2]]) import numpy as np a = np.array([0.11111,0.89568,5.00688]) np.round(a) # 四舍五入取整, np.around 和 round 用法一致 array([0., 1., 5.]) np.round(a,decimals = 2) # 四舍五入保留2位小数 array([0.11, 0.9 , 5.01]) np.floor(a) # 向下取整 array([0., 0., 5.]) np.ceil(a) # 向上取整 array([1., 1., 6.]) import numpy as np c = np.array([1,2,5,4]) c[:,np.newaxis] array([[1], [2], [5], [4]]) c[np.newaxis,:] array([[1, 2, 5, 4]]) import numpy as np a = np.array([[1,2,3],[4,5,6]]) a = np.array([[1,2,3,6],[4,5,6,6]]) a1 = a.reshape((1,2,4)) a1 array([[[1, 2, 3, 6], [4, 5, 6, 6]]]) b = np.array([[3,4,5,6],[1,2,3,4],[4,5,5,5]]) array([[3, 4, 5, 6], [1, 2, 3, 4], [4, 5, 5, 5]]) b1 = b.reshape((1,3,4)).transpose((1,0,2)) array([[[3, 4, 5, 6]], [[1, 2, 3, 4]], [[4, 5, 5, 5]]]) array([[[1, 2, 3, 6], [4, 5, 6, 6]]]) a1+b1 array([[[ 4, 6, 8, 12], [ 7, 9, 11, 12]], [[ 2, 4, 6, 10], [ 5, 7, 9, 10]], [[ 5, 7, 8, 11], [ 8, 10, 11, 11]]]) c = np.array([[[1,2,5],[3,4,6]],[[4,5,6],[7,8,9]]]) array([[[1, 2, 5], [3, 4, 6]], [[4, 5, 6], [7, 8, 9]]]) c.transpose(1,0,2) # 将c的维度按照 第1维度,第0维度,第2维度的排序排成 第0,1,2维度 array([[[1, 2, 5], [4, 5, 6]], [[3, 4, 6], [7, 8, 9]]]) c.transpose(1,2,0) # 将c的维度按照 第1维度,第2维度,第0维度的排序排成 第0,1,2维度 array([[[1, 4], [2, 5], [5, 6]], [[3, 7], [4, 8], [6, 9]]]) import numpy as np a = np.array([2,2,3,4,5,5,6,7]) a[0:7:2] array([2, 3, 5, 6]) a[::-1] array([7, 6, 5, 5, 4, 3, 2, 2]) import numpy as np a = np.array([2,2,3,4,5,5,6,7]) s = slice(0,7,2) a[s] array([2, 3, 5, 6])
大概内容(引用自老师)@Dr.PhyiscsLogical
1. numpy基本加减和取行操作
2. 矩阵删除、插入、尾部添加操作(delete,insert,append)
3. delete()函数
4. insert()函数
5. append()函数
6. np.random.choice(a, size, replace, p)
7. np.argmax(a, axis=None, out=None)
8. 星号(*)的作用
9. ndarray.ndim代表的就是shape元组的长度。
10. numpy.linspace用法
11.拉平操作 ravel()和faltten()及reshape(1,-1)的区别联系(补充[None,:]操作)
12.np.prod() 计算元素乘积
13.把矩阵大于或小于N的元素置M的技巧
14. numpy中的矩阵copy问题
15. np.zeros_like()构造全零矩阵,无需指定大小
random.rand和random.rand和random.randint区别
np.random.random() 生成一个随机数
np.random.uniform()
np.random.rand() 生成均匀分布矩阵
np.random.randn() 生成正太分布矩阵
np.randm.randint() 生成离散均匀分布的整数值组成的矩阵
python 断言 assert
星号( * ) 和 .dot 和 np.multiply 的区别联系
np.pad()函数进行padding操作
numpy.empty() 创建指定形状和数据类型且未初始化的数组
判断两个矩阵元素完全相等&&存在相等
关于 np.mat() 的使用
np.full 用于形成元素全为某元素的矩阵
astype 转换数据类型
np.meshgrid() 快速生成网格
np.hstack() 和 np.vstack() 用于堆叠矩阵
numpy 取整
np.newaxis 在特定位置增加一个维度
python 广播机制
numpy.transpose()转置
numpy 中双冒号的用法