numpy 可用的常规函数

该部分位于numpy - ref - 1.14.5中的2.8 available ufuncs

1 数学运算

加法规则:

numpy.add(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘add‘>

x1 ,x2 - number,ndarray,Iterable 均可,实际上一般为ndarray。

out - 存储结果的位置,如果提供,其必须事具有输入广播的形状,如果无或者不提供,则返回新分配的数组;元组(可能仅作为关键字参数)的长度必须等于输出的数量。

余下参数略,不做说明,实际上记住 numpy.add(x1, x2) 即可,余下参数用的不多

示例一:一个ndarray + 数字

将数字与ndarray中的每一个元素相加

a = np.arange(1,5).reshape(2,2)
print(a)
# [[1 2]
#  [3 4]]
b = np.add(a,2)
print(b)
# [[3 4]
#  [5 6]]

示例二:两个同shape的ndarray相加

对应元素相加

a = np.arange(1,5).reshape(2,2)
print(a)
# [[1 2]
#  [3 4]]
b = np.arange(0,4).reshape(2,2)
print(b)
# [[0 1]
#  [2 3]]
c = np.add(a,b)
print(c)
# [[1 3]
#  [5 7]]

示例三:两个shape不全相等的ndarray相加

此时相当于 广播

a = np.arange(1,5).reshape(2,2)
print(a)
# [[1 2]
#  [3 4]]
b = np.arange(0,2)
print(b)
# [0 1]
c = np.add(a,b)
print(c)
# [[1 3]
#  [3 5]]

示例四:两个shape不全相等的ndarray相加

此时 与 广播 等价

a = np.arange(1,5).reshape(2,2)
print(a)
# [[1 2]
#  [3 4]]
b = np.arange(0,2).reshape(-1,1)
print(b)
# [[0]
#  [1]]
c = np.add(a,b)
print(c)
# [[1 2]
#  [4 5]]

示例五:两个shape不全相等的ndarray相加

此时相当于广播

a = np.arange(1,5).reshape(2,2)
print(a)
# [[1 2]
#  [3 4]]
b = np.arange(0,4).reshape(1,2,2)
print(b)
# [[[0 1]
#   [2 3]]]
c = np.add(a,b)
print(c)
# [[[1 3]
#   [5 7]]]

示例六:x1 x2的shape值不符合广播规则时,无法add

numpy 广播

a = np.arange(1,5).reshape(2,2)
print(a)
# [[1 2]
#  [3 4]]
b = np.arange(0,4)
print(b)
# [0 1 2 3]
c = np.add(a,b)
print(c)
# ValueError: operands could not be broadcast together with shapes (2,2) (4,) 
a = np.arange(1,7).reshape(2,3)
print(a)
# [[1 2 3]
#  [4 5 6]]
b = np.arange(0,4).reshape(2,2)
print(b)
# [[0 1]
#  [2 3]]
c = np.add(a,b)
print(c)
# ValueError: operands could not be broadcast together with shapes (2,3) (2,2) 

综上:当x1.shape != x2.shape 时,x1 x2 必须可广播到一个通用形状(可能是其中一个或另一个形状shape)they must be broadcastable to a common shape (which may be the shape of one or the other)。

add(x1, x2, /[, out, where, casting, order, . . . ])   按元素添加参数 Add arguments element-wise.
subtract(x1, x2, /[, out, where, casting, . . . ])     Subtract arguments, element-wise.
multiply(x1, x2, /[, out, where, casting, . . . ]) Multiply arguments element-wise.
divide(x1, x2, /[, out, where, casting, . . . ]) Returns a true division of the inputs, element-wise.
logaddexp(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs.
logaddexp2(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs in
base-2.
true_divide(x1, x2, /[, out, where, . . . ]) Returns a true division of the inputs, element-wise.
floor_divide(x1, x2, /[, out, where, . . . ]) Return the largest integer smaller or equal to the division
of the inputs.
negative(x, /[, out, where, casting, order, . . . ]) Numerical negative, element-wise.
positive(x, /[, out, where, casting, order, . . . ]) Numerical positive, element-wise.
power(x1, x2, /[, out, where, casting, . . . ]) First array elements raised to powers from second array,
element-wise.
remainder(x1, x2, /[, out, where, casting, . . . ]) Return element-wise remainder of division.
mod(x1, x2, /[, out, where, casting, order, . . . ]) Return element-wise remainder of division.
fmod(x1, x2, /[, out, where, casting, . . . ]) Return the element-wise remainder of division.
divmod(x1, x2[, out1, out2], / [[, out, . . . ]) Return element-wise quotient and remainder simultane
ously.
absolute(x, /[, out, where, casting, order, . . . ]) Calculate the absolute value element-wise.
fabs(x, /[, out, where, casting, order, . . . ]) Compute the absolute values element-wise.
rint(x, /[, out, where, casting, order, . . . ]) Round elements of the array to the nearest integer.
sign(x, /[, out, where, casting, order, . . . ]) Returns an element-wise indication of the sign of a number.
heaviside(x1, x2, /[, out, where, casting, . . . ]) Compute the Heaviside step function.
conj(x, /[, out, where, casting, order, . . . ]) Return the complex conjugate, element-wise.
exp(x, /[, out, where, casting, order, . . . ]) Calculate the exponential of all elements in the input array.
exp2(x, /[, out, where, casting, order, . . . ]) Calculate 2**p for all p in the input array.
log(x, /[, out, where, casting, order, . . . ]) Natural logarithm, element-wise.

add(x1, x2, /[, out, where, casting, order, . . . ]) Add arguments element-wise.
subtract(x1, x2, /[, out, where, casting, . . . ]) Subtract arguments, element-wise.
multiply(x1, x2, /[, out, where, casting, . . . ]) Multiply arguments element-wise.
divide(x1, x2, /[, out, where, casting, . . . ]) Returns a true division of the inputs, element-wise.
logaddexp(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs.
logaddexp2(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs in
base-2.
true_divide(x1, x2, /[, out, where, . . . ]) Returns a true division of the inputs, element-wise.
floor_divide(x1, x2, /[, out, where, . . . ]) Return the largest integer smaller or equal to the division
of the inputs.
negative(x, /[, out, where, casting, order, . . . ]) Numerical negative, element-wise.
positive(x, /[, out, where, casting, order, . . . ]) Numerical positive, element-wise.
power(x1, x2, /[, out, where, casting, . . . ]) First array elements raised to powers from second array,
element-wise.
remainder(x1, x2, /[, out, where, casting, . . . ]) Return element-wise remainder of division.
mod(x1, x2, /[, out, where, casting, order, . . . ]) Return element-wise remainder of division.
fmod(x1, x2, /[, out, where, casting, . . . ]) Return the element-wise remainder of division.
divmod(x1, x2[, out1, out2], / [[, out, . . . ]) Return element-wise quotient and remainder simultane
ously.
absolute(x, /[, out, where, casting, order, . . . ]) Calculate the absolute value element-wise.
fabs(x, /[, out, where, casting, order, . . . ]) Compute the absolute values element-wise.
rint(x, /[, out, where, casting, order, . . . ]) Round elements of the array to the nearest integer.
sign(x, /[, out, where, casting, order, . . . ]) Returns an element-wise indication of the sign of a number.
heaviside(x1, x2, /[, out, where, casting, . . . ]) Compute the Heaviside step function.
conj(x, /[, out, where, casting, order, . . . ]) Return the complex conjugate, element-wise.
exp(x, /[, out, where, casting, order, . . . ]) Calculate the exponential of all elements in the input array.
exp2(x, /[, out, where, casting, order, . . . ]) Calculate 2**p for all p in the input array.
log(x, /[, out, where, casting, order, . . . ]) Natural logarithm, element-wise.

原文地址:https://www.cnblogs.com/gengyi/p/9231834.html

时间: 2024-10-17 12:21:56

numpy 可用的常规函数的相关文章

数据分析与展示——NumPy数据存取与函数

NumPy库入门 NumPy数据存取和函数 数据的CSV文件存取 CSV文件 CSV(Comma-Separated Value,逗号分隔值)是一种常见的文件格式,用来存储批量数据. np.savetxt(frame,array,fmt='%.18e',delimiter=None) frame:文件.字符串或产生器,可以是.gz或.bz2的压缩文件. array:存入文件的数组. fmt:写入文件的格式,例如:%d %.2f %.18e. delimiter:分割字符串,默认是任何空格. 范例

Numpy中的tile函数用法

0.引言 在看机器学习实战这本书时,遇到numpy.tile(A,B)函数,开始没太明白这个函数用法,网上帖子也不太详细,经过一番试验后基本搞明白基本用法,分享给大家. 1.函数定义 tile函数是模板numpy.lib.shape_base中的函数. 函数形式:tile(A,rep) 功能:重复A的各个维度 参数类型: - A: Array类的都可以 - rep:A沿着各个维度重复的次数,从低维向高维重复 -A的类型众多,几乎所有类型都可以:array, list, tuple, dict,

Python数据分析与展示(1)-数据分析之表示(1)-NumPy数据存取与函数

NumPy数据存取与函数 数据的CSV文件存取 CSV文件 CSV(Comma-Separated Value,逗号分隔值) CSV是一种常见的文件格式,用来存储批量数据. 将数据写入CSV文件 np.savetxt(frame, array, fmt='%.18e', delimiter=None) -frame: 文件.字符串或产生器,可以是.gz或.bz2的压缩文件 -array: 存入文件的数组 -fmt: 写入文件的格式,例如:%d %.2f %.18e -delimiter:分割字符

python pandas numpy matplotlib 常用方法及函数

import numpy as np import pandas as pd import matplotlib.pyplot as plt ---------------numpy----------------------- arr = np.array([1,2,3], dtype=np.float64) np.zeros((3,6)) np.empty((2,3,2)) np.arange(15) arr.dtype arr.ndim arr.shape arr.astype(np.in

numpy下的flatten()函数用法

flatten是numpy.ndarray.flatten的一个函数,其官方文档是这样描述的: ndarray.flatten(order='C') Return a copy of the array collapsed into one dimension. Parameters:   order : {‘C’, ‘F’, ‘A’, ‘K’}, optional ‘C’ means to flatten in row-major (C-style) order. ‘F’ means to f

numpy.random之常用函数

在实际开发中,我们经常会使用随机函数,比如交叉验证,构造测试数据等.下面,是我常用的几个生成随机样本的函数: 1,rand(n1,n2,-,nn) 每一维度都是[0.0,1.0)半闭半开区间上的随机分布 2,randn(n1,n2,-,nn) 返回一个样本,具有标准正态分布 3,random([size]) sample([size]) Random_sample([size]) 返回随机的浮点数,在半开区间 [0.0, 1.0). 如果想了解更多的函数,可以看下下面这篇博客,写的比较全: py

数据分析与展示---Numpy数据存取与函数

简介 一:数据的CSV文件存取(一维或二维) (一)写入文件savetxt (二)读取文件loadtxt 二:多维数据的存取 (一)保存文件tofile (二)读取文件fromfile (三)NumPy 的便捷文件存取save/savez或load 三:NumPy的随机数函数(random模块) rand()均匀分布 randn()标准状态分布 randint()整数数组 seed()随机数种子 shuffle()根据数组第一轴产生一个新的乱序数组(在原数组基础) permutation()同上

numpy库补充 mean函数应用

mean()函数功能:求取均值经常操作的参数为axis,以m * n矩阵举例: axis 不设置值,对 m*n 个数求均值,返回一个实数 axis = 0:压缩行,对各列求均值,返回 1* n 矩阵 axis =1 :压缩列,对各行求均值,返回 m *1 矩阵 举例: >>> import numpy as np >>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])>>> now2 = np.ma

numpy数组符号化与函数向量化

计算净额成交量 Key_Function: np.sign(数组): 返回数组中每个元素的正负符号 np.piecewise(数组, 条件列表, 返回值列表): 通过条件列表中的元素, 判断数组中的元素在条件列表中的索引, 取返回值列表对应的索引, 当做返回值, 用这些返回值组成一个新的数组 Code: import numpy as np import matplotlib.pyplot as plt c, v = np.loadtxt('BHP.csv', delimiter=',', us