MCPcopy Index your code
hub / github.com/clips/pattern / centroid

Function centroid

pattern/vector/__init__.py:1402–1421  ·  view source on GitHub ↗

Returns the center of the given list of vectors. For example: if each vector has two features, (x, y)-coordinates in 2D space, the centroid is the geometric center of the coordinates forming a polygon. Since vectors are sparse (i.e., features with weight 0 are omitted),

(vectors=[], features=[])

Source from the content-addressed store, hash-verified

1400 return sum(iterable) / float(length or 1)
1401
1402def centroid(vectors=[], features=[]):
1403 """ Returns the center of the given list of vectors.
1404 For example: if each vector has two features, (x, y)-coordinates in 2D space,
1405 the centroid is the geometric center of the coordinates forming a polygon.
1406 Since vectors are sparse (i.e., features with weight 0 are omitted),
1407 the list of all features (= Model.vector) must be given.
1408 """
1409 c = []
1410 for v in vectors:
1411 if isinstance(v, Cluster):
1412 c.extend(v.flatten())
1413 elif isinstance(v, Document):
1414 c.append(v.vector)
1415 else:
1416 c.append(v)
1417 if not features:
1418 features = _features(c)
1419 c = [(f, mean((v.get(f, 0) for v in c), len(c))) for f in features]
1420 c = Vector((f, w) for f, w in c if w != 0)
1421 return c
1422
1423class DistanceMap(object):
1424

Callers 2

k_meansFunction · 0.85
hierarchicalFunction · 0.85

Calls 7

lenFunction · 0.85
meanFunction · 0.70
VectorClass · 0.70
extendMethod · 0.45
flattenMethod · 0.45
appendMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…