lda

package module
v0.0.0-...-98f5c4b Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

Linear Discriminant Analysis in Go

Linear Discriminant Analysis is a powerful and commonly used dimensionality reduction and classification technique used in statistics and machine learning. It extracts components from an input dataset in a way that maximizes class separability while minimizing the variance within each class. A key assumption here is that the dataset needs to be normally distributed. In the world of machine learning, LDA can be used as a classifier algorithm, which this library also provides.

Getting Started

Import "github.com/RadiusNetworks/lda"

Dependencies

math sort gonum.org/v1/gonum/mat

Usage

The library contains a predefined struct LD that can be used to access LDA methods.
Important note about input data: LDA is a supervised learning technique so all training data needs to be labeled.

First step

The first step would be to make a variable of type LD and call LinearDiscriminant by passing in a matrix of input data and an array of labels that correspond to the data. If that call is successful, then you can use other LDA methods, such as Transform and Predict.

Example: Iris dataset

// Load your data
// See lda_test.go for an example of loading and pre-processing data

// Create a matrix and fill it with iris data
dataMatrix := mat.NewDense(numberOfRows, numberofColumns, yourDataset)

// Create an array ([]int) of labels for your dataset
var labels []int
for yourLabel := range labelsFromYourDataset {
	labels = append(labels, yourLabel)
}

// Instantiate an LD object and call LinearDiscriminant to fit the model and check if input data follows preconditions in the process
var ld LDA.LD
err := ld.LinearDiscriminant(dataMatrix, labels)

if err == nil {
  // If the call is successful, you can now use other methods
  numDimensions := 2 // number of dimensions to reduce to
  result := ld.Transform(dataMatrix, numDimensions)
  
  // We can graph the result of the transformation on an XY plane
  PlotLDA(result, labels, "LDA Plot.png")
  
  // We can use the result of the transformation to classify test data
  // *See section on method Predict below*
  
} else {
  // handle error
}

...

Using LDA as a classifier with Predict

Method Predict takes in a set of data and returns a number (Int), a prediction for what class the set of data would be in. Below is an example of a test that checks if Predict is classifying data correctly.
Example: Iris test data
(See lda_test.go for the complete implementation of this example)

// Create test cases with test data and corresponding labels (classes that you expect the data points to be in)
// Call LinearDiscriminant with the test data and the labels as arguments
// Call Transform
result := ld.Transform(test.data, numDims)
r, _ := test.testPredict.Dims()
for k := 0; k < r; k++ {
	c, _ := ld.Predict(test.testPredict.RawRowView(k))
	if c != test.wantClass[k] {
		t.Errorf("unexpected prediction result %v got:%v, want:%v", k, c, test.wantClass[k])
	}
}
values := make([]string, ld.p)
for j := 0; j < ld.n; j++ {
	row := result.RawRowView(j)
	for k := 0; k < numDims; k++ {
		values[k] = fmt.Sprintf("%.4f", row[k])
	}
      }
    }
}

Tests

We provide a sample test file that tests both the dimensionality reduction and the classification features of the algorithm. The test uses the famous Iris dataset, which can be found here: https://archive.ics.uci.edu/ml/datasets/Iris

Credits and Acknowledgements

The implementation of the LDA algorithm is based on a Java version provided by https://github.com/haifengl/smile
Additional resources used: https://sebastianraschka.com/Articles/2014_python_lda.html
Created by Andrei Kozyrev (@akozyrev) with the help of Tim Judkins (@b0tn3rd) and Eleanor Nuechterlein (@Eleanor2). Open sourced by Radius Networks.

Contributing and License

LDA-go is Apache-2.0 licensed. Contributions are welcome.

Documentation

Overview

Package lda provides methods for calculating linear discriminant analysis (LDA). LDA can be used as a dimensionality reduction technique and as a classifier. Both capabilities are often used in the realm of machine learning and statistical modeling. This package provides a prediction method that can classify input data based on previous calculations and feature extraction from training data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LD

type LD struct {
	// contains filtered or unexported fields
}

LD is a type for computing and extracting the linear discriminant analysis of a matrix. The results of the linear discriminant analysis are only valid if the call to LinearDiscriminant was successful.

func (*LD) GetEigen

func (ld *LD) GetEigen() mat.Eigen

GetEigen is a getter method for eigen values

No parameters. Returns a mat.Eigen object

func (*LD) LinearDiscriminant

func (ld *LD) LinearDiscriminant(x mat.Matrix, y []int) (err error)

LinearDiscriminant performs linear discriminant analysis on the matrix of the input data, which is represented as an n×p matrix x, where each row is an observation and each column is a variable.

Parameter x is a matrix of input/training data. Parameter y is an array of input/training labels in [0,k) where k is the number of classes. Returns true iff the analysis was successful.

func (*LD) Predict

func (ld *LD) Predict(x []float64) (int, error)

Predict performs a prediction based on training data to assess which class a certain set of data would be in.

Parameter x is the set of data to classify. Returns a prediction for what class the set of data would be in.

Additional details: LDA can be used as a supervised learning algorithm to predict and classify data based on features extracted from training data. LDA reduces dimensionality of the data and performs feature extraction to maximize separation between classes. Precondition: training data must be labeled and labels must be ints starting from 0.

func (*LD) Transform

func (ld *LD) Transform(x mat.Matrix, n int) *mat.Dense

Transform performs a transformation on the matrix of the input data, which is represented as an ld.n × p matrix x

Parameter x is the matrix to be transformed. Parameter n is the number of dimensions desired. Returns the transformed matrix.

Jump to

Keyboard shortcuts

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