本文整理汇总了Python中MatrixUtil.assert_1d方法的典型用法代码示例。如果您正苦于以下问题:Python MatrixUtil.assert_1d方法的具体用法?Python MatrixUtil.assert_1d怎么用?Python MatrixUtil.assert_1d使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MatrixUtil
的用法示例。
在下文中一共展示了MatrixUtil.assert_1d方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bott_duffin
# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_1d [as 别名]
def bott_duffin(M, v):
"""
Compute a constrained generalized inverse.
Specifically, this is the Bott-Duffin inverse of M
constrained to the orthogonal complement of v.
This function assumes that v has rank 1,
although Bott-Duffin inverses are also defined
for inverses constrained to orthogonal complements
of higher dimensional subspaces.
Maybe this could be a separate python function
where v is replaced by a shape-2 numpy array.
@param M: a matrix
@param v: a vector
@return: the constrained generalized inverse of M
"""
# check the shapes of the input matrix and vector
MatrixUtil.assert_1d(v)
n = len(v)
if M.shape != (n, n):
raise ValueError('M and v have incompatible shapes')
# check that v is nonzero
v_dot_v = np.inner(v, v)
if not v_dot_v:
raise ValueError('expected nonzero v')
# compute the orthogonal projection onto v
P = np.outer(v, v) / v_dot_v
# compute the orthogonal projection onto the orthogonal complement of v
I = np.eye(n)
C = I - P
# compute the constrained generalized inverse
B = np.dot(C, np.linalg.inv(np.dot(M, C) + P))
return B
示例2: get_wcss
# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_1d [as 别名]
def get_wcss(sqdists, labels):
"""
Get the within-cluster sum of squares.
@param sqdists: for each point, the squared distance to each center
@param labels: cluster labels
@return: within-cluster sum of squares
"""
MatrixUtil.assert_2d(sqdists)
MatrixUtil.assert_1d(labels)
if len(sqdists) != len(labels):
raise ValueError('array incompatibility')
return sum(row[label] for row, label in zip(sqdists, labels))
示例3: get_centers
# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_1d [as 别名]
def get_centers(points, labels):
"""
Inputs and outputs are numpy arrays.
@param points: euclidean points
@param labels: conformant cluster indices
"""
MatrixUtil.assert_2d(points)
MatrixUtil.assert_1d(labels)
if len(points) != len(labels):
raise ValueError('array incompatibility')
ncoords = len(points[0])
nclusters = max(labels) + 1
sums = [np.zeros(ncoords) for i in range(nclusters)]
counts = [0]*nclusters
for point, label in zip(points, labels):
sums[label] += point
counts[label] += 1
M = np.array([s/c for s, c in zip(sums, counts)])
return M