本文整理汇总了Python中numpy.linalg.matrix_rank方法的典型用法代码示例。如果您正苦于以下问题:Python linalg.matrix_rank方法的具体用法?Python linalg.matrix_rank怎么用?Python linalg.matrix_rank使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.linalg
的用法示例。
在下文中一共展示了linalg.matrix_rank方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matrix_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_matrix_rank(self):
# Full rank matrix
assert_equal(4, matrix_rank(np.eye(4)))
# rank deficient matrix
I = np.eye(4)
I[-1, -1] = 0.
assert_equal(matrix_rank(I), 3)
# All zeros - zero rank
assert_equal(matrix_rank(np.zeros((4, 4))), 0)
# 1 dimension - rank 1 unless all 0
assert_equal(matrix_rank([1, 0, 0, 0]), 1)
assert_equal(matrix_rank(np.zeros((4,))), 0)
# accepts array-like
assert_equal(matrix_rank([1]), 1)
# greater than 2 dimensions treated as stacked matrices
ms = np.array([I, np.eye(4), np.zeros((4,4))])
assert_equal(matrix_rank(ms), np.array([3, 4, 0]))
# works on scalar
assert_equal(matrix_rank(1), 1)
示例2: test_matrix_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_matrix_rank(self):
# Full rank matrix
yield assert_equal, 4, matrix_rank(np.eye(4))
# rank deficient matrix
I = np.eye(4)
I[-1, -1] = 0.
yield assert_equal, matrix_rank(I), 3
# All zeros - zero rank
yield assert_equal, matrix_rank(np.zeros((4, 4))), 0
# 1 dimension - rank 1 unless all 0
yield assert_equal, matrix_rank([1, 0, 0, 0]), 1
yield assert_equal, matrix_rank(np.zeros((4,))), 0
# accepts array-like
yield assert_equal, matrix_rank([1]), 1
# greater than 2 dimensions raises error
yield assert_raises, TypeError, matrix_rank, np.zeros((2, 2, 2))
# works on scalar
yield assert_equal, matrix_rank(1), 1
示例3: test_matrix_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_matrix_rank(self):
# Full rank matrix
yield assert_equal, 4, matrix_rank(np.eye(4))
# rank deficient matrix
I = np.eye(4)
I[-1, -1] = 0.
yield assert_equal, matrix_rank(I), 3
# All zeros - zero rank
yield assert_equal, matrix_rank(np.zeros((4, 4))), 0
# 1 dimension - rank 1 unless all 0
yield assert_equal, matrix_rank([1, 0, 0, 0]), 1
yield assert_equal, matrix_rank(np.zeros((4,))), 0
# accepts array-like
yield assert_equal, matrix_rank([1]), 1
# greater than 2 dimensions treated as stacked matrices
ms = np.array([I, np.eye(4), np.zeros((4,4))])
yield assert_equal, matrix_rank(ms), np.array([3, 4, 0])
# works on scalar
yield assert_equal, matrix_rank(1), 1
示例4: _multivariate_ols_test
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def _multivariate_ols_test(hypotheses, fit_results, exog_names,
endog_names):
def fn(L, M, C):
# .. [1] https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_introreg_sect012.htm
params, df_resid, inv_cov, sscpr = fit_results
# t1 = (L * params)M
t1 = L.dot(params).dot(M) - C
# H = t1'L(X'X)^L't1
t2 = L.dot(inv_cov).dot(L.T)
q = matrix_rank(t2)
H = t1.T.dot(inv(t2)).dot(t1)
# E = M'(Y'Y - B'(X'X)B)M
E = M.T.dot(sscpr).dot(M)
return E, H, q, df_resid
return _multivariate_test(hypotheses, exog_names, endog_names, fn)
示例5: test_matrix_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_matrix_rank(self):
# Full rank matrix
yield assert_equal, 4, matrix_rank(np.eye(4))
# rank deficient matrix
I=np.eye(4); I[-1, -1] = 0.
yield assert_equal, matrix_rank(I), 3
# All zeros - zero rank
yield assert_equal, matrix_rank(np.zeros((4, 4))), 0
# 1 dimension - rank 1 unless all 0
yield assert_equal, matrix_rank([1, 0, 0, 0]), 1
yield assert_equal, matrix_rank(np.zeros((4,))), 0
# accepts array-like
yield assert_equal, matrix_rank([1]), 1
# greater than 2 dimensions raises error
yield assert_raises, TypeError, matrix_rank, np.zeros((2, 2, 2))
# works on scalar
yield assert_equal, matrix_rank(1), 1
示例6: find_colinear_columns
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def find_colinear_columns(
arr: np.ndarray,
arr_rank: Optional[int]=None) -> List[int]:
if arr_rank is None:
arr_rank = la.matrix_rank(arr)
K = arr.shape[1]
num_colinear_cols = K - arr_rank
if num_colinear_cols <= 0: raise ValueError("No colinear columns.")
# Cycle through all columns; if a col doesn't increase rank, it's
# colinear so flag it
target_rank = 2
colinear_cols = []
for j in range(1, K):
sub_rank = la.matrix_rank(arr[:, :(j + 1)])
if sub_rank == target_rank:
target_rank += 1
else:
colinear_cols.append(j)
if len(colinear_cols) == num_colinear_cols:
break # We found them all; quit.
return colinear_cols
示例7: test_2_in_1_ACA_with_different_matrices
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_2_in_1_ACA_with_different_matrices():
n = 5
A = np.arange(1, 1+n**2).reshape((n, n)) + np.random.rand(n, n)
B = A.T
def get_row_func(i):
return A[i, :], B[i, :]
def get_col_func(j):
return A[:, j], B[:, j]
lrA, lrB = LowRankMatrix.from_rows_and_cols_functions_with_multi_ACA(
get_row_func, get_col_func, n, n, nb_matrices=2, max_rank=3
)
assert matrix_rank(lrA.full_matrix()) == matrix_rank(lrB.full_matrix()) == 3
示例8: test_symmetric_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_symmetric_rank(self):
assert_equal(4, matrix_rank(np.eye(4), hermitian=True))
assert_equal(1, matrix_rank(np.ones((4, 4)), hermitian=True))
assert_equal(0, matrix_rank(np.zeros((4, 4)), hermitian=True))
# rank deficient matrix
I = np.eye(4)
I[-1, -1] = 0.
assert_equal(3, matrix_rank(I, hermitian=True))
# manually supplied tolerance
I[-1, -1] = 1e-8
assert_equal(4, matrix_rank(I, hermitian=True, tol=0.99e-8))
assert_equal(3, matrix_rank(I, hermitian=True, tol=1.01e-8))
示例9: test_reduced_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_reduced_rank():
# Test matrices with reduced rank
rng = np.random.RandomState(20120714)
for i in range(100):
# Make a rank deficient matrix
X = rng.normal(size=(40, 10))
X[:, 0] = X[:, 1] + X[:, 2]
# Assert that matrix_rank detected deficiency
assert_equal(matrix_rank(X), 9)
X[:, 3] = X[:, 4] + X[:, 5]
assert_equal(matrix_rank(X), 8)
示例10: get_R_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def get_R_rank(self):
"""Get the rank of the R matrix.
Returns
-------
rank : int
The rank of the R matrix
"""
return matrix_rank(self.get_R())
示例11: test_symmetric_rank
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def test_symmetric_rank(self):
yield assert_equal, 4, matrix_rank(np.eye(4), hermitian=True)
yield assert_equal, 1, matrix_rank(np.ones((4, 4)), hermitian=True)
yield assert_equal, 0, matrix_rank(np.zeros((4, 4)), hermitian=True)
# rank deficient matrix
I = np.eye(4)
I[-1, -1] = 0.
yield assert_equal, 3, matrix_rank(I, hermitian=True)
# manually supplied tolerance
I[-1, -1] = 1e-8
yield assert_equal, 4, matrix_rank(I, hermitian=True, tol=0.99e-8)
yield assert_equal, 3, matrix_rank(I, hermitian=True, tol=1.01e-8)
示例12: initialize
# 需要导入模块: from numpy import linalg [as 别名]
# 或者: from numpy.linalg import matrix_rank [as 别名]
def initialize(self, design):
# PLEASE don't assume we have a constant...
# TODO: handle case for noconstant regression
self.design = design
self.whitened_design = self.whiten(self.design)
self.calc_beta = spl.pinv(self.whitened_design)
self.normalized_cov_beta = np.dot(self.calc_beta,
np.transpose(self.calc_beta))
self.df_total = self.whitened_design.shape[0]
eps = np.abs(self.design).sum() * np.finfo(np.float).eps
self.df_model = matrix_rank(self.design, eps)
self.df_residuals = self.df_total - self.df_model