Native Go machine learning: low-latency inference, zero dependencies, scikit-learn-style API.
GopherML is a machine learning library written entirely in Go, designed to provide ultra-fast training and inference tools using native concurrency (goroutines) and without Cgo or external dependencies. It follows Pythonβs scikit-learn architecture and API, adapted for cryptocurrency bots and high-frequency inference.
- Features
- Installation
- Getting started
- Packages
- Tests and CI
- Repository structure
- Documentation
- Examples
- Subproject: go-nlp
- Roadmap
- Design metrics
- Contributing and license
- β‘ Low-latency inference:
PredictSingleandPredictProbaSingleare optimized for bot hot paths (zero extra allocations per tick). - π§© scikit-learn-style API:
NewModel()βFit(X, y)βPredict(X)βScore(X, y). If you already use scikit-learn, you'll feel at home. - π Native concurrency: parallel training of Random Forest,
GridSearchCVand linear algebra withgoroutinesandsync.WaitGroupβ without Cgo. - π‘οΈ Strict validation: All input data is validated against
NaN/Inf(utils.CheckFinite) before touching a model. It never fails in silence. - πΎ Persistence with integrity:
Save/Loadingobformat, with automatic post-load validation via thepersistence.Validatorinterface. - π Complete metrics: Accuracy, Precision, Recall, F1, RΒ², MAE, MSE, RMSE, and ROC-AUC.
- πͺ Crypto pipeline: the
crypto/package encapsulates the complete flow OHLCV β features β trading signal (Hold/Long/Short). - π« Zero dependencies: only the Go standard library. No NumPy, no CGO, no bindings.
Requirements: Go 1.23 or higher.
go get github.com/ateto3/gopherml@latestAdd a replace in your project's go.mod:
module github.com/your-user/your-bot
go 1.23
require github.com/ateto3/gopherml v0.0.0
replace github.com/ateto3/gopherml => /path/to/GopherMLThen run:
go mod tidyComplete binary classification with Random Forest:
package main
import (
"fmt"
"log"
"github.com/ateto3/gopherml/ensemble"
"github.com/ateto3/gopherml/preprocessing"
)
func main() {
// Data: [length, width] β label 0 or 1
X := [][]float64{
{1.0, 2.0}, {1.5, 1.8}, {1.2, 2.5}, {1.1, 1.9},
{5.0, 6.0}, {5.5, 5.8}, {5.2, 6.5}, {4.9, 5.9},
}
y := []float64{0, 0, 0, 0, 1, 1, 1, 1}
// 1. Scale the data (essential for linear models)
scaler := preprocessing.NewStandardScaler()
Xs, err := scaler.FitTransform(X)
if err != nil {
log.Fatal(err)
}
// 2. Train
rf := ensemble.NewRandomForestClassifier()
rf.NEstimators = 100
rf.MaxDepth = 5
rf.RandomState = 42
if err := rf.Fit(Xs, y); err != nil {
log.Fatal(err)
}
// 3. Predict (batch)
preds, err := rf.Predict(Xs)
if err != nil {
log.Fatal(err)
}
fmt.Println("Predictions:", preds) // [0 0 0 0 1 1 1 1]
// 4. Evaluate
acc, err := rf.Score(Xs, y)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Accuracy: %.2f%%\n", acc*100)
// 5. Hot path: single sample, no batch overhead
label, err := rf.PredictSingle([]float64{5.1, 6.1})
if err != nil {
log.Fatal(err)
}
fmt.Println("Single sample:", label)
}Full pipeline for crypto bots (OHLCV β signal):
cfg := crypto.DefaultPredictorConfig()
cfg.WindowSize = 20
cfg.MinConfidence = 0.65
p := crypto.NewCryptoPredictor(cfg)
if err := p.LoadModel("btcusdt_1h_rf.gob"); err != nil {
log.Fatal(err)
}
signal, confidence, err := p.PredictSignal(lastCandles) // len(lastCandles) == WindowSize
if err == nil && confidence >= cfg.MinConfidence {
// signal: crypto.SignalHold | crypto.SignalLong | crypto.SignalShort
_ = signal
}See USAGE.md for the full guide with trading examples.
| Package | Description |
|---|---|
/crypto |
Crypto pipeline: OHLCV β feature engineering β trading signal (CryptoPredictor) |
/linear_model |
LinearRegression, Ridge, Lasso, ElasticNet, LogisticRegression, SGDClassifier + parallel versions |
/tree |
DecisionTreeClassifier and DecisionTreeRegressor (CART; Gini, Entropy, and MSE criteria) |
/ensemble |
RandomForestClassifier and RandomForestRegressor (parallel training with bootstrap) |
/metrics |
Accuracy, Precision, Recall, F1, RΒ², MAE, MSE, RMSE, ROC-AUC, reports, and confusion matrix |
/model_selection |
KFold, CrossValScore, and parallel GridSearchCV |
/preprocessing |
StandardScaler, MinMaxScaler, LabelEncoder, PolynomialFeatures |
/persistence |
Model Save/Load with post-load integrity validation |
/utils |
Data validation (CheckXy, CheckFinite), TrainTestSplit, LoadCSV, deterministic RNG |
/examples |
Executable end-to-end examples |
# Build everything
go build ./...
# Analyze code (must be clean)
go vet ./...
# Tests with race detector
go test ./... -race
# Format (recommended before a PR)
gofmt -w .Current status: all packages in green and clean go vet. Each push runs the CI workflow (go vet + go test -race).
GopherML/
βββ README.md # This file
βββ USAGE.md # Usage guide with crypto bot examples
βββ EXPLAINING.md # Architecture and internal logic
βββ CONTRIBUTING.md # How to contribute (includes AI guidelines)
βββ CHANGELOG.md # Change history
βββ docs/ # Extended documentation (guides, API, FAQ)
βββ crypto/ # OHLCV β features β signal pipeline
βββ ensemble/ # Random Forest
βββ linear_model/ # Linear models and SGD
βββ tree/ # CART decision trees
βββ metrics/ # Evaluation metrics
βββ model_selection/ # Cross-validation and grid search
βββ preprocessing/ # Scaling and encoding
βββ persistence/ # Save/load models (gob)
βββ utils/ # Data validation and utilities
βββ examples/ # Executable examples
βββ go-nlp/ # Submodule: concurrent NLP (NLTK port)
βββ .github/ # Issue/PR templates and CI
| Document | Content |
|---|---|
| docs/GETTING_STARTED.md | Step-by-step tutorial from scratch |
| USAGE.md | Usage guide with crypto bot examples |
| docs/API_REFERENCE.md | API reference for all packages |
| EXPLAINING.md | Architecture and internal logic |
| docs/ARCHITECTURE.md | Detailed architecture and design decisions |
| docs/PERFORMANCE.md | Performance guide and optimizations |
| docs/FAQ.md | Frequently asked questions |
| CONTRIBUTING.md | Contribution guide and code standards |
| CHANGELOG.md | Change history |
Full index in docs/README.md.
All examples are executable programs (package main):
# Classification with Random Forest + metrics + cross-validation
go run examples/classification/basic_classification.go
# Regression: LinearRegression vs RandomForestRegressor
go run examples/regression/basic_regression.go
# Crypto pipeline: synthetic OHLCV β signal β persistence
go run examples/crypto/crypto_trading_bot.go
# Save and load a trained model
go run examples/persistence/model_persistence.go
# Parallel hyperparameter search with GridSearchCV
go run examples/grid_search/hyperparameter_tuning.go
# Preprocessing pipeline with scalers and encoder
go run examples/preprocessing/preprocessing_pipeline.goDetails in examples/README.md.
The go-nlp/ directory contains a ** standalone Go module ** (github.com/ateto3/gopherml/go-nlp): a concurrent NLTK port with tokenization, POS tagging, parsing, text classification and sentiment analysis, plus token optimization for LLM APIs.
β οΈ go-nlphas its owngo.mod: it is independently compiled and tested withcd go-nlp && go build ./... && go test ./...
See go-nlp/README.md for full documentation.
- Basic ML models (linear regression, classification, trees, forests, ensembles)
- Model persistence (Save/Load with integrity validation)
- Evaluation metrics (accuracy, precision, recall, F1, RΒ², MAE, RMSE)
- Model selection and cross-validation (KFold, GridSearchCV)
- Data preprocessing (StandardScaler, MinMaxScaler, LabelEncoder)
- NaN/Inf validation on all input data
-
PredictProba+PredictSinglefor high-frequency bots -
crypto/package β OHLCV β features β trading signal pipeline - GradientBoosting (XGBoost-like)
- Streaming data support (online updates)
| Objective | Specification |
|---|---|
| Language | Go 1.23+ |
| Module | github.com/ateto3/gopherml |
| Target | Low latency inference/crypto bots |
| Hardware | Optimized for ARM64 (Raspberry Pi 4) and x86_64 |
| Dependencies | Zero (Go stdlib only) |
| Tests | go test ./... -race β 100% green tests |
Contributions are welcome! Read CONTRIBUTING.md for workflow, code standards, and performance guidelines (including guidelines for AI assistants). Before sending a PR, make sure they pass:
go vet ./... && go test ./... -raceThis project is licensed under mit β see LICENSE.
The code must be modular so that it can be easily imported into other Go binaries or automation microservices.
"The code must be so fast that Go's garbage collector doesn't even know we're here."