GoLearn

repository·master·Indexed 27 days ago

https://github.com/sjwhitworth/golearn

A 'batteries included' machine learning library for Go designed for simplicity and customisability. It provides an Instances structure for data representation and a Fit/Predict interface similar to scikit-learn. Features include tools for data loading via CSV, training/test splitting, and evaluation using confusion matrices. Supported algorithms include KNN (with euclidean, manhattan, and cosine metrics), Decision Trees, and liblinear.

Tokens
1.8K
Snippets
4
Records
13
Agent score
94%

What's inside golearn

  1. Explore GoLearn Data Manipulation and Filtering

    master

    GoLearn includes tools for data preprocessing and manipulation:

    • Loading Data: Use Instances.md to learn how to read data into the library.
    • Filtering: Use Filtering.md for operations like merging histograms or merging discrete data.
    • Attribute Management: Learn how to add features (AddingAttributes.md), retrieve attribute values (AttributeSpecifications.md), and set FloatAttribute precision (FloatAttributePrecision.md).
  2. Use KNNClassifier for classification

    master

    The KNNClassifier (K-Nearest Neighbours) is a classification method that determines the class of an unknown instance based on the K nearest training instances.

    For a complete implementation example, refer to the Iris dataset classification example: examples/knnclassifier/knnclassifier_iris.go.

  3. Configure Go environment variables

    master

    To ensure GoLearn and other Go tools work correctly, verify your GOPATH and PATH settings.

    1. Verify current settings: Run echo $GOROOT and echo $GOPATH in your terminal.
    2. Directory Requirements: Your go folder must exist in your home directory and be writable. If it doesn't exist, create it with:
      cd && mkdir go
    3. Variable Configuration: $GOPATH should include $GOROOT and a bin/ folder. For example, if $GOROOT is /home/sen/go, then $GOPATH should be configured accordingly.
    4. Persistence: Add the following to your Bash configuration file to ensure variables are set correctly:
      export GOROOT=$HOME/go
      export PATH=$PATH:$GOROOT/bin
    export GOROOT=$HOME/go
    export PATH=$PATH:$GOROOT/bin
  4. Get started with GoLearn machine learning workflows

    master

    GoLearn uses an Instances structure to represent data (similar to a Data Frame in R or Pandas). It follows a Fit/Predict interface similar to scikit-learn, allowing you to easily swap estimators.

    Key workflow steps:

    1. Load Data: Use base.ParseCSVToInstances to load datasets.
    2. Split Data: Use base.InstancesTrainTestSplit to create training and testing sets.
    3. Train: Call .Fit(trainData) on an estimator.
    4. Predict: Call .Predict(testData) to get predictions.
    5. Evaluate: Use the evaluation package to generate confusion matrices and summaries.
    package main
    
    import (
    	"fmt"
    
    	"github.com/sjwhitworth/golearn/base"
    	"github.com/sjwhitworth/golearn/evaluation"
    	"github.com/sjwhitworth/golearn/knn"
    )
    
    func main() {
    	// Load in a dataset, with headers.
    	rawData, err := base.ParseCSVToInstances("datasets/iris.csv", true)
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Println(rawData)
    
    	// Initialises a new KNN classifier
    	cls := knn.NewKnnClassifier("euclidean", "linear", 2)
    
    	// Do a training-test split
    	trainData, testData := base.InstancesTrainTestSplit(rawData, 0.50)
    	cls.Fit(trainData)
    
    	// Calculates the Euclidean distance and returns the most popular label
    	predictions, err := cls.Predict(testData)
    	if err != nil {
    		panic(err)
    	}
    
    	// Prints precision/recall metrics
    	confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
    	if err != nil {
    		panic(fmt.Sprintf("Unable to get confusion matrix: %s", err.Error()))
    	}
    	fmt.Println(evaluation.GetSummary(confusionMat))
    }
  5. Install GoLearn

    master

    GoLearn requires Go 1.4 or higher. Most of the library uses the Go standard library, but some components depend on C.

    System Dependencies

    1. C Compiler: Ensure a compatible compiler is installed (verify by running g++ in your terminal).
    2. BLAS Library: GoLearn uses Gonum BLAS, which requires OpenBLAS or a similar library to be installed on your system. Follow the Gonum BLAS installation instructions for your specific operating system.

    Installation Steps

    1. Fetch the GoLearn repository:
      go get -t -u -v github.com/sjwhitworth/golearn
    2. Navigate to the source directory and install internal dependencies:
      cd $GOPATH/src/github.com/sjwhitworth/golearn
      go get -t -u -v ./...
    go get -t -u -v github.com/sjwhitworth/golearn
    cd $GOPATH/src/github.com/sjwhitworth/golearn
    go get -t -u -v ./...
  6. Run GoLearn built-in examples

    master

    You can explore practical implementations by running the provided examples located in the $GOPATH/src/github.com/sjwhitworth/golearn/examples/ directory.

    Available example directories:

    • knnclassifier
    • instances
    • trees
    cd $GOPATH/src/github.com/sjwhitworth/golearn/examples/knnclassifier
    go run knnclassifier_iris.go
    
    cd $GOPATH/src/github.com/sjwhitworth/golearn/examples/instances
    go run instances.go
    
    cd $GOPATH/src/github.com/sjwhitworth/golearn/examples/trees
    go run trees.go