cluster

package
v0.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 11 Imported by: 0

README

Go-ML cluster

Documentation

Package cluster is a machine learning clustering package for Go.

Example Usage

Below contains example usage of the cluster package on the Iris dataset and a makeMoons sample dataset. These examples can be found in ../tests/cluster_test.go.

Principal Component Analysis on Iris
    // Test PCA and check for errors
	result, err := cluster.PCA(data, 2)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at building PCA -> "%s"`, err)
	}

	// Generate scatterplot for PCA results
	plot_params := []string{"../tests/misc/pcaTest.png", "PCA Test", "PCA 1", "PCA 2"}
	err = cluster.ScatterPlot2DimenData(result, label, plot_params)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at pca plotting -> "%s"`, err)
	}
Plot

Principal Component Analysis Example

K-Means Clustering on Iris
    // Test KMeans and check for errors
	km := cluster.NewKMeans(3, 500)
	err = km.Train(result)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at kmeans training -> "%s"`, err)
	}
	result, new_label, err := km.Evaluate(result)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at kmeans evaluation -> "%s"`, err)
	}

	// Generate scatterplot for KMeans results
	plot_params = []string{"../tests/misc/kmeansIrisTest.png", "KMeans Test", "X", "Y"}
	err = cluster.ScatterPlot2DimenData(result, new_label, plot_params)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at kmeans plotting -> "%s"`, err)
	}
Plot

K-Means Clustering on Iris

Spectral Clustering on MakeMoons
    // Test Spectral Clustering and check for errors
	new_label, err = cluster.Spectral(data, 0.4)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at spectral clustering -> "%s"`, err)
	}

	// Generate scatterplot for Spectral Clustering results
	plot_params = []string{"../tests/misc/spectralMoonsTest.png", "Spectral Clustering Test", "X", "Y"}
	err = cluster.ScatterPlot2DimenData(data, new_label, plot_params)
	if err != nil {
		t.Fatalf(`TestCluster(): failed at spectral plotting -> "%s"`, err)
	}
Plot

Spectral Clustering on MakeMoons

Documentation

Overview

The cluster package contains code for various clustering algorithms and other related functions needed to run them.

As of now, the list of functionality supported is Principal Component Analysis, KMeans clustering, Spectral Clustering, and generating scatterplots for labeled data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Covariance

func Covariance(data [][]float64, rowvar bool) ([][]float64, error)

Covariance returns the covariance matrix of data. If `rowvar` is true, the data is treated with the variables being arranged via rows. It returns an error if data dimensions are inconsistent/empty.

func EigenSym

func EigenSym(data [][]float64) ([]float64, [][]float64, error)

EigenSym returns the eigenvalues and eigenvectors of a real symmetric matrix. It returns an error if data dimensions are inconsistent/empty or if it fails to factorize

func Euclidean

func Euclidean(point []float64, data [][]float64) []float64

Euclidean returns all the euclidean distances from point to each coordinate in data

func PCA

func PCA(data [][]float64, n int) ([][]float64, error)

PCA performs principal component analysis on `data` and reduces dimensionality to `n` dimensions. It assumes that the variable data is arranged via columns. It returns the reduced data. It returns an error if data dimensions are inconsistent/empty or if there are more variables than n.

func ScatterPlot2DimenData

func ScatterPlot2DimenData(data [][]float64, label []string, params []string) error

ScatterPlot2DimenData generates a scatterplot using Go's plotter package. `Data` provides the points on the plot, `label` contains the label for each point, `params` consist of the plot's parameters such as output directory, plot title, and axis names. It returns an error if any string params are provided incorrectly.

func Spectral

func Spectral(data [][]float64, radius float64) ([]string, error)

Spectral performs spectral clustering on `data` and returns the classified labels for each point. Spectral clustering partitions the data into 2 clusters. `radius` determines the connectivity of each point. It returns an error if eigenvectors fail to be generated from the Laplacian matrix.

Types

type KMeans

type KMeans struct {
	CENTROIDS [][]float64
	// contains filtered or unexported fields
}

KMeans represents a classification model utilizing K-Means clustering

func NewKMeans

func NewKMeans(n, max_eps int) *KMeans

NewKMeans creates a new K-Means model with `n` clusters. `Max_eps` represents the maximum amount of episodes or iterations the model will train on until convergence.

func (*KMeans) Evaluate

func (model *KMeans) Evaluate(data [][]float64) ([][]float64, []string, error)

Evaluate takes in `data` and classifies it according to the trained K-Means model. It returns the data along with the centroid points with its correlated labels. An error is returned if the model was not yet trained.

func (*KMeans) Train

func (model *KMeans) Train(data [][]float64) error

Train performs the K-Means clustering algorithm on `data` and generates a classification model. An error is returned if dimensions are empty or not consistent. As well, it returns an error if centroid calculations fail at any point.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL