mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-11 15:16:54 +08:00
Improved k-means code [skip ci]
This commit is contained in:
@@ -148,23 +148,25 @@ QuickCenters(Relation index, VectorArray samples, VectorArray centers, IvfflatTy
|
|||||||
/* Fill remaining with random data */
|
/* Fill remaining with random data */
|
||||||
while (centers->length < centers->maxlen)
|
while (centers->length < centers->maxlen)
|
||||||
{
|
{
|
||||||
|
Datum center = PointerGetDatum(VectorArrayGet(centers, centers->length));
|
||||||
|
|
||||||
if (type == IVFFLAT_TYPE_VECTOR)
|
if (type == IVFFLAT_TYPE_VECTOR)
|
||||||
{
|
{
|
||||||
Vector *vec = (Vector *) VectorArrayGet(centers, centers->length);
|
Vector *vec = DatumGetVector(center);
|
||||||
|
|
||||||
SET_VARSIZE(vec, VECTOR_SIZE(dimensions));
|
SET_VARSIZE(vec, VECTOR_SIZE(dimensions));
|
||||||
vec->dim = dimensions;
|
vec->dim = dimensions;
|
||||||
|
|
||||||
for (int j = 0; j < dimensions; j++)
|
for (int j = 0; j < dimensions; j++)
|
||||||
vec->x[j] = RandomDouble();
|
vec->x[j] = RandomDouble();
|
||||||
|
|
||||||
/* Normalize if needed (only needed for random centers) */
|
|
||||||
if (normprocinfo != NULL)
|
|
||||||
ApplyNorm(normprocinfo, collation, PointerGetDatum(vec), type);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
elog(ERROR, "Unsupported type");
|
elog(ERROR, "Unsupported type");
|
||||||
|
|
||||||
|
/* Normalize if needed (only needed for random centers) */
|
||||||
|
if (normprocinfo != NULL)
|
||||||
|
ApplyNorm(normprocinfo, collation, center, type);
|
||||||
|
|
||||||
centers->length++;
|
centers->length++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -434,7 +436,8 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, IvfflatTyp
|
|||||||
|
|
||||||
for (int64 j = 0; j < numCenters; j++)
|
for (int64 j = 0; j < numCenters; j++)
|
||||||
{
|
{
|
||||||
Vector *vec = (Vector *) VectorArrayGet(newCenters, j);
|
Datum center = PointerGetDatum(VectorArrayGet(newCenters, j));
|
||||||
|
Vector *vec = DatumGetVector(center);
|
||||||
|
|
||||||
if (centerCounts[j] > 0)
|
if (centerCounts[j] > 0)
|
||||||
{
|
{
|
||||||
@@ -458,7 +461,7 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, IvfflatTyp
|
|||||||
|
|
||||||
/* Normalize if needed */
|
/* Normalize if needed */
|
||||||
if (normprocinfo != NULL)
|
if (normprocinfo != NULL)
|
||||||
ApplyNorm(normprocinfo, collation, PointerGetDatum(vec), type);
|
ApplyNorm(normprocinfo, collation, center, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Step 5 */
|
/* Step 5 */
|
||||||
|
|||||||
Reference in New Issue
Block a user