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=[])
| 1400 | return sum(iterable) / float(length or 1) |
| 1401 | |
| 1402 | def 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 | |
| 1423 | class DistanceMap(object): |
| 1424 |