编程实践(Numpy)上-学习笔记三(数组的操作-变形)

更改形状

  • numpy.ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即ndim属性(秩)。
  • numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素
  • numpy.ndarray.flatten([order='C'])将数组的副本转换为一维数组,并返回。
    • order:'C' 按行 'F' 按列 'A' 原顺序 'K' 元素 在内存中的出现顺序。(简记)
    • order:{'C/F ,'A,K}可选使用此索引顺序读取a的元素。
    • flatten() 返回的是拷贝
  • numpy.ravel(a, order='C')Return a contiguous flattened array. 返回的是视图;order='F'时为拷贝
  • numpy.reshape(a, newshape[, order='C'])在不更改数据的情况下为数组赋予新的形状。
  • reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
  • reshape()函数当参数newshape = -1时,表示将数组降为一维。

数组转置

  • numpy.transpose(a, axes=None) Permute the dimensions of an array.
  • numpy.ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.

更改维度

  • numpy.newaxis = None None的别名,对索引数组很有用。
  • numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
    • a表示输入的数组;
    • axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;

数组组合

  • numpy.concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis.
  • numpy.stack(arrays, axis=0, out=None)Join a sequence of arrays along a new axis. 沿着新的轴加入一系列数组(stack为增加维度的拼接)
  • numpy.vstack(tup)Stack arrays in sequence vertically (row wise).
  • numpy.hstack(tup)Stack arrays in sequence horizontally (column wise).
    hstack(),vstack()分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于concatenate,用于在已有轴上进行操作。

数组拆分

  • numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays as views into ary.
  • numpy.vsplit(ary, indices_or_sections) Split an array into multiple sub-arrays vertically (row-wise).
  • numpy.hsplit(ary, indices_or_sections) Split an array into multiple sub-arrays horizontally (column-wise).

数组平铺

  • numpy.tile(A, reps) Construct an array by repeating A the number of times given by reps. 将原矩阵横向、纵向地复制
  • numpy.repeat(a, repeats, axis=None) Repeat elements of an array. 重复数组的元素。
    • axis=0,沿着y轴复制,实际上增加了行数。
    • axis=1,沿着x轴复制,实际上增加了列数。
    • repeats,可以为一个数,也可以为一个矩阵。
    • axis=None时就会flatten当前矩阵,实际上就是变成了一个行向量。

添加和删除元素

  • numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None) Find the unique elements of an array. 查找数组的唯一元素。
    • return_index:the indices of the input array that give the unique values
    • return_inverse:the indices of the unique array that reconstruct the input array
    • return_counts:the number of times each unique value comes up in the input array

练习

将 arr转换为2行的2维数组。arr = np.arange(10)

垂直堆叠数组a和数组b。
a = np.arange(10).reshape([2, -1])
b = np.repeat(1, 10).reshape([2, -1])

将数组a与数组b水平堆叠。
a = np.arange(10).reshape([2, -1])
b = np.repeat(1, 10).reshape([2, -1])

将 arr的2维数组按列输出。
arr = np.array([[16, 17, 18, 19, 20],[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35],[26, 27, 28, 29, 30]])

给定两个随机数组A和B,验证它们是否相等。
A = np.random.randint(0,2,5) B = np.random.randint(0,2,5)

在给定的numpy数组中找到重复的条目(第二次出现以后),并将它们标记为True。第一次出现应为False。
a = np.random.randint(0, 5, 10)

建立一个随机数在1-10之间的3行2列的数组,并将其转换成2行3列的数组。

参考:https://github.com/datawhalechina/team-learning-program/tree/master/IntroductionToNumpy/task03 数组的操作-变形

posted @ 2020-10-29 16:59  柔南青空  阅读(354)  评论(0编辑  收藏  举报