Explore GoLearn Classification algorithms
masterGoLearn provides several classification implementations, including:
- KNN (K-Nearest Neighbors)
- Trees (Decision Trees)
- liblinear
For regression analysis, refer to the Regression.md documentation.
repository·master·Indexed 27 days ago
https://github.com/sjwhitworth/golearnA '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.
GoLearn provides several classification implementations, including:
For regression analysis, refer to the Regression.md documentation.
GoLearn includes tools for data preprocessing and manipulation:
Instances.md to learn how to read data into the library.Filtering.md for operations like merging histograms or merging discrete data.AddingAttributes.md), retrieve attribute values (AttributeSpecifications.md), and set FloatAttribute precision (FloatAttributePrecision.md).CSVFiles.md guide.TrainTestSplit.md for implementation details.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.
To ensure GoLearn and other Go tools work correctly, verify your GOPATH and PATH settings.
echo $GOROOT and echo $GOPATH in your terminal.go folder must exist in your home directory and be writable. If it doesn't exist, create it with:cd && mkdir go$GOPATH should include $GOROOT and a bin/ folder. For example, if $GOROOT is /home/sen/go, then $GOPATH should be configured accordingly.export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/binexport GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/binGoLearn 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:
base.ParseCSVToInstances to load datasets.base.InstancesTrainTestSplit to create training and testing sets..Fit(trainData) on an estimator..Predict(testData) to get predictions.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))
}GoLearn requires Go 1.4 or higher. Most of the library uses the Go standard library, but some components depend on C.
g++ in your terminal).go get -t -u -v github.com/sjwhitworth/golearncd $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 ./...You can explore practical implementations by running the provided examples located in the $GOPATH/src/github.com/sjwhitworth/golearn/examples/ directory.
Available example directories:
knnclassifierinstancestreescd $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.goGoLearn supports the following environments:
| Operating System | Go Version |
|---|---|
| Mac OS X 10.8 | 1.2+ |
| Ubuntu 14.04 | 1.2+ |
| OpenSUSE 13.1 | 1.2+ |
Note: For Mac OS X users, installing BLAS via Homebrew has not been confirmed to work reliably.