github.com/schollz/clusters@v0.0.0-20221201012527-c6c68863636f/kmeans_estimator_test.go (about)

     1  package clusters
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestKmeansEstimator(t *testing.T) {
     8  	const (
     9  		C = 10
    10  		E = 1
    11  	)
    12  
    13  	var (
    14  		f = "data/bus-stops.csv"
    15  		i = CsvImporter()
    16  	)
    17  
    18  	d, e := i.Import(f, 4, 5)
    19  	if e != nil {
    20  		t.Errorf("Error importing data: %s\n", e.Error())
    21  	}
    22  
    23  	c, e := KMeansEstimator(1000, C, EuclideanDistance)
    24  	if e != nil {
    25  		t.Errorf("Error initializing kmeans clusterer: %s\n", e.Error())
    26  	}
    27  
    28  	r, e := c.Estimate(d)
    29  	if e != nil {
    30  		t.Errorf("Error running test: %s\n", e.Error())
    31  	}
    32  
    33  	if r != E {
    34  		t.Errorf("Estimated number of clusters should be %d, it s %d\n", E, r)
    35  	}
    36  }