101 lines
1.7 KiB
Go
101 lines
1.7 KiB
Go
// pam 用Go来写PAM
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
//先设置一些参数
|
|
|
|
const (
|
|
dataN = 10
|
|
dim = 3
|
|
clusterN = 3
|
|
)
|
|
|
|
type Ctx struct {
|
|
data [][]float64
|
|
SetS map[int]interface{}
|
|
}
|
|
|
|
func initData() [][]float64 { //初始数据集
|
|
src := rand.NewSource(time.Now().UnixNano())
|
|
rnd := rand.New(src)
|
|
r := make([][]float64, dataN)
|
|
for n := 0; n < dataN; n++ {
|
|
r[n] = make([]float64, dim)
|
|
for d := 0; d < dim; d++ {
|
|
r[n][d] = rnd.Float64()
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func copyIntMap(src map[int]interface{}, dst map[int]interface{}) {
|
|
for k, v := range src {
|
|
dst[k] = v
|
|
}
|
|
}
|
|
|
|
func minDistance(vec []float64, mat [][]float64) float64 {
|
|
var r float64
|
|
r = 1e20
|
|
var t float64
|
|
for m := 0; m < len(mat); m++ {
|
|
t = 0
|
|
for v := 0; v < len(vec); v++ {
|
|
t += math.Pow(vec[v]-mat[m][v], 2)
|
|
}
|
|
t = math.Sqrt(t)
|
|
if t < r {
|
|
r = t
|
|
}
|
|
}
|
|
return r
|
|
}
|
|
|
|
func subData(set map[int]interface{}, ctx *Ctx) [][]float64 {
|
|
r := make([][]float64, len(set))
|
|
ind := 0
|
|
for k, _ := range set {
|
|
r[ind] = ctx.data[k]
|
|
ind++
|
|
}
|
|
}
|
|
|
|
func selectInitCluster(ctx *Ctx) {
|
|
for clusterI := 0; clusterI < clusterN-1; clusterI++ {
|
|
for cluster := 0; cluster < dataN; cluster++ {
|
|
if _, has := ctx.SetS[cluster]; has {
|
|
continue
|
|
}
|
|
//选一个候选数据
|
|
cddtI := cluster
|
|
cadSetS := make(map[int]interface{})
|
|
copyIntMap(cadSetS, ctx.SetS)
|
|
cadSetS[cddtI] = nil
|
|
Cij := 0.0
|
|
for J := 0; J < dataN; J++ {
|
|
if _, has := cadSetS[J]; has {
|
|
continue
|
|
}
|
|
d := ctx.data[J]
|
|
//寻找最短距离
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
ctx := Ctx{}
|
|
ctx.data = initData()
|
|
ctx.SetS = make(map[int]interface{})
|
|
ctx.SetS[1] = nil
|
|
fmt.Println("Hello World!")
|
|
}
|