本文整理汇总了Python中numpy.asarray_chkfinite方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.asarray_chkfinite方法的具体用法?Python numpy.asarray_chkfinite怎么用?Python numpy.asarray_chkfinite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.asarray_chkfinite方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cholesky
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def _cholesky(a, lower=False, overwrite_a=False, clean=True,
check_finite=True):
"""Common code for cholesky() and cho_factor()."""
if check_finite:
a1 = asarray_chkfinite(a)
else:
a1 = asarray(a)
if len(a1.shape) != 2 or a1.shape[0] != a1.shape[1]:
raise ValueError('expected square matrix')
overwrite_a = overwrite_a or _datacopied(a1, a)
potrf, = get_lapack_funcs(('potrf',), (a1,))
c, info = potrf(a1, lower=lower, overwrite_a=overwrite_a, clean=clean)
if info > 0:
raise LinAlgError("%d-th leading minor not positive definite" % info)
if info < 0:
raise ValueError('illegal value in %d-th argument of internal potrf'
% -info)
return c, lower
示例2: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def __init__(self, inverse_array, lower=True, make_triangular=True):
"""
Args:
inverse_array (array): 2D containing values of *inverse* of this
matrix, with the inverse of a lower (upper) triangular matrix
being itself lower (upper) triangular. Any values above (below)
diagonal are ignored for lower (upper) triangular matrices i.e.
when `lower == True` (`lower == False`).
lower (bool): Whether the matrix is lower-triangular (`True`) or
upper-triangular (`False`).
make_triangular (bool): Whether to ensure `inverse_array` is
triangular by explicitly zeroing entries in upper triangle if
`lower == True` and in lower triangle if `lower == False`.
"""
inverse_array = np.asarray_chkfinite(inverse_array)
inverse_array = (
_make_array_triangular(inverse_array, lower) if make_triangular
else inverse_array)
super().__init__(inverse_array.shape, _inverse_array=inverse_array)
self._lower = lower
示例3: _fractal_mfdfa_q
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def _fractal_mfdfa_q(q=2):
# TODO: Add log calculator for q ≈ 0
# Fractal powers as floats
q = np.asarray_chkfinite(q, dtype=np.float)
# Ensure q≈0 is removed, since it does not converge. Limit set at |q| < 0.1
q = q[(q < -0.1) + (q > 0.1)]
# Reshape q to perform np.float_power
q = q.reshape(-1, 1)
return q
示例4: pinv
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def pinv(a, cond=None, rcond=None):
"""Compute the (Moore-Penrose) pseudo-inverse of a matrix.
Calculate a generalized inverse of a matrix using a least-squares
solver.
Parameters
----------
a : array, shape (M, N)
Matrix to be pseudo-inverted
cond, rcond : float
Cutoff for 'small' singular values in the least-squares solver.
Singular values smaller than rcond*largest_singular_value are
considered zero.
Returns
-------
B : array, shape (N, M)
Raises LinAlgError if computation does not converge
Examples
--------
>>> from numpy import *
>>> a = random.randn(9, 6)
>>> B = linalg.pinv(a)
>>> allclose(a, dot(a, dot(B, a)))
True
>>> allclose(B, dot(B, dot(a, B)))
True
"""
a = asarray_chkfinite(a)
b = numpy.identity(a.shape[0], dtype=a.dtype)
if rcond is not None:
cond = rcond
return lstsq(a, b, cond=cond)[0]
示例5: check_and_set_idx
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def check_and_set_idx(ids, idx, prefix):
""" Reconciles passed-in IDs and indices and returns indices, as well as unique IDs
in the order specified by the indices. If only IDs supplied, returns the sort-arg
as the index. If only indices supplied, returns None for IDs. If both supplied,
checks that the correspondence is unique and returns unique IDs in the sort order of
the associated index.
:param np.ndarray ids: array of IDs
:param np.ndarray[int] idx: array of indices
:param str prefix: variable name (for error logging)
:return: unique IDs and indices (passed in or derived from the IDs)
:rtype: np.ndarray, np.ndarray
"""
if ids is None and idx is None:
raise ValueError('Both {}_ids and {}_idx cannot be None'.format(prefix, prefix))
if ids is None:
return None, np.asarray_chkfinite(idx)
if idx is None:
return np.unique(ids, return_inverse=True)
else:
ids = np.asarray(ids)
idx = np.asarray_chkfinite(idx)
if len(idx) != len(ids):
raise ValueError('{}_ids ({}) and {}_idx ({}) must have the same length'.format(
prefix, len(ids), prefix, len(idx)))
uniq_idx, idx_sort_index = np.unique(idx, return_index=True)
# make sure each unique index corresponds to a unique id
if not all(len(set(ids[idx == i])) == 1 for i in uniq_idx):
raise ValueError("Each index must correspond to a unique {}_id".format(prefix))
return ids[idx_sort_index], idx
示例6: _cholesky
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def _cholesky(a, lower=False, overwrite_a=False, clean=True,
check_finite=True):
"""Common code for cholesky() and cho_factor()."""
a1 = asarray_chkfinite(a) if check_finite else asarray(a)
a1 = atleast_2d(a1)
# Dimension check
if a1.ndim != 2:
raise ValueError('Input array needs to be 2 dimensional but received '
'a {}d-array.'.format(a1.ndim))
# Squareness check
if a1.shape[0] != a1.shape[1]:
raise ValueError('Input array is expected to be square but has '
'the shape: {}.'.format(a1.shape))
# Quick return for square empty array
if a1.size == 0:
return a1.copy(), lower
overwrite_a = overwrite_a or _datacopied(a1, a)
potrf, = get_lapack_funcs(('potrf',), (a1,))
c, info = potrf(a1, lower=lower, overwrite_a=overwrite_a, clean=clean)
if info > 0:
raise LinAlgError("%d-th leading minor of the array is not positive "
"definite" % info)
if info < 0:
raise ValueError('LAPACK reported an illegal value in {}-th argument'
'on entry to "POTRF".'.format(-info))
return c, lower
示例7: fast_johnson_lindenstrauss
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def fast_johnson_lindenstrauss(A, l, axis=1, random_state=None):
"""
Given an m x n matrix A, and an integer l, this scheme computes an m x l
orthonormal matrix Q whose range approximates the range of A
"""
random_state = check_random_state(random_state)
A = np.asarray_chkfinite(A)
if A.ndim != 2:
raise ValueError('A must be a 2D array, not %dD' % A.ndim)
if axis not in (0, 1):
raise ValueError('If supplied, axis must be in (0, 1)')
# TODO: Find name for sketch and put in _sketches
# construct gaussian random matrix
diag = random_state.choice((-1, 1), size=A.shape[axis]).astype(A.dtype)
if axis == 0:
diag = diag[:, np.newaxis]
# discrete fourier transform of AD (or DA)
FDA = fftpack.dct(A * diag, axis=axis, norm='ortho')
# randomly sample axis
return randomized_uniform_sampling(
FDA, l, axis=axis, random_state=random_state)
示例8: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def fit(self, X, y=None):
'''Fits DMD'''
self.X_ = np.asarray_chkfinite(X)
self.F_, self.l_, self.omega_ = compute_dmd(
self.X_, rank=self.rank, dt=self.dt, modes=self.modes,
order=self.order)
return self
示例9: ackley
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def ackley(x, a=20, b=0.2, c=2*np.pi):
x = np.asarray_chkfinite(x) # ValueError if any NaN or Inf
n = len(x)
s1 = np.sum(x**2, axis=0)
s2 = np.sum(np.cos(c * x), axis=0)
return -a*np.exp(-b*np.sqrt(s1 / n)) - np.exp(s2 / n) + a + np.exp(1)
示例10: nnls
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def nnls(A, b):
"""
Solve ``argmin_x || Ax - b ||_2`` for ``x>=0``. This is a wrapper
for a FORTRAN non-negative least squares solver.
Parameters
----------
A : ndarray
Matrix ``A`` as shown above.
b : ndarray
Right-hand side vector.
Returns
-------
x : ndarray
Solution vector.
rnorm : float
The residual, ``|| Ax-b ||_2``.
Notes
-----
The FORTRAN code was published in the book below. The algorithm
is an active set method. It solves the KKT (Karush-Kuhn-Tucker)
conditions for the non-negative least squares problem.
References
----------
Lawson C., Hanson R.J., (1987) Solving Least Squares Problems, SIAM
"""
A, b = map(asarray_chkfinite, (A, b))
if len(A.shape) != 2:
raise ValueError("expected matrix")
if len(b.shape) != 1:
raise ValueError("expected vector")
m, n = A.shape
if m != b.shape[0]:
raise ValueError("incompatible dimensions")
w = zeros((n,), dtype=double)
zz = zeros((m,), dtype=double)
index = zeros((n,), dtype=int)
x, rnorm, mode = _nnls.nnls(A, m, n, b, w, zz, index)
if mode != 1:
raise RuntimeError("too many iterations")
return x, rnorm
示例11: lu_factor
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def lu_factor(a, overwrite_a=False, check_finite=True):
"""
Compute pivoted LU decomposition of a matrix.
The decomposition is::
A = P L U
where P is a permutation matrix, L lower triangular with unit
diagonal elements, and U upper triangular.
Parameters
----------
a : (M, M) array_like
Matrix to decompose
overwrite_a : bool, optional
Whether to overwrite data in A (may increase performance)
check_finite : bool, optional
Whether to check that the input matrix contains only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
Returns
-------
lu : (N, N) ndarray
Matrix containing U in its upper triangle, and L in its lower triangle.
The unit diagonal elements of L are not stored.
piv : (N,) ndarray
Pivot indices representing the permutation matrix P:
row i of matrix was interchanged with row piv[i].
See also
--------
lu_solve : solve an equation system using the LU factorization of a matrix
Notes
-----
This is a wrapper to the ``*GETRF`` routines from LAPACK.
"""
if check_finite:
a1 = asarray_chkfinite(a)
else:
a1 = asarray(a)
if len(a1.shape) != 2 or (a1.shape[0] != a1.shape[1]):
raise ValueError('expected square matrix')
overwrite_a = overwrite_a or (_datacopied(a1, a))
getrf, = get_lapack_funcs(('getrf',), (a1,))
lu, piv, info = getrf(a1, overwrite_a=overwrite_a)
if info < 0:
raise ValueError('illegal value in %d-th argument of '
'internal getrf (lu_factor)' % -info)
if info > 0:
warn("Diagonal number %d is exactly zero. Singular matrix." % info,
RuntimeWarning)
return lu, piv
示例12: lu_solve
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def lu_solve(lu_and_piv, b, trans=0, overwrite_b=False, check_finite=True):
"""Solve an equation system, a x = b, given the LU factorization of a
Parameters
----------
(lu, piv)
Factorization of the coefficient matrix a, as given by lu_factor
b : array
Right-hand side
trans : {0, 1, 2}, optional
Type of system to solve:
===== =========
trans system
===== =========
0 a x = b
1 a^T x = b
2 a^H x = b
===== =========
overwrite_b : bool, optional
Whether to overwrite data in b (may increase performance)
check_finite : bool, optional
Whether to check that the input matrices contain only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
Returns
-------
x : array
Solution to the system
See also
--------
lu_factor : LU factorize a matrix
"""
(lu, piv) = lu_and_piv
if check_finite:
b1 = asarray_chkfinite(b)
else:
b1 = asarray(b)
overwrite_b = overwrite_b or _datacopied(b1, b)
if lu.shape[0] != b1.shape[0]:
raise ValueError("incompatible dimensions.")
getrs, = get_lapack_funcs(('getrs',), (lu, b1))
x,info = getrs(lu, piv, b1, trans=trans, overwrite_b=overwrite_b)
if info == 0:
return x
raise ValueError('illegal value in %d-th argument of internal gesv|posv'
% -info)
示例13: expm_frechet_kronform
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def expm_frechet_kronform(A, method=None, check_finite=True):
"""
Construct the Kronecker form of the Frechet derivative of expm.
Parameters
----------
A : array_like with shape (N, N)
Matrix to be expm'd.
method : str, optional
Extra keyword to be passed to expm_frechet.
check_finite : bool, optional
Whether to check that the input matrix contains only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
Returns
-------
K : 2d ndarray with shape (N*N, N*N)
Kronecker form of the Frechet derivative of the matrix exponential.
Notes
-----
This function is used to help compute the condition number
of the matrix exponential.
See also
--------
expm : Compute a matrix exponential.
expm_frechet : Compute the Frechet derivative of the matrix exponential.
expm_cond : Compute the relative condition number of the matrix exponential
in the Frobenius norm.
"""
if check_finite:
A = np.asarray_chkfinite(A)
else:
A = np.asarray(A)
if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
raise ValueError('expected a square matrix')
n = A.shape[0]
ident = np.identity(n)
cols = []
for i in range(n):
for j in range(n):
E = np.outer(ident[i], ident[j])
F = expm_frechet(A, E,
method=method, compute_expm=False, check_finite=False)
cols.append(vec(F))
return np.vstack(cols).T
示例14: expm_cond
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def expm_cond(A, check_finite=True):
"""
Relative condition number of the matrix exponential in the Frobenius norm.
Parameters
----------
A : 2d array_like
Square input matrix with shape (N, N).
check_finite : bool, optional
Whether to check that the input matrix contains only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
Returns
-------
kappa : float
The relative condition number of the matrix exponential
in the Frobenius norm
Notes
-----
A faster estimate for the condition number in the 1-norm
has been published but is not yet implemented in scipy.
.. versionadded:: 0.14.0
See also
--------
expm : Compute the exponential of a matrix.
expm_frechet : Compute the Frechet derivative of the matrix exponential.
"""
if check_finite:
A = np.asarray_chkfinite(A)
else:
A = np.asarray(A)
if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
raise ValueError('expected a square matrix')
X = scipy.linalg.expm(A)
K = expm_frechet_kronform(A, check_finite=False)
# The following norm choices are deliberate.
# The norms of A and X are Frobenius norms,
# and the norm of K is the induced 2-norm.
A_norm = scipy.linalg.norm(A, 'fro')
X_norm = scipy.linalg.norm(X, 'fro')
K_norm = scipy.linalg.norm(K, 2)
kappa = (K_norm * A_norm) / X_norm
return kappa
示例15: cho_solve
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray_chkfinite [as 别名]
def cho_solve(c_and_lower, b, overwrite_b=False, check_finite=True):
"""Solve the linear equations A x = b, given the Cholesky factorization of A.
Parameters
----------
(c, lower) : tuple, (array, bool)
Cholesky factorization of a, as given by cho_factor
b : array
Right-hand side
overwrite_b : bool, optional
Whether to overwrite data in b (may improve performance)
check_finite : bool, optional
Whether to check that the input matrices contain only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
Returns
-------
x : array
The solution to the system A x = b
See also
--------
cho_factor : Cholesky factorization of a matrix
"""
(c, lower) = c_and_lower
if check_finite:
b1 = asarray_chkfinite(b)
c = asarray_chkfinite(c)
else:
b1 = asarray(b)
c = asarray(c)
if c.ndim != 2 or c.shape[0] != c.shape[1]:
raise ValueError("The factored matrix c is not square.")
if c.shape[1] != b1.shape[0]:
raise ValueError("incompatible dimensions.")
overwrite_b = overwrite_b or _datacopied(b1, b)
potrs, = get_lapack_funcs(('potrs',), (c, b1))
x, info = potrs(c, b1, lower=lower, overwrite_b=overwrite_b)
if info != 0:
raise ValueError('illegal value in %d-th argument of internal potrs'
% -info)
return x