当前位置:K88软件开发文章中心编程语言PythonPython01 → 文章内容

Numpy 数组操作

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-11 13:39:15

n')





# 手动使用 broadcast 将 x 与 y 相加b = np.broadcast(x,y)c = np.empty(b.shape)print ('手动使用 broadcast 将 x 与 y 相加:')print (c.shape)print ('\n')c.flat = [u + v for (u,v) in b]print ('调用 flat 函数:')print (c)print ('\n')





# 获得了和 NumPy 内建的广播支持相同的结果print ('x 与 y 的和:')print (x + y)输出结果为:对 y 广播 x:1 41 5广播对象的形状:(3, 3)手动使用 broadcast 将 x 与 y 相加:(3, 3)调用 flat 函数:[[5. 6. 7.] [6. 7. 8.] [7. 8. 9.]]x 与 y 的和:[[5 6 7] [6 7 8] [7 8 9]]numpy.broadcast_tonumpy.broadcast_to 函数将数组广播到新形状。它在原始数组上返回只读视图。 它通常不连续。 如果新形状不符合 NumPy 的广播规则,该函数可能会抛出ValueError。numpy.broadcast_to(array, shape, subok)实例import numpy as npa = np.arange(4).reshape(1,4)print ('原数组:')print (a)print ('\n')print ('调用 broadcast_to 函数之后:')print (np.broadcast_to(a,(4,4)))输出结果为:原数组:[[0 1 2 3]]调用 broadcast_to 函数之后:[[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]]numpy.expand_dimsnumpy.expand_dims 函数通过在指定位置插入新的轴来扩展数组形状,函数格式如下:





numpy.expand_dims(arr, axis)参数说明:arr:输入数组axis:新轴插入的位置实例import numpy as npx = np.array(([1,2],[3,4]))print ('数组 x:')print (x)print ('\n')y = np.expand_dims(x, axis = 0)print ('数组 y:')print (y)print ('\n')print ('数组 x 和 y 的形状:')print (x.shape, y.shape)print ('\n')





# 在位置 1 插入轴y = np.expand_dims(x, axis = 1)print ('在位置 1 插入轴之后的数组 y:')print (y)print ('\n')print ('x.ndim 和 y.ndim:')print (x.ndim,y.ndim)print ('\n')print ('x.shape 和 y.shape:')print (x.shape, y.shape)输出结果为:数组 x:[[1 2] [3 4]]数组 y:[[[1 2] [3 4]]]数组 x 和 y 的形状:(2, 2) (1, 2, 2)在位置 1 插入轴之后的数组 y:[[[1 2]] [[3 4]]]x.ndim 和 y.ndim:2 3x.shape 和 y.shape:(2, 2) (2, 1, 2)numpy.squeezenumpy.squeeze 函数从给定数组的形状中删除一维的条目,函数格式如下:numpy.squeeze(arr, axis) 参数说明:arr:输入数组axis:整数或整数元组,用于选择形状中一维条目的子集实例import numpy as npx = np.arange(9).reshape(1,3,3)print ('数组 x:')print (x)print ('\n')y = np.squeeze(x)print ('数组 y:')print (y)print ('\n')print ('数组 x 和 y 的形状:')print (x.shape, y.shape)输出结果为:数组 x:[[[0 1 2] [3 4 5] [6 7 8]]]数组 y:[[0 1 2] [3 4 5] [6 7 8]]数组 x 和 y 的形状:(1, 3, 3) (3, 3)连接数组函数描述concatenate 连接沿现有轴的数组序列stack 沿着新的轴加入一系列数组。hstack 水平堆叠序列中的数组(列方向)vstack 竖直堆叠序列中的数组(行方向)numpy.concatenatenumpy.concatenate 函数用于沿指定轴连接相同形状的两个或多个数组,格式如下:numpy.concatenate((a1, a2, ...), axis)参数说明:a1, a2, ...:相同类型的数组axis:沿着它连接数组的轴,默认为 0实例import numpy as npa = np.array([[1,2],[3,4]])print ('第一个数组:')print (a)print ('\n')b = np.array([[5,6],[7,8]])print ('第二个数组:')print (b)print ('\n')





# 两个数组的维度相同print ('沿轴 0 连接两个数组:')print (np.concatenate((a,b)))print ('\n')print ('沿轴 1 连接两个数组:')print (np.concatenate((a,b),axis = 1))输出结果为:第一个数组:[[1 2] [3 4]]第二个数组:[[5 6] [7 8]]沿轴 0 连接两个数组:[[1 2] [3 4] [5 6] [7 8]]沿轴 1 连接两个数组:[[1 2 5 6] [3 4 7 8]]numpy.stacknumpy.stack 函数用于沿新轴连接数组序列,格式如下:numpy.stack(arrays, axis)参数说明:arrays相同形状的数组序列axis:返回数组中的轴,输入数组沿着它来堆叠实例import numpy as npa = np.array([[1,2],[3,4]])print ('第一个数组:')print (a)print ('\n')b = np.array([[5,6],[7,8]])print ('第二个数组:')print (b)print ('\n')print ('沿轴 0 堆叠两个数组:')print (np.stack((a,b),0))print ('\n')print ('沿轴 1 堆叠两个数组:')print (np.stack((a,b),1))输出结果如下:第一个数组:[[1 2] [3 4]]第二个数组:[[5 6] [7 8]]沿轴 0 堆叠两个数组:[[[1 2] [3 4]] [[5 6] [7 8]]]沿轴 1 堆叠两个数组:[[[1 2] [5 6]] [[3 4] [7 8]]]numpy.hstacknumpy.hstack 是 numpy.stack 函数的变体,它通过水平堆叠来生成数组。实例import numpy as npa = np.array([[1,2],[3,4]])print ('第一个数组:')print (a)print ('\n')b = np.array([[5,6],[7,8]])print ('第二个数组:')print (b)print ('\n')print ('水平堆叠:')c = np.hstack((a,b))print (c)print ('\n')输出结果如下:第一个数组:[[1 2] [3 4]]第二个数组:[[5 6] [7 8]]水平堆叠:[[1 2 5 6] [3 4 7 8]]numpy.vstacknumpy.vstack 是 numpy.stack 函数的变体,它通过垂直堆叠来生成数组。实例import numpy as npa = np.array([[1,2],[3,4]])print ('第一个数组:')print (a)print ('\n')b = np.array([[5,6],[7,8]])print ('第二个数组:')print (b)print ('\n')print ('竖直堆叠:')c = np.vstack((a,b))print (c)输出结果为:第一个数组:[[1 2] [3 4]]第二个数组:[[5 6] [7 8]]竖直堆叠:[[1 2] [3 4] [5 6] [7 8]]分割数组函数数组及操作split 将一个数组分割为多个子数组hsplit 将一个数组水平分割为多个子数组(按列)vsplit 将一个数组垂直分割为多个子数组(按行)numpy.splitnumpy.split 函数沿特定的轴将数组分割为子数组,格式如下:numpy.split(ary, indices_or_sections, axis)参数说明:ary:被分割的数组indices_or_sections:果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭) axis:沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分实例import numpy as npa = np.arange(9)print ('第一个数组:')print (a)pri

上一页  [1] [2] [3] [4]  下一页


Numpy 数组操作