本文整理汇总了Python中MatrixUtil.assert_2d方法的典型用法代码示例。如果您正苦于以下问题:Python MatrixUtil.assert_2d方法的具体用法?Python MatrixUtil.assert_2d怎么用?Python MatrixUtil.assert_2d使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MatrixUtil
的用法示例。
在下文中一共展示了MatrixUtil.assert_2d方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_wcss
# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_2d [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))
示例2: get_centers
# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_2d [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
示例3: get_point_center_sqdists
# 需要导入模块: import MatrixUtil [as 别名]
# 或者: from MatrixUtil import assert_2d [as 别名]
def get_point_center_sqdists(points, centers):
"""
Inputs and outputs are numpy arrays.
@param points: the points to be reclustered
@param centers: cluster centers
@return: for each point, the squared distance to each center
"""
MatrixUtil.assert_2d(points)
MatrixUtil.assert_2d(centers)
npoints = len(points)
ncenters = len(centers)
# get the dot products of points with themselves
pself = np.sum(points*points, axis=1)
# get the dot products of centers with themselves
cself = np.sum(centers*centers, axis=1)
# get the matrix product of points and centers
prod = np.dot(points, centers.T)
# get the matrix of squared distances
sqdists = (
np.outer(pself, np.ones(ncenters)) +
np.outer(np.ones(npoints), cself) -
2*prod)
return sqdists