Numpy常用API

一、输入和输出

1.1 NumPy二进制文件(NPY,NPZ)

load(file[, mmap_mode, allow_pickle, ...]) 从.npy, .npz或pickle文件加载数组或pickle对象。

save(file, arr[, allow_pickle, fix_imports]) 将数组保存在NumPy.npy格式的二进制文件。

savez(file, args, *kwds) 将多个数组以未压缩的.npz格式保存到单个文件中。

savez_compreesed(file, args, *kwds)将多个数组以压缩的.npz格式保持了到单个文件中。

1.2 文本文件

loadtxt(fname[, dtype, comments, delimiter, …]) 从文本文件加载数据。
savetxt(fname, X[, fmt, delimiter, newline, …]) 从文本文件加载数据。
genfromtxt(fname[, dtype, comments, …]) 从文本文件加载数据,并按指定处理缺失值。
fromregex(file, regexp, dtype[, encoding]) 使用来自文本文件构造数组

1.3 正则表达式解析

fromstring(string[, dtype, count, sep]) 从字符串中的文本数据初始化的新1-D数组。
ndarray.tofile(fid[, sep, format]) 将数组作为文本或二进制写入文件(默认)。
ndarray.tolist() 将数组作为(可能是嵌套的)列表返回。

1.4 原始二进制文件

fromfile(file[, dtype, count, sep]) 根据文本或二进制文件中的数据构造数组。
ndarray.tofile(fid[, sep, format]) 将数组作为文本或二进制写入文件(默认)。

1.5 内存映射文件

memmap为存储在磁盘上的二进制文件中的数组创建内存映射。

1.6 Base-n相关

binary_repr(num[, width]) 将输入数字的二进制表示形式返回为字符串。
base_repr(number[, base, padding]) 返回给定基本系统中数字的字符串表示形式。

1.7 数据源

DataSource([destpath]) 通用数据源文件(文件,http,ftp,...)。

二、创建数组

2.1 ones和zeros等方法

empty(shape[, dtype, order]) 返回给定形状和类型的新数组,而不初始化条目。
empty_like(a[, dtype, order, subok]) 返回一个与给定数组具有相同形状和类型的新数组。
eye(N[, M, k, dtype, order]) 返回一个二维数组,其中对角线为1,零点为零。
identity(n[, dtype]) 返回标识数组。
ones(shape[, dtype, order]) 返回一个给定形状和类型的新数组,用一个填充。
ones_like(a[, dtype, order, subok]) 返回与给定数组具有相同形状和类型的数组。
zeros(shape[, dtype, order]) 返回给定形状和类型的新数组,用零填充。
zeros_like(a[, dtype, order, subok]) 返回与给定数组具有相同形状和类型的零数组。
full(shape, fill_value[, dtype, order]) 返回给定形状和类型的新数组,填充fill_value。
full_like(a, fill_value[, dtype, order, subok]) 返回与给定数组具有相同形状和类型的完整数组。

2.2 利用现有数据

array(object[, dtype, copy, order, subok, ndmin]) 创建一个新的数组。
asarray(a[, dtype, order]) 将输入的参数转换为数组。
asanyarray(a[, dtype, order]) 将输入转换为ndarray,但通过ndarray子类传递。
ascontiguousarray(a[, dtype]) 在内存中返回连续数组(C顺序)。
asmatrix(data[, dtype]) 将输入转换为矩阵。
copy(a[, order]) 返回给定对象的数组副本。
frombuffer(buffer[, dtype, count, offset]) 将缓冲区转换为一维数组。
fromfile(file[, dtype, count, sep]) 从文本或二进制文件中的数据构造数组。
fromfunction(function, shape, **kwargs) 通过在每个坐标上执行函数来构造数组。
fromiter(iterable, dtype[, count]) 从可迭代对象创建一个新的一维数组。
fromstring(string[, dtype, count, sep]) 从字符串中的文本数据初始化的新的一维数组.
loadtxt(fname[, dtype, comments, delimiter, …]) 从文本文件加载数据。
Creating record arrays (numpy.rec)
core.records.array(obj[, dtype, shape, …]) 从各种各样的对象构造一个记录数组。
core.records.fromarrays(arrayList[, dtype, …]) 从数组的(平面)列表创建记录数组
core.records.fromrecords(recList[, dtype, …]) 从文本形式的记录列表中创建一个重新数组
core.records.fromstring(datastring[, dtype, …]) 从字符串中包含的二进制数据创建(只读)记录数组。
core.records.fromfile(fd[, dtype, shape, …]) 从二进制文件数据创建数组

2.3 创建字符数组

core.defchararray.array(obj[, itemsize, …]) 创建一个字符数组。
core.defchararray.asarray(obj[, itemsize, …]) 将输入转换为字符数组,只在必要时复制数据。

2.4 数值范围

arange([start,] stop[, step,][, dtype]) 在给定的间隔内返回均匀间隔的值。
linspace(start, stop[, num, endpoint, …]) 在指定的间隔内返回均匀间隔的数字。
logspace(start, stop[, num, endpoint, base, …]) 返回数在对数刻度上均匀分布。
geomspace(start, stop[, num, endpoint, dtype]) 返回数在对数尺度上均匀分布(几何级数)。
meshgrid(xi, *kwargs) 从坐标向量返回坐标矩阵。
mgrid nd_grid 实例,它返回一个密集的多维“meshgrid”。
ogrid nd_grid 实例,它返回一个开放的多维“meshgrid”。

2.5 构建矩阵

diag(v[, k]) 提取对角线或构造对角线阵列。
diagflat(v[, k]) 使用展平输入创建二维数组作为对角线。
tri(N[, M, k, dtype]) 一个数组,其中包含给定对角线和低于给定对角线的数字,其他地方为零
tril(m[, k]) 数组的下三角形。
triu(m[, k]) 数组的上三角形。
vander(x[, N, increasing]) 生成Vandermonde矩阵。

2.6 矩阵类

mat(data[, dtype]) 将输入解释为矩阵。
bmat(obj[, ldict, gdict]) 从字符串、嵌套序列或数组生成矩阵对象。

原文地址:https://www.cnblogs.com/hugechuanqi/p/9839646.html