You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

kmeans.go 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package object
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/casibase/casibase/util"
  20. "github.com/muesli/clusters"
  21. "github.com/muesli/kmeans"
  22. )
  23. func fa2Str(floatArray []float64) string {
  24. sData := []string{}
  25. for _, f := range floatArray {
  26. sData = append(sData, fmt.Sprintf("%f", f))
  27. }
  28. return strings.Join(sData, "|")
  29. }
  30. func runKmeans(factors []*Factor, clusterNumber int) {
  31. factorMap := map[string]*Factor{}
  32. var d clusters.Observations
  33. for _, factor := range factors {
  34. if len(factor.Data) == 0 {
  35. continue
  36. }
  37. dataKey := factor.GetDataKey()
  38. factorMap[dataKey] = factor
  39. d = append(d, clusters.Coordinates(factor.Data))
  40. }
  41. km := kmeans.New()
  42. cs, err := km.Partition(d, clusterNumber)
  43. if err != nil {
  44. panic(err)
  45. }
  46. for i, c := range cs {
  47. fmt.Printf("Centered at x: %.2f y: %.2f\n", c.Center[0], c.Center[1])
  48. fmt.Printf("Matching data points: %+v\n\n", c.Observations)
  49. color := util.GetRandomColor()
  50. for _, observation := range c.Observations {
  51. floatArray := observation.Coordinates()
  52. dataKey := fa2Str(floatArray)
  53. factor, ok := factorMap[dataKey]
  54. if !ok {
  55. panic(fmt.Errorf("factorMap factor not found, dataKey = %s", dataKey))
  56. }
  57. factor.Category = strconv.Itoa(i)
  58. factor.Color = color
  59. }
  60. }
  61. }
  62. func updateWordsetFactorCategories(owner string, wordsetName string) {
  63. wordset, _ := getWordset(owner, wordsetName)
  64. runKmeans(wordset.Factors, 100)
  65. UpdateWordset(wordset.GetId(), wordset)
  66. }