Пример #1
0
func NewKMeans(X Matrix, metric MetricOp) *KMeans {
	// initialize index to complete index iterating over all elements of X, unless defined otherwise
	return &KMeans{
		X:      X,
		Metric: metric,
		Index:  mlgo.Range(0, len(X)),
	}
}
Пример #2
0
func NewDistances(X Matrix, metric MetricOp) (d *Distances) {
	// each row of X is considered one data point
	m := len(X)

	// allocate space
	D := make(Matrix, m)
	for i := 0; i < m; i++ {
		D[i] = make(Vector, m)
	}

	// calculate distances for lower and upper triangles together
	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			d := metric(X[i], X[j])
			D[i][j], D[j][i] = d, d
		}
	}

	d = &Distances{rep: D, metric: metric, index: mlgo.Range(0, m)}

	return
}