SciPy 1.12 中文文档(十八)
scipy.linalg.dft
原文:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.dft.html#scipy.linalg.dft
scipy.linalg.dft(n, scale=None)
离散傅里叶变换矩阵。
创建用于计算序列的离散傅里叶变换矩阵[1]。生成矩阵的第 n 个原根为 exp(-2pii/n),其中 i = sqrt(-1)。
参数:
nint
矩阵大小。
scalestr,可选
必须为 None、“sqrtn”或“n”。如果scale为“sqrtn”,则矩阵除以sqrt(n)。如果scale为“n”,则矩阵除以n。如果scale为 None(默认值),则矩阵不归一化,返回值仅为单位根的范德蒙德矩阵。
返回:
m(n, n) ndarray
DFT 矩阵。
注意事项
当scale为 None 时,用dft返回的矩阵乘以向量,在数学上等同于(但效率远低于)scipy.fft.fft执行的计算。
从版本 0.14.0 开始。
参考资料
[1]
“DFT 矩阵”,en.wikipedia.org/wiki/DFT_matrix
示例
>>> import numpy as np
>>> from scipy.linalg import dft
>>> np.set_printoptions(precision=2, suppress=True) # for compact output
>>> m = dft(5)
>>> m
array([[ 1\. +0.j , 1\. +0.j , 1\. +0.j , 1\. +0.j , 1\. +0.j ],
[ 1\. +0.j , 0.31-0.95j, -0.81-0.59j, -0.81+0.59j, 0.31+0.95j],
[ 1\. +0.j , -0.81-0.59j, 0.31+0.95j, 0.31-0.95j, -0.81+0.59j],
[ 1\. +0.j , -0.81+0.59j, 0.31-0.95j, 0.31+0.95j, -0.81-0.59j],
[ 1\. +0.j , 0.31+0.95j, -0.81+0.59j, -0.81-0.59j, 0.31-0.95j]])
>>> x = np.array([1, 2, 3, 0, 3])
>>> m @ x # Compute the DFT of x
array([ 9\. +0.j , 0.12-0.81j, -2.12+3.44j, -2.12-3.44j, 0.12+0.81j])
验证m @ x等同于fft(x)。
>>> from scipy.fft import fft
>>> fft(x) # Same result as m @ x
array([ 9\. +0.j , 0.12-0.81j, -2.12+3.44j, -2.12-3.44j, 0.12+0.81j])
scipy.linalg.fiedler
原文:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.fiedler.html#scipy.linalg.fiedler
scipy.linalg.fiedler(a)
返回对称的 Fiedler 矩阵
给定一组数字 a,Fiedler 矩阵具有结构 F[i, j] = np.abs(a[i] - a[j]),因此对角线为零且条目非负。Fiedler 矩阵具有主要的正特征值和其他负特征值。尽管不一般有效,对于特定输入,可以像 [1] 中给出的那样显式计算逆和行列式。
参数:
a(n,) array_like
系数数组
返回:
F(n, n) ndarray
请参见
circulant, toeplitz
注意事项
1.3.0 版本新增。
参考文献
[1]
J. Todd, “基础数值数学:第 2 卷:数值代数”, 1977 年, Birkhauser, DOI:10.1007/978-3-0348-7286-7
示例
>>> import numpy as np
>>> from scipy.linalg import det, inv, fiedler
>>> a = [1, 4, 12, 45, 77]
>>> n = len(a)
>>> A = fiedler(a)
>>> A
array([[ 0, 3, 11, 44, 76],
[ 3, 0, 8, 41, 73],
[11, 8, 0, 33, 65],
[44, 41, 33, 0, 32],
[76, 73, 65, 32, 0]])
明确的行列式和逆矩阵公式似乎仅适用于单调递增/递减的数组。注意三对角结构和角落。
>>> Ai = inv(A)
>>> Ai[np.abs(Ai) < 1e-12] = 0. # cleanup the numerical noise for display
>>> Ai
array([[-0.16008772, 0.16666667, 0\. , 0\. , 0.00657895],
[ 0.16666667, -0.22916667, 0.0625 , 0\. , 0\. ],
[ 0\. , 0.0625 , -0.07765152, 0.01515152, 0\. ],
[ 0\. , 0\. , 0.01515152, -0.03077652, 0.015625 ],
[ 0.00657895, 0\. , 0\. , 0.015625 , -0.00904605]])
>>> det(A)
15409151.999999998
>>> (-1)**(n-1) * 2**(n-2) * np.diff(a).prod() * (a[-1] - a[0])
15409152
scipy.linalg.fiedler_companion
scipy.linalg.fiedler_companion(a)
返回一个菲德勒伴随矩阵
给定多项式系数数组a,此函数形成一个具有特殊结构的五对角矩阵,其特征值与a的根一致。
参数:
a(N,) array_like
降序排列的多项式系数的一维数组,其中首个系数非零。若N < 2,则返回一个空数组。
返回:
c(N-1, N-1) ndarray
生成的伴随矩阵
参见
companion
注意事项
类似于companion,主导系数应为非零值。若主导系数不为 1,则在生成数组之前会重新缩放其他系数。为避免数值问题,最好提供一个首一多项式。
新版本 1.3.0 中新增。
参考文献
[1]
M. 菲德勒,“关于伴随矩阵的一个注记”,《线性代数及其应用》,2003 年,DOI:10.1016/S0024-3795(03)00548-2
示例
>>> import numpy as np
>>> from scipy.linalg import fiedler_companion, eigvals
>>> p = np.poly(np.arange(1, 9, 2)) # [1., -16., 86., -176., 105.]
>>> fc = fiedler_companion(p)
>>> fc
array([[ 16., -86., 1., 0.],
[ 1., 0., 0., 0.],
[ 0., 176., 0., -105.],
[ 0., 1., 0., 0.]])
>>> eigvals(fc)
array([7.+0.j, 5.+0.j, 3.+0.j, 1.+0.j])
scipy.linalg.hadamard
原文:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.hadamard.html#scipy.linalg.hadamard
scipy.linalg.hadamard(n, dtype=<class 'int'>)
构造一个哈达玛矩阵。
使用 Sylvester 的构造方法构造一个 n×n 的哈达玛矩阵。n 必须是 2 的幂。
参数:
nint
矩阵的阶数。n 必须是 2 的幂。
dtypedtype,可选
要构造的数组的数据类型。
返回值:
H(n, n) ndarray
哈达玛矩阵。
注意
自版本 0.8.0 新增。
示例
>>> from scipy.linalg import hadamard
>>> hadamard(2, dtype=complex)
array([[ 1.+0.j, 1.+0.j],
[ 1.+0.j, -1.-0.j]])
>>> hadamard(4)
array([[ 1, 1, 1, 1],
[ 1, -1, 1, -1],
[ 1, 1, -1, -1],
[ 1, -1, -1, 1]])
scipy.linalg.hankel
原文链接:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.hankel.html#scipy.linalg.hankel
scipy.linalg.hankel(c, r=None)
构建一个 Hankel 矩阵。
Hankel 矩阵具有恒定的反对角线,以 c 作为其第一列,r 作为其最后一行。如果未提供 r,则假定 r = zeros_like(c)。
参数:
carray_like
矩阵的第一列。无论 c 的实际形状如何,都会转换为一维数组。
rarray_like,可选
矩阵的最后一行。如果为 None,则假定 r = zeros_like(c)。忽略 r[0];返回矩阵的最后一行为 [c[-1], r[1:]]。无论 r 的实际形状如何,都会转换为一维数组。
返回:
A(len(c), len(r)) 数组
Hankel 矩阵。其数据类型与 (c[0] + r[0]).dtype 相同。
参见
Toeplitz 矩阵
循环矩阵
示例
>>> from scipy.linalg import hankel
>>> hankel([1, 17, 99])
array([[ 1, 17, 99],
[17, 99, 0],
[99, 0, 0]])
>>> hankel([1,2,3,4], [4,7,7,8,9])
array([[1, 2, 3, 4, 7],
[2, 3, 4, 7, 7],
[3, 4, 7, 7, 8],
[4, 7, 7, 8, 9]])
scipy.linalg.helmert
原始文档:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.helmert.html#scipy.linalg.helmert
scipy.linalg.helmert(n, full=False)
创建一个阶数为n的 Helmert 矩阵。
在统计学、组合分析或 Aitchison 几何学中有应用。
参数:
nint
要创建的数组大小。
fullbool,可选
如果为 True,则返回形状为(n, n)的 ndarray。否则返回不包括第一行的子矩阵。默认值为 False。
返回:
Mndarray
Helmert 矩阵。形状为(n, n)或(n-1, n),具体取决于full参数。
示例
>>> from scipy.linalg import helmert
>>> helmert(5, full=True)
array([[ 0.4472136 , 0.4472136 , 0.4472136 , 0.4472136 , 0.4472136 ],
[ 0.70710678, -0.70710678, 0\. , 0\. , 0\. ],
[ 0.40824829, 0.40824829, -0.81649658, 0\. , 0\. ],
[ 0.28867513, 0.28867513, 0.28867513, -0.8660254 , 0\. ],
[ 0.2236068 , 0.2236068 , 0.2236068 , 0.2236068 , -0.89442719]])
scipy.linalg.hilbert
原文链接:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.hilbert.html#scipy.linalg.hilbert
scipy.linalg.hilbert(n)
创建阶数为 n 的希尔伯特矩阵。
返回 n 行 n 列的数组,其中 h[i,j] = 1 / (i + j + 1)。
参数:
nint
创建数组的大小。
返回:
h(n, n) ndarray
希尔伯特矩阵。
另请参阅
计算希尔伯特矩阵的逆。
注意事项
新版本 0.10.0 中新增。
示例
>>> from scipy.linalg import hilbert
>>> hilbert(3)
array([[ 1\. , 0.5 , 0.33333333],
[ 0.5 , 0.33333333, 0.25 ],
[ 0.33333333, 0.25 , 0.2 ]])
scipy.linalg.invhilbert
scipy.linalg.invhilbert(n, exact=False)
计算阶数为 n 的 Hilbert 矩阵的逆。
Hilbert 矩阵的逆中的条目是整数。当 n 大于 14 时,逆中的某些条目超过了 64 位整数的上限。exact 参数提供两种处理这些大整数的选项。
参数:
nint
Hilbert 矩阵的阶数。
exactbool,可选
如果为 False,则返回的数组数据类型为 np.float64,并且数组是逆的近似。如果为 True,则返回的数组是精确的整数逆数组。在 n > 14 时表示精确逆的对象数组是长整数。对于 n <= 14,精确的逆作为数据类型为 np.int64 的数组返回。
返回:
invh(n, n) ndarray
如果 exact 为 False,则数组的数据类型为 np.float64。如果 exact 为 True,则数据类型为 np.int64(当 n <= 14 时)或 object(当 n > 14 时)。在后一种情况下,数组中的对象将是长整数。
另请参阅
hilbert
创建 Hilbert 矩阵。
注意事项
自 0.10.0 版本起新增。
示例
>>> from scipy.linalg import invhilbert
>>> invhilbert(4)
array([[ 16., -120., 240., -140.],
[ -120., 1200., -2700., 1680.],
[ 240., -2700., 6480., -4200.],
[ -140., 1680., -4200., 2800.]])
>>> invhilbert(4, exact=True)
array([[ 16, -120, 240, -140],
[ -120, 1200, -2700, 1680],
[ 240, -2700, 6480, -4200],
[ -140, 1680, -4200, 2800]], dtype=int64)
>>> invhilbert(16)[7,7]
4.2475099528537506e+19
>>> invhilbert(16, exact=True)[7,7]
42475099528537378560
scipy.linalg.leslie
原文链接:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.leslie.html#scipy.linalg.leslie
scipy.linalg.leslie(f, s)
创建一个 Leslie 矩阵。
给定长度为 n 的繁殖系数数组 f 和长度为 n-1 的存活系数数组 s,返回相关的 Leslie 矩阵。
参数:
f(N,) 数组型
“繁殖”系数。
s(N-1,) 数组型
“存活”系数必须是 1 维的。s 的长度必须比 f 的长度少 1,且至少为 1。
返回:
L(N, N) 二维数组
该数组除了第一行为 f,第一个次对角线为 s 外,其他均为零。数组的数据类型将与 f[0]+s[0] 的数据类型相同。
注意
从版本 0.8.0 开始新加入。
Leslie 矩阵用于建模离散时间的年龄结构人口增长 [1] [2]。在具有 n 个年龄类别的人口中,两组参数定义了 Leslie 矩阵:n 个“繁殖系数”,指每个年龄类别每人口产生的后代数,以及 n - 1 个“存活系数”,指每个年龄类别的每人口存活率。
参考资料
[1]
P. H. Leslie, 关于在某些人口数学中使用矩阵的论文, 生物统计学, Vol. 33, No. 3, 183–212 (Nov. 1945)
[2]
P. H. Leslie, 有关在人口数学中使用矩阵的进一步说明, 生物统计学, Vol. 35, No. 3/4, 213–245 (Dec. 1948)
示例
>>> from scipy.linalg import leslie
>>> leslie([0.1, 2.0, 1.0, 0.1], [0.2, 0.8, 0.7])
array([[ 0.1, 2\. , 1\. , 0.1],
[ 0.2, 0\. , 0\. , 0\. ],
[ 0\. , 0.8, 0\. , 0\. ],
[ 0\. , 0\. , 0.7, 0\. ]])
scipy.linalg.pascal
原文:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.pascal.html#scipy.linalg.pascal
scipy.linalg.pascal(n, kind='symmetric', exact=True)
返回 n x n Pascal 矩阵。
Pascal 矩阵是一个包含其元素为二项式系数的矩阵。
Parameters:
nint
The size of the matrix to create; that is, the result is an n x n matrix.
kindstr, optional
Must be one of ‘symmetric’, ‘lower’, or ‘upper’. Default is ‘symmetric’.
exactbool, optional
If exact is True, the result is either an array of type numpy.uint64 (if n < 35) or an object array of Python long integers. If exact is False, the coefficients in the matrix are computed using scipy.special.comb with exact=False. The result will be a floating point array, and the values in the array will not be the exact coefficients, but this version is much faster than exact=True.
Returns:
p(n, n) ndarray
Pascal 矩阵。
See also
invpascal
Notes
See en.wikipedia.org/wiki/Pascal_matrix for more information about Pascal matrices.
New in version 0.11.0.
Examples
>>> from scipy.linalg import pascal
>>> pascal(4)
array([[ 1, 1, 1, 1],
[ 1, 2, 3, 4],
[ 1, 3, 6, 10],
[ 1, 4, 10, 20]], dtype=uint64)
>>> pascal(4, kind='lower')
array([[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 2, 1, 0],
[1, 3, 3, 1]], dtype=uint64)
>>> pascal(50)[-1, -1]
25477612258980856902730428600
>>> from scipy.special import comb
>>> comb(98, 49, exact=True)
25477612258980856902730428600
scipy.linalg.invpascal
scipy.linalg.invpascal(n, kind='symmetric', exact=True)
返回 n x n 帕斯卡矩阵的逆。
帕斯卡矩阵是一个以二项式系数作为其元素的矩阵。
参数:
nint
要创建的矩阵的大小;即结果是一个 n x n 的矩阵。
kindstr, 可选
必须是 'symmetric'、'lower' 或 'upper' 中的一个。默认为 'symmetric'。
exactbool, 可选
如果 exact 为 True,则结果为 numpy.int64 类型的数组(若 n <= 35),或者 Python 整数的对象数组。如果 exact 为 False,则矩阵中的系数通过 scipy.special.comb 计算,其中 exact=False。结果将是一个浮点数数组,并且对于大的 n,数组中的值不会是精确的系数。
返回:
invp(n, n) ndarray
帕斯卡矩阵的逆。
另见
注释
新增于版本 0.16.0。
参考
[1]
“帕斯卡矩阵”, en.wikipedia.org/wiki/Pascal_matrix
[2]
Cohen, A. M., “帕斯卡矩阵的逆”, 数学杂志, 59(408), pp. 111-112, 1975.
示例
>>> from scipy.linalg import invpascal, pascal
>>> invp = invpascal(5)
>>> invp
array([[ 5, -10, 10, -5, 1],
[-10, 30, -35, 19, -4],
[ 10, -35, 46, -27, 6],
[ -5, 19, -27, 17, -4],
[ 1, -4, 6, -4, 1]])
>>> p = pascal(5)
>>> p.dot(invp)
array([[ 1., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 1.]])
kind 和 exact 的使用示例:
>>> invpascal(5, kind='lower', exact=False)
array([[ 1., -0., 0., -0., 0.],
[-1., 1., -0., 0., -0.],
[ 1., -2., 1., -0., 0.],
[-1., 3., -3., 1., -0.],
[ 1., -4., 6., -4., 1.]])
scipy.linalg.toeplitz
原文链接:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.toeplitz.html#scipy.linalg.toeplitz
scipy.linalg.toeplitz(c, r=None)
构造一个 Toeplitz 矩阵。
Toeplitz 矩阵具有恒定的对角线,其第一列是c,第一行是r。如果没有给出r,则假定r == conjugate(c)。
参数:
carray_like
矩阵的第一列。无论c的实际形状如何,它都将被转换为一个一维数组。
rarray_like,可选
矩阵的第一行。如果为 None,则假定r = conjugate(c);在这种情况下,如果c[0]是实数,则结果是一个 Hermitian 矩阵。忽略r[0];返回矩阵的第一行为*[c[0], r[1:]]。无论r*的实际形状如何,它都将被转换为一个一维数组。
返回:
A(len(c), len(r)) ndarray
Toeplitz 矩阵。dtype 与*(c[0] + r[0])*的类型相同。
另请参见
circulant
循环矩阵
hankel
Hankel 矩阵
solve_toeplitz
解决一个 Toeplitz 系统。
注意
当c或r为标量,或者c为复数且r为 None 时,在版本 0.8.0 中的行为发生了变化。以前版本中的行为没有记录,并且不再支持。
示例
>>> from scipy.linalg import toeplitz
>>> toeplitz([1,2,3], [1,4,5,6])
array([[1, 4, 5, 6],
[2, 1, 4, 5],
[3, 2, 1, 4]])
>>> toeplitz([1.0, 2+3j, 4-1j])
array([[ 1.+0.j, 2.-3.j, 4.+1.j],
[ 2.+3.j, 1.+0.j, 2.-3.j],
[ 4.-1.j, 2.+3.j, 1.+0.j]])
scipy.linalg.tri
原文:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.linalg.tri.html#scipy.linalg.tri
scipy.linalg.tri(N, M=None, k=0, dtype=None)
从版本 1.11.0 开始废弃:tri 被 numpy.tri 取代,将在 SciPy 1.13.0 中移除。
构造 (N, M) 矩阵,其主对角线及其以下到第 k 条对角线的元素为 1。
矩阵满足 A[i,j] == 1 当 j <= i + k。
参数:
Nint
矩阵第一维的大小。
Mint 或 None,可选
矩阵的第二维大小。如果 M 是 None,则假设 M = N。
kint,可选
矩阵填充 1 的次对角线的数量。k = 0 是主对角线,k < 0 是次对角线,k > 0 是超对角线。
dtypedtype,可选
矩阵的数据类型。
返回:
tri(N, M) 数组
三角矩阵。
示例
>>> from scipy.linalg import tri
>>> tri(3, 5, 2, dtype=int)
array([[1, 1, 1, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1]])
>>> tri(3, 5, -1, dtype=int)
array([[0, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 1, 0, 0, 0]])
scipy.linalg.get_blas_funcs
scipy.linalg.get_blas_funcs(names, arrays=(), dtype=None, ilp64=False)
返回名称对应的可用 BLAS 函数对象。
使用数组确定 BLAS 例程的最佳前缀。
参数:
namesstr 或 str 序列
BLAS 函数的名称,不包括类型前缀。
arraysndarray 序列,可选
可以提供数组以确定 BLAS 例程的最佳前缀。如果未提供,则使用双精度例程,否则使用数组中最通用的类型。
dtypestr 或 dtype,可选
数据类型说明符。如果arrays非空,则不使用。
ilp64{True, False, ‘preferred’},可选
是否返回 ILP64 例程变体。选择'preferred'将返回 ILP64 例程(如果可用),否则返回 32 位例程。默认为 False。
返回:
funcs列表
包含找到的函数的列表。
注意事项
这个例程会自动选择 Fortran/C 接口之间的优先顺序。尽可能使用 Fortran 代码处理列主序数组。在其他情况下,则优先选择 C 代码。
在 BLAS 中,命名约定是所有函数都以类型前缀开头,这取决于主矩阵的类型。这些可以是 NumPy 类型{float32, float64, complex64, complex128}对应的{'s', 'd', 'c', 'z'}。代码和 dtype 存储在返回函数的typecode和dtype属性中。
示例
>>> import numpy as np
>>> import scipy.linalg as LA
>>> rng = np.random.default_rng()
>>> a = rng.random((3,2))
>>> x_gemv = LA.get_blas_funcs('gemv', (a,))
>>> x_gemv.typecode
'd'
>>> x_gemv = LA.get_blas_funcs('gemv',(a*1j,))
>>> x_gemv.typecode
'z'
scipy.linalg.get_lapack_funcs
scipy.linalg.get_lapack_funcs(names, arrays=(), dtype=None, ilp64=False)
返回名称中可用的 LAPACK 函数对象。
数组用于确定 LAPACK 程序包的最佳前缀。
参数:
namesstr 或字符串序列
没有类型前缀的 LAPACK 函数的名称。
arraysndarrays 序列,可选
可以提供数组以确定 LAPACK 程序包的最佳前缀。如果未提供,则将使用双精度例程,否则将使用数组中最通用的类型。
dtypestr 或 dtype,可选
数据类型说明符。如果 arrays 非空,则不使用。
ilp64{True, False, ‘preferred’},可选
是否返回 ILP64 例程变体。选择 ‘preferred’ 如果可用,则返回 ILP64 例程,否则返回 32 位例程。默认为 False
返回:
funcs列表
包含找到的函数的列表。
注意事项
该例程会自动在 Fortran/C 接口之间进行选择。在列主序数组中尽可能使用 Fortran 代码。在所有其他情况下,优选 C 代码。
在 LAPACK 中,命名约定是所有函数以一个类型前缀开头,这取决于主矩阵的类型。这些可以是 NumPy 类型 {float32, float64, complex64, complex128} 中的 {‘s’, ‘d’, ‘c’, ‘z’} 之一,分别存储在返回函数的 typecode 属性中。
示例
假设我们想使用 ‘?lange’ 例程来计算数组的选定范数。我们传递我们的数组以获得正确的 ‘lange’ 版本。
>>> import numpy as np
>>> import scipy.linalg as LA
>>> rng = np.random.default_rng()
>>> a = rng.random((3,2))
>>> x_lange = LA.get_lapack_funcs('lange', (a,))
>>> x_lange.typecode
'd'
>>> x_lange = LA.get_lapack_funcs('lange',(a*1j,))
>>> x_lange.typecode
'z'
几个 LAPACK 例程在其内部 WORK 数组具有最佳大小时效果最佳(足够大以进行快速计算,但又小到不浪费内存)。这个大小也通过专用查询函数确定,通常作为独立函数包装并通常表示为 ###_lwork。下面是 ?sysv 的示例。
>>> a = rng.random((1000, 1000))
>>> b = rng.random((1000, 1)) * 1j
>>> # We pick up zsysv and zsysv_lwork due to b array
... xsysv, xlwork = LA.get_lapack_funcs(('sysv', 'sysv_lwork'), (a, b))
>>> opt_lwork, _ = xlwork(a.shape[0]) # returns a complex for 'z' prefix
>>> udut, ipiv, x, info = xsysv(a, b, lwork=int(opt_lwork.real))
scipy.linalg.find_best_blas_type
scipy.linalg.find_best_blas_type(arrays=(), dtype=None)
寻找最佳匹配的 BLAS/LAPACK 类型。
数组用于确定 BLAS 例程的最佳前缀。
参数:
arraysndarrays 的序列,可选
可以提供数组来确定 BLAS 例程的最佳前缀。如果未提供,则使用双精度例程,否则使用数组中最通用的类型。
dtypestr 或 dtype,可选
数据类型指定符。如果arrays非空则不使用。
返回值:
prefixstr
BLAS/LAPACK 前缀字符。
dtypedtype
推断的 Numpy 数据类型。
prefer_fortranbool
是否优先使用 Fortran 顺序的例程而不是 C 顺序。
示例
>>> import numpy as np
>>> import scipy.linalg.blas as bla
>>> rng = np.random.default_rng()
>>> a = rng.random((10,15))
>>> b = np.asfortranarray(a) # Change the memory layout order
>>> bla.find_best_blas_type((a,))
('d', dtype('float64'), False)
>>> bla.find_best_blas_type((a*1j,))
('z', dtype('complex128'), False)
>>> bla.find_best_blas_type((b,))
('d', dtype('float64'), True)
杂项工具(scipy.misc)
自 1.10.0 版本起已弃用:此模块已弃用,并将在 SciPy v2.0.0 中完全移除。
各种无法归类的实用程序。
ascent() | 获取一张 8 位灰度深度、512 x 512 的衍生图像,便于演示使用。 |
|---|---|
central_diff_weights(Np[, ndiv]) | 返回一个 Np 点中心导数的权重。 |
derivative(func, x0[, dx, n, args, order]) | 在某一点找到函数的第 n 阶导数。 |
face([gray]) | 获取一张 1024 x 768 的彩色浣熊脸部图像。 |
electrocardiogram() | 加载一个心电图作为 1-D 信号的示例。 |
scipy.misc.ascent
原文链接:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.misc.ascent.html#scipy.misc.ascent
scipy.misc.ascent()
获取一个便于演示的 8 位灰度深度,512 x 512 像素的图像
该图像来源于www.public-domain-image.com/people-public-domain-images-pictures/
自 1.10.0 版本起已弃用:ascent在 SciPy 1.10.0 中已从scipy.misc.ascent中弃用,并将在 SciPy 1.12.0 中完全移除。数据集方法已移至scipy.datasets模块。请改用scipy.datasets.ascent。
参数:
None
返回:
ascentndarray
用于测试和演示的便利图像
示例
>>> import scipy.misc
>>> ascent = scipy.misc.ascent()
>>> ascent.shape
(512, 512)
>>> ascent.max()
255
>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(ascent)
>>> plt.show()
scipy.misc.central_diff_weights
scipy.misc.central_diff_weights(Np, ndiv=1)
返回 Np 点中心导数的权重。
假设等间距函数点。
如果权重在向量 w 中,则导数为 w[0] * f(x-h0dx) + … + w[-1] * f(x+h0dx)
自 1.10.0 版本起弃用:central_diff_weights 已从 SciPy 1.10.0 中的scipy.misc.central_diff_weights中弃用,并将在 SciPy 1.12.0 中完全移除。您可以考虑使用 findiff:github.com/maroba/findiff或 numdifftools:github.com/pbrod/numdifftools
参数:
Np:int
中心导数的点数。
ndiv:int,可选
分割数。默认为 1。
返回:
w:ndarray
Np 点中心导数的权重。其大小为Np。
注意事项
对大量点可能不精确。
引用
[1]
en.wikipedia.org/wiki/Finite_difference
示例
我们可以计算函数的导数值。
>>> from scipy.misc import central_diff_weights
>>> def f(x):
... return 2 * x**2 + 3
>>> x = 3.0 # derivative point
>>> h = 0.1 # differential step
>>> Np = 3 # point number for central derivative
>>> weights = central_diff_weights(Np) # weights for first derivative
>>> vals = [f(x + (i - Np/2) * h) for i in range(Np)]
>>> sum(w * v for (w, v) in zip(weights, vals))/h
11.79999999999998
该值接近解析解:f’(x) = 4x,所以 f’(3) = 12
scipy.misc.derivative
原文链接:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.misc.derivative.html#scipy.misc.derivative
scipy.misc.derivative(func, x0, dx=1.0, n=1, args=(), order=3)
在点处找到函数的第 n 阶导数。
给定一个函数,在点x0处使用间距dx的中心差分公式计算第 n 阶导数。
自 1.10.0 版本起弃用:derivative 自 SciPy 1.10.0 中的scipy.misc.derivative已被弃用,将在 SciPy 1.12.0 中完全移除。您可以考虑使用 findiff:github.com/maroba/findiff 或 numdifftools:github.com/pbrod/numdifftools
参数:
func函数
输入函数。
x0浮点数
找到第 n 阶导数的点。
dx浮点数,可选
Spacing.
n整数,可选
导数的阶数。默认为 1。
args元组,可选
参数
order整数,可选
要使用的点数,必须为奇数。
注意
减小步长过小可能导致舍入误差。
示例
>>> from scipy.misc import derivative
>>> def f(x):
... return x**3 + x**2
>>> derivative(f, 1.0, dx=1e-6)
4.9999999999217337
scipy.misc.face
原文:
docs.scipy.org/doc/scipy-1.12.0/reference/generated/scipy.misc.face.html#scipy.misc.face
scipy.misc.face(gray=False)
获得一张大小为 1024 x 768 的浣熊脸的彩色图像。
在 www.public-domain-image.com 上的 raccoon-procyon-lotor.jpg
自版本 1.10.0 起弃用:face 已从 SciPy 1.10.0 的 scipy.misc.face 中弃用,并将在 SciPy 1.12.0 中完全移除。数据集方法已迁移到 scipy.datasets 模块。请使用 scipy.datasets.face 替代。
参数:
graybool,可选
如果为 True,则返回 8 位灰度图像,否则返回彩色图像
返回:
facendarray
浣熊脸的图像
示例
>>> import scipy.misc
>>> face = scipy.misc.face()
>>> face.shape
(768, 1024, 3)
>>> face.max()
255
>>> face.dtype
dtype('uint8')
>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(face)
>>> plt.show()
scipy.misc.electrocardiogram
scipy.misc.electrocardiogram()
作为一维信号的示例加载心电图。
返回的信号是一个 5 分钟长的心电图(ECG),记录了心脏电活动,以 360 Hz 采样。
自 1.10.0 版本起不推荐使用:electrocardiogram从 SciPy 1.10.0 版起已经从scipy.misc.electrocardiogram中删除,并且将在 SciPy 1.12.0 版中完全移除。数据集方法已经迁移到scipy.datasets模块。请使用scipy.datasets.electrocardiogram。
返回:
ecg ndarray
心电图以毫伏(mV)为单位,在 360 Hz 采样。
注释
所提供的信号是来自录音 208(MLII 导联)的摘录(19:35 至 24:35),该信号由 MIT-BIH 心律失常数据库在 PhysioNet 上提供[1]。摘录包括噪声诱导的伪迹、典型的心跳以及病理性变化。
新功能在 1.1.0 版本中添加。
参考文献
[1]
Moody GB, Mark RG. MIT-BIH 心律失常数据库的影响。IEEE 工程与医学生物学杂志,20(3):45-50(2001 年 5-6 月)。(PMID: 11446209);DOI:10.13026/C2F305
[2]
Goldberger AL, Amaral LAN, Glass L, Hausdorff JM, Ivanov PCh, Mark RG, Mietus JE, Moody GB, Peng C-K, Stanley HE. PhysioBank,PhysioToolkit 和 PhysioNet:复杂生理信号的新研究资源的组成部分。循环 101(23):e215-e220;DOI:10.1161/01.CIR.101.23.e215
示例
>>> from scipy.misc import electrocardiogram
>>> ecg = electrocardiogram()
>>> ecg
array([-0.245, -0.215, -0.185, ..., -0.405, -0.395, -0.385])
>>> ecg.shape, ecg.mean(), ecg.std()
((108000,), -0.16510875, 0.5992473991177294)
如所述,该信号显示了几个具有不同形态的区域。例如,最初几秒显示了正常窦性节律心脏的电活动,如下图所示。
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> fs = 360
>>> time = np.arange(ecg.size) / fs
>>> plt.plot(time, ecg)
>>> plt.xlabel("time in s")
>>> plt.ylabel("ECG in mV")
>>> plt.xlim(9, 10.2)
>>> plt.ylim(-1, 1.5)
>>> plt.show()
然而,在第 16 秒之后,出现了第一次早期室性收缩,也称为早搏。这些与典型的心跳在形态上有所不同。这种差异可以在下图中清楚地观察到。
>>> plt.plot(time, ecg)
>>> plt.xlabel("time in s")
>>> plt.ylabel("ECG in mV")
>>> plt.xlim(46.5, 50)
>>> plt.ylim(-2, 1.5)
>>> plt.show()
在几个点上大的伪迹干扰了记录,例如:
>>> plt.plot(time, ecg)
>>> plt.xlabel("time in s")
>>> plt.ylabel("ECG in mV")
>>> plt.xlim(207, 215)
>>> plt.ylim(-2, 3.5)
>>> plt.show()
最后,检查功率谱显示,大部分生物信号由较低频率组成。在 60 Hz 时,可以清楚地观察到由电网引起的噪音。
>>> from scipy.signal import welch
>>> f, Pxx = welch(ecg, fs=fs, nperseg=2048, scaling="spectrum")
>>> plt.semilogy(f, Pxx)
>>> plt.xlabel("Frequency in Hz")
>>> plt.ylabel("Power spectrum of the ECG in mV**2")
>>> plt.xlim(f[[0, -1]])
>>> plt.show()
多维图像处理(scipy.ndimage)
此包含各种多维图像处理函数。
滤波器
convolve(input, weights[, output, mode, ...]) | 多维卷积运算。 |
|---|---|
convolve1d(input, weights[, axis, output, ...]) | 沿指定轴计算一维卷积。 |
correlate(input, weights[, output, mode, ...]) | 多维相关运算。 |
correlate1d(input, weights[, axis, output, ...]) | 沿指定轴计算一维相关运算。 |
gaussian_filter(input, sigma[, order, ...]) | 多维高斯滤波器。 |
gaussian_filter1d(input, sigma[, axis, ...]) | 一维高斯滤波器。 |
gaussian_gradient_magnitude(input, sigma[, ...]) | 使用高斯导数计算多维梯度幅值。 |
gaussian_laplace(input, sigma[, output, ...]) | 使用高斯二阶导数的多维拉普拉斯滤波器。 |
generic_filter(input, function[, size, ...]) | 使用给定函数计算多维滤波器。 |
generic_filter1d(input, function, filter_size) | 沿指定轴计算一维滤波器。 |
generic_gradient_magnitude(input, derivative) | 使用提供的梯度函数计算梯度幅值。 |
generic_laplace(input, derivative2[, ...]) | 使用提供的二阶导数函数进行 N 维拉普拉斯滤波器。 |
laplace(input[, output, mode, cval]) | 基于近似二阶导数的 N 维拉普拉斯滤波器。 |
maximum_filter(input[, size, footprint, ...]) | 计算多维最大值滤波器。 |
maximum_filter1d(input, size[, axis, ...]) | 沿指定轴计算 1-D 最大值滤波器。 |
median_filter(input[, size, footprint, ...]) | 计算多维中值滤波器。 |
minimum_filter(input[, size, footprint, ...]) | 计算多维最小值滤波器。 |
minimum_filter1d(input, size[, axis, ...]) | 沿指定轴计算 1-D 最小值滤波器。 |
percentile_filter(input, percentile[, size, ...]) | 计算多维百分位数滤波器。 |
prewitt(input[, axis, output, mode, cval]) | 计算普雷维特滤波器。 |
rank_filter(input, rank[, size, footprint, ...]) | 计算多维秩滤波器。 |
sobel(input[, axis, output, mode, cval]) | 计算 Sobel 滤波器。 |
uniform_filter(input[, size, output, mode, ...]) | 多维均匀过滤器。 |
uniform_filter1d(input, size[, axis, ...]) | 沿指定轴计算 1-D 均匀滤波器。 |
Fourier filters
fourier_ellipsoid(input, size[, n, axis, output]) | 多维椭球傅里叶滤波器。 |
|---|---|
fourier_gaussian(input, sigma[, n, axis, output]) | 多维高斯傅里叶滤波器。 |
fourier_shift(input, shift[, n, axis, output]) | 多维傅立叶移位滤波器。 |
fourier_uniform(input, size[, n, axis, output]) | 多维均匀傅立叶滤波器。 |
插值
affine_transform(input, matrix[, offset, ...]) | 应用仿射变换。 |
|---|---|
geometric_transform(input, mapping[, ...]) | 应用任意几何变换。 |
map_coordinates(input, coordinates[, ...]) | 通过插值将输入数组映射到新坐标。 |
rotate(input, angle[, axes, reshape, ...]) | 旋转数组。 |
shift(input, shift[, output, order, mode, ...]) | 移动数组。 |
spline_filter(input[, order, output, mode]) | 多维样条滤波器。 |
spline_filter1d(input[, order, axis, ...]) | 沿给定轴计算一维样条滤波器。 |
zoom(input, zoom[, output, order, mode, ...]) | 缩放数组。 |
测量
center_of_mass(input[, labels, index]) | 计算数组中标签值的质心。 |
|---|---|
extrema(input[, labels, index]) | 计算数组中标签处的最小值和最大值及其位置。 |
find_objects(input[, max_label]) | 在标记数组中查找对象。 |
histogram(input, min, max, bins[, labels, index]) | 计算数组值的直方图,可选地在标签处计算。 |
label(input[, structure, output]) | 对数组中的特征进行标记。 |
labeled_comprehension(input, labels, index, ...) | 大致相当于 [func(input[labels == i]) for i in index]。 |
maximum(input[, labels, index]) | 计算带标签区域数组值的最大值。 |
maximum_position(input[, labels, index]) | 查找数组标签处数值最大值的位置。 |
mean(input[, labels, index]) | 计算带标签数组值的平均值。 |
median(input[, labels, index]) | 计算带标签区域数组值的中位数。 |
minimum(input[, labels, index]) | 计算带标签区域数组值的最小值。 |
minimum_position(input[, labels, index]) | 查找数组标签处数值最小值的位置。 |
standard_deviation(input[, labels, index]) | 计算 N 维图像数组值的标准差,可选择指定的子区域。 |
sum_labels(input[, labels, index]) | 计算数组值的总和。 |
value_indices(arr, *[, ignore_value]) | 查找给定数组中每个不同值的索引。 |
variance(input[, labels, index]) | 计算 N 维图像数组值的方差,可选择指定的子区域。 |
watershed_ift(input, markers[, structure, ...]) | 使用图像森林变换算法从标记应用分水岭。 |
形态学
binary_closing(input[, structure, ...]) | 使用给定的结构元素进行多维二值闭运算。 |
|---|---|
binary_dilation(input[, structure, ...]) | 使用给定的结构元素进行多维二进制膨胀。 |
binary_erosion(input[, structure, ...]) | 使用给定的结构元素进行多维二进制腐蚀。 |
binary_fill_holes(input[, structure, ...]) | 填补二进制对象中的空洞。 |
binary_hit_or_miss(input[, structure1, ...]) | 多维二进制击中或错过变换。 |
binary_opening(input[, structure, ...]) | 使用给定的结构元素进行多维二进制开运算。 |
binary_propagation(input[, structure, mask, ...]) | 使用给定的结构元素进行多维二进制传播。 |
black_tophat(input[, size, footprint, ...]) | 多维黑顶帽滤波器。 |
distance_transform_bf(input[, metric, ...]) | 通过蛮力算法进行的距离变换函数。 |
distance_transform_cdt(input[, metric, ...]) | 针对 chamfer 类型变换的距离变换。 |
distance_transform_edt(input[, sampling, ...]) | 精确的欧几里得距离变换。 |
generate_binary_structure(rank, connectivity) | 为二进制形态学操作生成二进制结构。 |
grey_closing(input[, size, footprint, ...]) | 多维灰度闭运算。 |
grey_dilation(input[, size, footprint, ...]) | 计算灰度膨胀,使用结构元素或对应于平坦结构元素的足迹。 |
grey_erosion(input[, size, footprint, ...]) | 计算灰度侵蚀,可以使用结构元素或与平面结构元素对应的足印。 |
grey_opening(input[, size, footprint, ...]) | 多维灰度开运算。 |
iterate_structure(structure, iterations[, ...]) | 通过自身的膨胀迭代结构。 |
morphological_gradient(input[, size, ...]) | 多维形态梯度。 |
morphological_laplace(input[, size, ...]) | 多维形态拉普拉斯。 |
white_tophat(input[, size, footprint, ...]) | 多维白顶帽滤波器。 |
scipy.ndimage.convolve
scipy.ndimage.convolve(input, weights, output=None, mode='reflect', cval=0.0, origin=0)
多维卷积。
数组与给定内核进行卷积。
参数:
inputarray_like
输入数组。
weightsarray_like
权重数组,与输入具有相同数量的维度
outputarray or dtype, optional
放置输出的数组,或者返回数组的 dtype。默认情况下,将创建与输入相同 dtype 的数组。
mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}, optional
mode参数确定如何扩展输入数组超出其边界。默认为‘reflect’。每个有效值的行为如下:
‘reflect’ (d c b a | a b c d | d c b a)
通过关于最后一个像素边缘的反射来扩展输入。这种模式有时也称为半样本对称。
‘constant’ (k k k k | a b c d | k k k k)
通过填充所有超出边缘的值来扩展输入,填充值由cval参数定义。
‘nearest’ (a a a a | a b c d | d d d d)
通过复制最后一个像素来扩展输入。
‘mirror’ (d c b | a b c d | c b a)
通过关于最后一个像素中心的反射来扩展输入。这种模式有时也称为全样本对称。
‘wrap’ (a b c d | a b c d | a b c d)
通过环绕到相反的边缘扩展输入。
为了与插值函数保持一致,还可以使用以下模式名称:
‘grid-mirror’
这是‘reflect’的同义词。
‘grid-constant’
这是‘constant’的同义词。
‘grid-wrap’
这是‘wrap’的同义词。
cvalscalar, optional
如果mode为‘constant’,则用来填充输入边缘之外的值。默认为 0.0。
originint, optional
控制输入信号的原点,即滤波器产生输出的第一个元素的中心。正值将滤波器向右移动,负值将滤波器向左移动。默认为 0。
返回:
resultndarray
input与weights的卷积结果。
参见
correlate
将图像与内核相关联。
注意
结果中的每个值为(C_i = \sum_j{I_{i+k-j} W_j}),其中 W 是weights内核,j 是在(W)上的 N-D 空间索引,I 是input,k 是由输入参数中的origin指定的 W 中心的坐标。
示例
可能最简单的情况是理解mode='constant', cval=0.0,因为在这种情况下,边界(即在input的任一值上,weights内核延伸超出input的边缘)被视为零。
>>> import numpy as np
>>> a = np.array([[1, 2, 0, 0],
... [5, 3, 0, 4],
... [0, 0, 0, 7],
... [9, 3, 0, 0]])
>>> k = np.array([[1,1,1],[1,1,0],[1,0,0]])
>>> from scipy import ndimage
>>> ndimage.convolve(a, k, mode='constant', cval=0.0)
array([[11, 10, 7, 4],
[10, 3, 11, 11],
[15, 12, 14, 7],
[12, 3, 7, 0]])
设置cval=1.0等效于在input的外边缘填充 1.0(然后仅提取结果的原始区域)。
>>> ndimage.convolve(a, k, mode='constant', cval=1.0)
array([[13, 11, 8, 7],
[11, 3, 11, 14],
[16, 12, 14, 10],
[15, 6, 10, 5]])
使用mode='reflect'(默认情况下),外部值会在input边缘处反射,以填补缺失的值。
>>> b = np.array([[2, 0, 0],
... [1, 0, 0],
... [0, 0, 0]])
>>> k = np.array([[0,1,0], [0,1,0], [0,1,0]])
>>> ndimage.convolve(b, k, mode='reflect')
array([[5, 0, 0],
[3, 0, 0],
[1, 0, 0]])
在角落处,包括对角线方向。
>>> k = np.array([[1,0,0],[0,1,0],[0,0,1]])
>>> ndimage.convolve(b, k)
array([[4, 2, 0],
[3, 2, 0],
[1, 1, 0]])
使用mode='nearest'时,会将最接近边缘的单个值在input中重复,以匹配重叠的weights。
>>> c = np.array([[2, 0, 1],
... [1, 0, 0],
... [0, 0, 0]])
>>> k = np.array([[0, 1, 0],
... [0, 1, 0],
... [0, 1, 0],
... [0, 1, 0],
... [0, 1, 0]])
>>> ndimage.convolve(c, k, mode='nearest')
array([[7, 0, 3],
[5, 0, 2],
[3, 0, 1]])
scipy.ndimage.convolve1d
scipy.ndimage.convolve1d(input, weights, axis=-1, output=None, mode='reflect', cval=0.0, origin=0)
沿给定轴计算一维卷积。
沿给定轴对数组的行进行卷积。
参数:
input类似数组
输入数组。
weights数组
1-D 数字序列。
axis整数,可选
input的轴,沿其计算。默认为 -1。
output数组或 dtype,可选
放置输出的数组,或返回数组的 dtype。默认情况下,将创建与输入相同 dtype 的数组。
mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’},可选
mode 参数确定输入数组如何超出其边界。默认为 'reflect'。每个有效值的行为如下:
‘reflect’ (d c b a | a b c d | d c b a)
输入通过最后一个像素边缘反射来扩展。有时这种模式也被称为半样本对称。
‘constant’ (k k k k | a b c d | k k k k)
输入通过填充所有超出边缘的值与由 cval 参数定义的相同常量值来扩展。
‘nearest’ (a a a a | a b c d | d d d d)
输入通过复制最后一个像素来扩展。
‘mirror’ (d c b | a b c d | c b a)
输入通过最后一个像素的中心反射扩展。有时这种模式也被称为整样本对称。
‘wrap’ (a b c d | a b c d | a b c d)
输入通过绕回到相反边缘来扩展。
为了与插值函数保持一致,以下模式名称也可以使用:
‘grid-mirror’
这是“reflect”的同义词。
‘grid-constant’
这是“constant”的同义词。
‘grid-wrap’
这是“wrap”的同义词。
cval标量,可选
如果 mode 为 'constant',则用来填充输入之外边缘的值。默认值为 0.0。
origin整数,可选
控制滤波器在输入数组像素上的放置。值为 0(默认)将滤波器居中于像素上,正值将滤波器向左移动,负值向右移动。
返回:
convolve1d数组
与输入相同形状的卷积数组
示例
>>> from scipy.ndimage import convolve1d
>>> convolve1d([2, 8, 0, 4, 1, 9, 9, 0], weights=[1, 3])
array([14, 24, 4, 13, 12, 36, 27, 0])
scipy.ndimage.correlate
scipy.ndimage.correlate(input, weights, output=None, mode='reflect', cval=0.0, origin=0)
多维相关性。
数组与给定核心相关。
参数:
input类数组
输入数组。
weightsndarray
与输入相同维数的权重数组。
output数组或数据类型,可选
放置输出的数组或返回数组的数据类型。默认情况下将创建与输入相同数据类型的数组。
mode{‘reflect’,‘constant’,‘nearest’,‘mirror’,‘wrap’},可选
mode参数确定如何扩展输入数组超出其边界的方式。默认为‘reflect’。每个有效值的行为如下:
‘reflect’(d c b a | a b c d | d c b a)
输入通过关于最后一个像素边缘的反射进行扩展。有时也称为半样本对称。
‘constant’(k k k k | a b c d | k k k k)
输入通过使用由cval参数定义的相同常数值填充边缘之外的所有值进行扩展。
‘nearest’(a a a a | a b c d | d d d d)
输入通过复制最后一个像素进行扩展。
‘mirror’(d c b | a b c d | c b a)
输入通过关于最后一个像素中心的反射进行扩展。有时也称为整样本对称。
‘wrap’(a b c d | a b c d | a b c d)
输入通过绕到相反边缘来扩展。
为了与插值函数保持一致,还可以使用以下模式名称:
‘grid-mirror’
这是‘reflect’的同义词。
‘grid-constant’
这是‘constant’的同义词。
‘grid-wrap’
这是‘wrap’的同义词。
cval标量,可选
如果mode为‘constant’,则填充输入超出边缘的值。默认为 0.0。
origin整数或序列,可选
控制过滤器在输入数组像素上的放置位置。值为 0(默认)将过滤器居中于像素上,正值将过滤器向左移动,负值将其向右移动。通过传递与输入数组维数相同长度的起始序列,可以在每个轴上指定不同的偏移量。
返回:
resultndarray
输入与权重的相关性结果。
另请参阅
convolve
用核心对图像进行卷积。
示例
相关性是将常称为核心的滤波器掩模移动到图像上并计算每个位置的乘积之和的过程。
>>> from scipy.ndimage import correlate
>>> import numpy as np
>>> input_img = np.arange(25).reshape(5,5)
>>> print(input_img)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
定义一个核心(权重),用于相关性。在本例中,是为了中心和上、下、左、右相邻元素的和。
>>> weights = [[0, 1, 0],
... [1, 1, 1],
... [0, 1, 0]]
我们可以计算相关性结果:例如,元素[2,2]为7 + 11 + 12 + 13 + 17 = 60。
>>> correlate(input_img, weights)
array([[ 6, 10, 15, 20, 24],
[ 26, 30, 35, 40, 44],
[ 51, 55, 60, 65, 69],
[ 76, 80, 85, 90, 94],
[ 96, 100, 105, 110, 114]])
scipy.ndimage.correlate1d
scipy.ndimage.correlate1d(input, weights, axis=-1, output=None, mode='reflect', cval=0.0, origin=0)
沿给定轴计算一维相关。
沿给定轴与给定权重相关的数组行。
参数:
inputarray_like
输入数组。
weightsarray
一维数字序列。
axisint, 可选
计算的input轴。默认为-1。
outputarray 或 dtype, 可选
放置输出的数组,或返回数组的数据类型。默认情况下,将创建与输入相同数据类型的数组。
mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}, 可选
mode参数确定如何扩展输入数组超出其边界的方式。默认为‘reflect’。每个有效值的行为如下:
‘reflect’ (d c b a | a b c d | d c b a)
输入通过关于最后一个像素的边缘反射进行扩展。此模式有时也称为半样本对称。
‘constant’ (k k k k | a b c d | k k k k)
通过使用cval参数定义的相同常量值填充所有超出边缘的值来扩展输入。
‘nearest’ (a a a a | a b c d | d d d d)
输入通过复制最后一个像素进行扩展。
‘mirror’ (d c b | a b c d | c b a)
输入通过反射最后一个像素的中心进行扩展。此模式有时也称为整样本对称。
‘wrap’ (a b c d | a b c d | a b c d)
输入通过环绕到相反边缘进行扩展。
为了与插值函数保持一致,以下模式名称也可以使用:
‘grid-mirror’
这是“reflect”的同义词。
‘grid-constant’
这是“constant”的同义词。
‘grid-wrap’
这是“wrap”的同义词。
cvalscalar, 可选
如果mode为‘constant’,则用于填充输入边界之外的值的值。默认为 0.0。
originint, 可选
控制滤波器在输入数组像素上的放置。默认值为 0,将滤波器居中于像素,正值将滤波器向左移动,负值向右移动。
返回:
resultndarray
相关结果。具有与input相同的形状。
示例
>>> from scipy.ndimage import correlate1d
>>> correlate1d([2, 8, 0, 4, 1, 9, 9, 0], weights=[1, 3])
array([ 8, 26, 8, 12, 7, 28, 36, 9])
scipy.ndimage.gaussian_filter
scipy.ndimage.gaussian_filter(input, sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0, *, radius=None, axes=None)
多维高斯滤波器。
参数:
输入array_like
输入数组。
sigma标量或标量序列
高斯核的标准偏差。高斯滤波器的标准偏差作为序列给出,或作为单个数字,此时对于所有轴都是相等的。
顺序整数或整数序列,可选
每个轴上的滤波器顺序以整数序列或单个数字给出。顺序为 0 对应于使用高斯核的卷积。正数顺序对应于与高斯导数的卷积。
输出数组或 dtype,可选
要放置输出的数组,或返回数组的 dtype。默认情况下,将创建与输入相同 dtype 的数组。
模式字符串或序列,可选
当滤波器重叠边界时,mode参数决定如何扩展输入数组。通过传递与输入数组维数相等的模式序列,可以指定每个轴上的不同模式。默认值为‘reflect’。有效的值及其行为如下:
‘reflect’ (d c b a | a b c d | d c b a)
输入通过关于最后一个像素边缘的反射进行扩展。此模式有时也称为半样本对称。
‘constant’ (k k k k | a b c d | k k k k)
输入通过填充超出边缘的所有值来扩展,填充值由cval参数定义。
‘nearest’ (a a a a | a b c d | d d d d)
输入通过复制最后一个像素来扩展。
‘mirror’ (d c b | a b c d | c b a)
输入通过关于最后一个像素中心的反射进行扩展。此模式有时也称为全样本对称。
‘wrap’ (a b c d | a b c d | a b c d)
输入通过包裹到对立边缘来扩展。
为了与插值函数保持一致,还可以使用以下模式名称:
‘grid-constant’
这是“constant”的同义词。
‘grid-mirror’
这是“reflect”的同义词。
‘grid-wrap’
这是“wrap”的同义词。
cval标量,可选
如果mode为‘constant’,则填充输入边缘之外的值。默认为 0.0。
截断浮点数,可选
在此标准偏差截断滤波器。默认为 4.0。
半径None 或整数或整数序列,可选
高斯核的半径。半径作为序列给出,或作为单个数字,此时对于所有轴都是相等的。如果指定,则沿每个轴的核大小将为2*radius + 1,truncate将被忽略。默认为 None。
轴整数元组或 None,可选
如果为 None,则 input 沿所有轴进行滤波。否则,将沿指定轴进行滤波。当指定 axes 时,用于 sigma、order、mode 和/或 radius 的任何元组必须与 axes 的长度相匹配。这些元组中的第 i 个条目对应于 axes 中的第 i 个条目。
返回:
gaussian_filterndarray
返回形状与 input 相同的数组。
注释
多维滤波器实现为一系列 1-D 卷积滤波器。中间数组以与输出相同的数据类型存储。因此,对于具有有限精度的输出类型,由于中间结果可能存储不足的精度,结果可能不精确。
高斯核在每个轴向上的大小为2*radius + 1。如果 radius 为 None,则默认使用 radius = round(truncate * sigma)。
示例
>>> from scipy.ndimage import gaussian_filter
>>> import numpy as np
>>> a = np.arange(50, step=2).reshape((5,5))
>>> a
array([[ 0, 2, 4, 6, 8],
[10, 12, 14, 16, 18],
[20, 22, 24, 26, 28],
[30, 32, 34, 36, 38],
[40, 42, 44, 46, 48]])
>>> gaussian_filter(a, sigma=1)
array([[ 4, 6, 8, 9, 11],
[10, 12, 14, 15, 17],
[20, 22, 24, 25, 27],
[29, 31, 33, 34, 36],
[35, 37, 39, 40, 42]])
>>> from scipy import datasets
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> plt.gray() # show the filtered result in grayscale
>>> ax1 = fig.add_subplot(121) # left side
>>> ax2 = fig.add_subplot(122) # right side
>>> ascent = datasets.ascent()
>>> result = gaussian_filter(ascent, sigma=5)
>>> ax1.imshow(ascent)
>>> ax2.imshow(result)
>>> plt.show()
scipy.ndimage.gaussian_filter1d
scipy.ndimage.gaussian_filter1d(input, sigma, axis=-1, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0, *, radius=None)
1-D 高斯滤波器。
参数:
inputarray_like
输入数组。
sigmascalar
高斯核的标准差。
axisint,可选
input沿其计算的轴。默认值为-1。
orderint,可选
顺序为 0 对应于与高斯核卷积。正序对应于高斯的那个导数的卷积。
outputarray 或 dtype,可选
放置输出的数组,或者返回数组的 dtype。默认情况下,将创建与输入相同 dtype 的数组。
mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’},可选
mode参数确定如何扩展输入数组超出其边界。默认值为‘reflect’。每个有效值的行为如下:
‘reflect’ (d c b a | a b c d | d c b a)
输入通过反射最后一个像素的边界来扩展。此模式有时也称为半样本对称。
‘constant’ (k k k k | a b c d | k k k k)
输入通过使用与cval参数定义的相同常数值填充边缘以扩展。
‘nearest’ (a a a a | a b c d | d d d d)
输入通过复制最后一个像素来扩展。
‘mirror’ (d c b | a b c d | c b a)
输入通过反射最后一个像素的中心来扩展。此模式有时也称为整样本对称。
‘wrap’ (a b c d | a b c d | a b c d)
输入通过环绕到相反边缘来扩展。
为了与插值函数保持一致,还可以使用以下模式名称:
‘grid-mirror’
这是“reflect”的同义词。
‘grid-constant’
这是“constant”的同义词。
‘grid-wrap’
这是“wrap”的同义词。
cvalscalar,可选
如果mode为‘constant’,则填充输入边缘之外的值。默认值为 0.0。
truncatefloat,可选
在此标准差处截断滤波器。默认值为 4.0。
radiusNone 或 int,可选
高斯核的半径。如果指定,核的大小将为2*radius + 1,truncate将被忽略。默认值为 None。
返回:
gaussian_filter1dndarray
注意事项
高斯核沿每个轴的大小将为2*radius + 1。如果radius为 None,则使用默认值radius = round(truncate * sigma)。
示例
>>> from scipy.ndimage import gaussian_filter1d
>>> import numpy as np
>>> gaussian_filter1d([1.0, 2.0, 3.0, 4.0, 5.0], 1)
array([ 1.42704095, 2.06782203, 3\. , 3.93217797, 4.57295905])
>>> gaussian_filter1d([1.0, 2.0, 3.0, 4.0, 5.0], 4)
array([ 2.91948343, 2.95023502, 3\. , 3.04976498, 3.08051657])
>>> import matplotlib.pyplot as plt
>>> rng = np.random.default_rng()
>>> x = rng.standard_normal(101).cumsum()
>>> y3 = gaussian_filter1d(x, 3)
>>> y6 = gaussian_filter1d(x, 6)
>>> plt.plot(x, 'k', label='original data')
>>> plt.plot(y3, '--', label='filtered, sigma=3')
>>> plt.plot(y6, ':', label='filtered, sigma=6')
>>> plt.legend()
>>> plt.grid()
>>> plt.show()