【Numpy】array操作总结

  • 官方Document: https://www.numpy.org/devdocs/reference/routines.array-manipulation.html
  • 开发测试环境
    • Win10
    • Python 3.6.4
    • NumPy 1.14.2

Basic operations


函数原型作用
[copyto](dst, src[, casting, where])Copies values from one array to another, broadcasting as necessary.

Changing array shape


函数原型作用
[reshape](a, newshape[, order])Gives a new shape to an array without changing its data.
[ravel](a[, order])Return a contiguous flattened array.
[ndarray.flat]A 1-D iterator over the array.
ndarray.flattenReturn a copy of the array collapsed into one dimension.

Transpose-like operations


函数原型作用
[moveaxis](a, source, destination)Move axes of an array to new positions.
[rollaxis](a, axis[, start])Roll the specified axis backwards, until it lies in a given position.
[swapaxes](a, axis1, axis2)Interchange two axes of an array.
[ndarray.T]Same as self.transpose(), except that self is returned if self.ndim < 2.
[transpose](a[, axes])Permute the dimensions of an array.

Changing number of dimensions


函数原型作用
atleast_1d(*arys)Convert inputs to arrays with at least one dimension.
atleast_2d(*arys)View inputs as arrays with at least two dimensions.
atleast_3d(*arys)View inputs as arrays with at least three dimensions.
broadcastProduce an object that mimics broadcasting.
broadcast_to(array, shape[, subok])Broadcast an array to a new shape.
broadcast_arrays(*args, **kwargs)Broadcast any number of arrays against each other.
expand_dims(a, axis)Expand the shape of an array.
squeeze(a[, axis])Remove single-dimensional entries from the shape of an array.

expand_dims

扩展array的shape, 插入一个新的轴,该轴将出现在扩展阵列形状的轴位置

>>> x = np.array([1,2])
>>> x.shape
(2,)

以下操作相当于 x[np.newaxis,:] or x[np.newaxis]:

>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = np.expand_dims(x, axis=1)  # Equivalent to x[:,np.newaxis]
>>> y
array([[1],[2]])
>>> y.shape
(2, 1)

注意在一些列子中使用None替换np.newaxis

>>> np.newaxis is None
True

squeeze

从数组的形状中移除一维条目
- a : array
- axis: None或者整数或者整数元组,默认是None
选择shape单维度的条目,若选取的axis的shape条目不为1,则会抛出异常

>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=0).shape
(3, 1)
>>> np.squeeze(x, axis=1).shape
Traceback (most recent call last):
...
ValueError: cannot select an axis to squeeze out which has size not equal to one
>>> np.squeeze(x, axis=2).shape
(1, 3)

axis参数输入为整数列表时,起作用相当于axis=None时的作用

Changing kind of array


函数原型作用
[asarray](a[, dtype, order])Convert the input to an array.
[asanyarray](a[, dtype, order])Convert the input to an ndarray, but pass ndarray subclasses through.
[asmatrix](data[, dtype])Interpret the input as a matrix.
[asfarray](a[, dtype])Return an array converted to a float type.
[asfortranarray](a[, dtype])Return an array laid out in Fortran order in memory.
[ascontiguousarray](a[, dtype])Return a contiguous array in memory (C order).
[asarray_chkfinite](a[, dtype, order])Convert the input to an array, checking for NaNs or Infs.
asscalarConvert an array of size 1 to its scalar equivalent.
[require](a[, dtype, requirements])Return an ndarray of the provided type that satisfies requirements.

Joining arrays


函数原型作用
concatenate((a1, a2, …)[, axis, out])Join a sequence of arrays along an existing axis.
stack(arrays[, axis, out])Join a sequence of arrays along a new axis.
column_stack(tup)Stack 1-D arrays as columns into a 2-D array.
dstack(tup)Stack arrays in sequence depth wise (along third axis).
hstack(tup)Stack arrays in sequence horizontally (column wise).
vstack(tup)Stack arrays in sequence vertically (row wise).
block(arrays)Assemble an nd-array from nested lists of blocks.

concatenate

沿着指定的维度进行合并,结果是该维度上shape增加

  • a1, a2, … : array序列,除了axis对应的维度外,其他shape相同
  • axis : 沿着axis维进行结合,结合后这个维的shape是序列这个维的shape之和
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> a.shape, b.shape
((2, 2), (1, 2))
>>> c = np.concatenate((a, b), axis=0) # (2,2) (1,2) =>(3,2)
>>> c.shape
(3, 2)
>>> c
array([[1, 2],[3, 4],[5, 6]])
>>> d = np.concatenate((a, b.T), axis=1)
>>> d
array([[1, 2, 5],[3, 4, 6]])
>>> d.shape
(2, 3)

Splitting arrays


函数原型作用
[split](ary, indices_or_sections[, axis])Split an array into multiple sub-arrays.
[array_split](ary, indices_or_sections[, axis])Split an array into multiple sub-arrays.
[dsplit](ary, indices_or_sections)Split array into multiple sub-arrays along the 3rd axis (depth).
[hsplit](ary, indices_or_sections)Split an array into multiple sub-arrays horizontally (column-wise).
[vsplit](ary, indices_or_sections)Split an array into multiple sub-arrays vertically (row-wise).

Tiling arrays


函数原型作用
[tile](A, reps)Construct an array by repeating A the number of times given by reps.
[repeat](a, repeats[, axis])Repeat elements of an array.

Adding and removing elements


函数原型作用
[delete](arr, obj[, axis])Return a new array with sub-arrays along an axis deleted.
[insert](arr, obj, values[, axis])Insert values along the given axis before the given indices.
[append](arr, values[, axis])Append values to the end of an array.
[resize](a, new_shape)Return a new array with the specified shape.
[trim_zeros](filt[, trim])Trim the leading and/or trailing zeros from a 1-D array or sequence.
[unique](ar[, return_index, return_inverse, …])Find the unique elements of an array.

Rearranging elements


函数原型作用
[flip](m[, axis])Reverse the order of elements in an array along the given axis.
fliplrFlip array in the left/right direction.
flipudFlip array in the up/down direction.
[reshape](a, newshape[, order])Gives a new shape to an array without changing its data.
[roll](a, shift[, axis])Roll array elements along a given axis.
[rot90](m[, k, axes])Rotate an array by 90 degrees in the plane specified by axes.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508645.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【TensorFlow】conv2d函数参数解释以及padding理解

卷积conv2d CNN在深度学习中有着举足轻重的地位&#xff0c;主要用于特征提取。在TensorFlow中涉及的函数是tf.nn.conv2d。 tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpuTrue, data_format“NHWC”, dilations[1, 1, 1, 1], nameNone) input 代表做卷积的…

卷积与傅立叶变换

一、卷积 1、一维的卷积 连续&#xff1a; 在泛函分析中&#xff0c;卷积是通过两个函数f(x)f(x)和g(x)g(x)生成第三个函数的一种算子&#xff0c;它代表的意义是&#xff1a;两个函数中的一个(我取g(x)g(x)&#xff0c;可以任意取)函数&#xff0c;把g(x)g(x)经过翻转平移,…

海明纠错码工作原理

海明纠错码 海明码&#xff08;Hamming Code&#xff09;是一个可以有多个校验位&#xff0c;具有检测并纠正一位错误代码的纠错码&#xff0c;所以它也仅用于信道特性比较好的环境中&#xff0c;如以太局域网中&#xff0c;因为如果信道特性不好的情况下&#xff0c;出现的错…

OpenCV-Python bindings是如何生成的(1)

翻译自How OpenCV-Python Bindings Works? 目标 学习 OpenCV-Python bindings是如何生成的如何为Python扩展新的opencv模块 OpenCV-Python bindings是如何生成的 在OpenCV里&#xff0c;所有算法都是用C实现的。但是这些算法可以在别的语言里使用&#xff0c;比如Python&…

OpenCV-Python bindings是如何生成的(2)

OpenCV-Python bindings生成流程 通过上篇文章和opencv python模块中的CMakeLists.txt文件&#xff0c;可以了解到opencv-python bindings生成的整个流程: 生成headers.txt文件 将每个模块的头文件添加到list中&#xff0c;通过一些关键词过滤掉一些不需要扩展的头文件&#x…

【TensorFlow】学习资源汇总以及知识总结

官方资源 官方网站 https://tensorflow.org 非翻墙神器不能访问也&#xff08;关键是我用了翻墙神器也没能访问&#xff09;伪官方网站 https://tensorflow.google.cn/ 墙内的人可以查阅的资料github https://github.com/tensorflow/tensorflow官方提供的models以及tutorial h…

机器学习资源锦集

http://www.cnblogs.com/pinard 十年码农&#xff0c;对数学统计学&#xff0c;数据挖掘&#xff0c;机器学习&#xff0c;大数据平台&#xff0c;大数据平台应用开发&#xff0c;大数据可视化感兴趣。github 深度学习 【深度学习】批归一化&#xff08;Batch Normalization&…

获取训练数据的方式

下载搜狗词库 https://pinyin.sogou.com/dict/ 在官网搜索相关的词库下载&#xff0c;比如地名等&#xff0c;然后使用脚本将此条转换成txt保存&#xff0c; 来源 # -*- coding: utf-8 -*- import os import sys import struct # 主要两部分 # 1.全局拼音表&#xff0c;貌似…

浅谈python MRO与Mixin模式

MRO(Method Resolution Order) In object-oriented programming languages with multiple inheritance, the diamond problem (sometimes referred to as the “deadly diamond of death”) is an ambiguity that arises when two classes B and C inherit from A, and class D…

CentOS7开发环境搭建(2)

关闭SELinux # 查看 $ getenforce Disabled $ sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: …

IntelliJ IDEA开发环境应用

安装 下载windows压缩包获取帮助: idea.medeming.com/jihuoma 常用设置 全局设置&#xff0c;对新建的工程生效 【File】【Other Settings】【Setings for New Projects…】 比如配置maven的路径以及配置文件的路径&#xff0c;基本设置一次即可&#xff0c;不需要每次新建工…

tcp状态机-三次握手-四次挥手以及常见面试题

TCP状态机介绍 在网络协议栈中&#xff0c;目前只有TCP提供了一种面向连接的可靠性数据传输。而可靠性&#xff0c;无非就是保证&#xff0c;我发给你的&#xff0c;你一定要收到。确保中间的通信过程中&#xff0c;不会丢失数据和乱序。在TCP保证可靠性数据传输的实现来看&am…

Visual studio Code的C/C++开发环境搭建

文章目录VS CodeC/C环境配置环境准备使用实例基于 VSCode 的远程开发平台环境准备参考VS Code Visual Studio Code&#xff08;简称VS Code&#xff09;是一个由微软开发&#xff0c;同时支持Windows 、 Linux和macOS等操作系统且开放源代码的代码编辑器&#xff0c;它支持测试…

Linux网络编程--文件描述符

文件描述符 在Unix和Unix-like操作系统中&#xff0c;文件描述符(file descriptor, FD)是一个文件或者像pipe或者network socket等之类的输入/输出源的唯一标识。 文件描述符通常是一个非负整数&#xff0c;负数通常代表无值或者错误。 文件描述符是POSIX API的一部分。每个除…

深信服 linux软件开发面试题整理

1、结构体可以进行比较 int memcmp ( const void * ptr1, const void * ptr2, size_t num ); Compare two blocks of memory Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match…

大端小端模式判断以及数据转换

简介 在计算机系统中&#xff0c;我们是以字节为单位的&#xff0c;每个地址单元都对应着一个字节&#xff0c;一个字节为 8bit。但是在C语言中除了8bit的char之外&#xff0c;还有16bit的short型&#xff0c;32bit的long型&#xff08;要看具体的编译器&#xff09;&#xff…

MSYS2下搭建Qt开发环境

最近随意浏览了一下俺们大省会城市的招聘信息&#xff0c;发现C招聘中涉及Qt经验的要求有不少&#xff0c;为了牛奶和面包&#xff0c;决心深入一下Qt开发。本篇文章由此而出。 Qt 关于Qt的人生经历在这不在累赘&#xff0c;资料随处可得&#xff0c;这里只记录干货。 环境搭…

CentOS7开发环境搭建(1)

文章目录BIOS开启VT支持U盘安装系统(2019-03-11)CentOS DNS配置CentOS网络配置配置静态IP克隆虚拟机网卡名称变更 CentOS6.5时间配置安装VMWare-tools用户管理 (2019-03-15 7.6.1810)给一般账号 root 权限Samba服务配置安装必备软件获取本机公网ipyum源和第三方库源管理配置本地…

ACM 欧拉公式

给出一个数X&#xff0c;求小于X的与X互质的数的个数&#xff0c;使用欧拉公式。 如果x1*x2*...*xnX,则个数nX*(1-1/x1)*(1-/x2)*... 使用这个的题目&#xff0c;超典型 相遇周期(HDOJ)

HDU 1495 非常可乐(BFS)

思路 最难在于想到这道题是BFS&#xff0c;想到之后只有六种情况就很好理解了。 代码 #include<stdio.h> #include<string.h> #include<math.h> #include<queue> using namespace std; int a,b,s; struct shui {int count;int ha,hb,hs; }t,t1; int m…