GoLearn is a machine learning library for the Go (Golang) programming language , designed to make it easy to build, train, and evaluate machine learning models. It’s one of the most popular ML libraries in the Go ecosystem and provides functionality similar to Python’s scikit-learn
, but tailored for Go developers.
📦 What is GoLearn?
GoLearn is an open-source project hosted on GitHub that aims to provide clean, idiomatic Go implementations of common machine learning algorithms and utilities. It supports both supervised and unsupervised learning techniques and includes tools for data preprocessing, model evaluation, and feature engineering.
🔗 Project Info
- GitHub : https://github.com/sajari/golearn
- License : MIT License
- Maintainer : Sajari (an AI/ML company)
🧠 Key Features
Feature | Description |
---|---|
Supervised Learning | Includes classifiers and regressors like Decision Trees, KNN, Linear Regression, Naive Bayes |
Unsupervised Learning | Clustering with KMeans |
Data Handling | Built-in DataFrame-like structure (base.DataFrame ) |
Model Evaluation | Metrics such as accuracy, precision, recall, F1-score, confusion matrix |
Cross-Validation | Support for k-fold cross-validation |
Feature Engineering | Normalization, standardization, encoding categorical variables |
Easy-to-use API | Similar to scikit-learn (fit/predict pattern) |
Pure Go | No external dependencies or bindings |
🛠️ Installation
To install GoLearn, use go get
:
go get github.com/sajari/golearn
Then import into your Go code:
go
⌄
import (
“github.com/sajari/golearn/base”
“github.com/sajari/golearn/evaluation”
“github.com/sajari/golearn/knn”
)
📊 Example: KNN Classifier with GoLearn
Here’s a simple example using the Iris dataset and a K-Nearest Neighbors classifier:
go
packagemain
import (
“fmt”
“github.com/sajari/golearn/base”
“github.com/sajari/golearn/knn”
“github.com/sajari/golearn/evaluation”
)
funcmain() {
// Load dataset
rawData, err := base.ParseCSVToInstances(“iris.csv”, true)
if err != nil {
panic(err)
}
// Create a KNN classifier
cls := knn.NewKnnClassifier(“euclidean”, “linear”, 2)
// Train-test split
trainData, testData := base.InstancesTrainTestSplit(rawData, 0.75)
// Train the model
cls.Fit(trainData)
// Make predictions
predictions := cls.Predict(testData)
// Evaluate performance
cm := evaluation.GetConfusionMatrix(testData, predictions)
fmt.Println(evaluation.GetSummary(cm))
}
Note: The Iris dataset should be in CSV format with headers, like:
sepal_length,sepal_width,petal_length,petal_width,class
5.1,3.5,1.4,0.2,Iris-setosa
📚 Supported Algorithms
✅ Supervised Learning
Algorithm | Type | Package |
---|---|---|
K-Nearest Neighbors (KNN) | Classification & Regression | github.com/sajari/golearn/knn |
Linear Regression | Regression | github.com/sajari/golearn/linear_regression |
Logistic Regression | Classification | github.com/sajari/golearn/logistic_regression |
Decision Tree | Classification | github.com/sajari/golearn/trees |
Naive Bayes | Classification | github.com/sajari/golearn/naive_bayes |
Perceptron | Classification | github.com/sajari/golearn/perceptron |
✅ Unsupervised Learning
Algorithm | Type | Package |
---|---|---|
K-Means Clustering | Clustering | github.com/sajari/golearn/clustering/kmeans |
📈 Model Evaluation Metrics
GoLearn supports a variety of metrics for evaluating model performance:
Metric | Use Case |
---|---|
Accuracy | Classification |
Precision / Recall / F1-Score | Classification |
Confusion Matrix | Classification |
Mean Squared Error (MSE) | Regression |
R² Score | Regression |
📁 Data Handling
GoLearn uses its own DataFrame
-like structure called Instances
:
- Supports loading from CSV (
ParseCSVToInstances
) - Handles both numeric and categorical features
- Can be manipulated via functions like
base.RemoveClassAttribute
,base.EnhanceAttributes
, etc.
🧪 Cross Validation
Use k-fold cross-validation to evaluate models more robustly:
go
cv := validation.NewKFoldCV(cls, 5)
err = cv.Run(rawData)
if err != nil {
panic(err)
}
fmt.Println(cv.GetResults())
🧩 Feature Engineering
GoLearn offers basic feature transformations:
- Normalization : Scale features between 0 and 1
- Standardization : Normalize with mean and standard deviation
- Categorical Encoding : Convert string labels to numerical values
Example:
normaliser := transforms.NewNormaliser()
normalizedData := normaliser.Transform(data)
📌 Limitations
While GoLearn is powerful for Go-based ML, it has some limitations compared to Python-based ecosystems:
Limitation | Description |
---|---|
Limited Algorithms | Fewer models than scikit-learn or TensorFlow |
Less Community Activity | Smaller community and slower updates |
No Deep Learning Support | Not suitable for neural networks or deep learning |
Lower Performance | For large datasets, not as fast as C++ or Rust-based ML |
🧠 Alternatives to GoLearn
If GoLearn doesn’t meet your needs, consider these alternatives:
Library | Description |
---|---|
Gorgonia | GPU-accelerated deep learning framework (similar to TensorFlow) |
GoML | Lightweight ML library with perceptron and linear regression |
Evo | Genetic algorithm library |
Cloud APIs | Use Google AutoML, AWS SageMaker, or Azure ML from Go via SDKs |
📚 Resources & Documentation
- Official Docs : https://pkg.go.dev/github.com/sajari/golearn
- GitHub Repo : https://github.com/sajari/golearn
- Examples : Included in repo under
/examples
- Blog Posts / Tutorials :
- [GoLearn Tutorial by Adrian Mouat](https://medium.com/ @adrianmouat/machine-learning-in-go-with-golearn-cd6b4c9a351f)
- Getting Started with GoLearn
✅ Summary Checklist
Task | Status |
---|---|
Install GoLearn | ✅ |
Load Dataset (CSV) | ✅ |
Train a Model (e.g., KNN) | ✅ |
Make Predictions | ✅ |
Evaluate Model (Accuracy, F1, etc.) | ✅ |
Apply Cross-Validation | ✅ |
Preprocess Data (Normalize, Encode) | ✅ |
Export or Save Model | ❌ (Limited support) |