// ckmeans.1d.dp implementation using L2 dissimilarity and linear clustering. Panics
// if the weights array does not match the supplied data array.
ckmeans.1d.dp implementation using L2 dissimilarity and linear clustering. Panics
if the weights array does not match the supplied data array.
// ckmeans . 1d . dp implementation using L2 dissimilarity and linear clustering . Panics // if the weights array does not match the supplied data array .
func CKMeans1dDp(data, weights []float64) []Cluster {
	if data == nil || len(data) == 0 {
		return []Cluster{}
	}
	if weights != nil && len(weights) != len(data) {
		panic("Invalid weights")
	}
	x := make([]float64, len(data))
	w := make([]float64, len(data))
	order := make([]int, len(data))
	for i := range order {
		order[i] = i
	}
	sort.SliceStable(order, func(i, j int) bool { return data[order[i]] < data[order[j]] })
	if weights == nil {
		for i := range data {
			x[i] = data[order[i]]
			w[i] = 1.0
		}
	} else {
		for i := range data {
			x[i] = data[order[i]]
			w[i] = weights[order[i]]
		}
	}
	kmin := 1
	kmax := 1
	p := x[0]
	for _, q := range x[1:] {
		if q != p {
			kmax++
			q = p
		}
	}
	k, clusters, centers, variance := ckmeans(x, w, kmin, kmax)
	index := make([]int, len(x))
	for i := range clusters {
		index[order[i]] = clusters[i]
	}
	clustered := make([]Cluster, k)
	for i := 0; i < k; i++ {
		clustered[i].Center = centers[i]
		clustered[i].Variance = variance[i]
	}
	for i, ix := range index {
		clustered[ix].Values = append(clustered[ix].Values, data[i])
	}
	return clustered
}
func CKMeans1dDp ( data , weights [ ] float64 ) [ ] Cluster { if data == nil || len ( data ) == 0 { return [ ] Cluster { } } if weights != nil && len ( weights ) != len ( data ) { panic ( " " ) } x := make ( [ ] float64 , len ( data ) ) w := make ( [ ] float64 , len ( data ) ) order := make ( [ ] int , len ( data ) ) for i := range order { order [ i ] = i } sort . SliceStable ( order , func ( i , j int ) bool { return data [ order [ i ] ] < data [ order [ j ] ] } ) if weights == nil { for i := range data { x [ i ] = data [ order [ i ] ] w [ i ] = 1.0 } } else { for i := range data { x [ i ] = data [ order [ i ] ] w [ i ] = weights [ order [ i ] ] } } kmin := 1 kmax := 1 p := x [ 0 ] for _ , q := range x [ 1 : ] { if q != p { kmax ++ q = p } } k , clusters , centers , variance := ckmeans ( x , w , kmin , kmax ) index := make ( [ ] int , len ( x ) ) for i := range clusters { index [ order [ i ] ] = clusters [ i ] } clustered := make ( [ ] Cluster , k ) for i := 0 ; i < k ; i ++ { clustered [ i ] . Center = centers [ i ] clustered [ i ] . Variance = variance [ i ] } for i , ix := range index { clustered [ ix ] . Values = append ( clustered [ ix ] . Values , data [ i ] ) } return clustered }
ckmeans.1d.dp implementation using L2 dissimilarity and linear clustering.
ckmeans . 1d . dp implementation using L2 dissimilarity and linear clustering .
// validate inputs // sort and order data // calculate range of K // TODO: should this include weights??