Skip to content

error while trying to fit the Gaussian Process Regressor #22

@AlishSem

Description

@AlishSem

Hello!
I am really new to this topic and golang in general, so sorry if this is too obvious

But i ran into the problem

I am trying to run the simple GP with the help of YouTube and ChatGPT, but my below code fails

package main

import (
	"fmt"
	"github.com/pa-m/sklearn/gaussian_process"
	"github.com/pa-m/sklearn/gaussian_process/kernels"
	"gonum.org/v1/gonum/mat"
	"log"
	"math/rand"
)

func main() {
	// Generate some sample data
	X := mat.NewDense(10, 1, nil)
	y := make([]float64, 10)
	for i := 0; i < 10; i++ {
		X.Set(i, 0, float64(i))
		y[i] = float64(i*i) + rand.NormFloat64() // Adding some noise
	}

	yMat := mat.NewDense(len(y), 1, y)

	// Print the training data for debugging
	fmt.Println("Training data X:")
	matPrint(X)
	fmt.Println("Training data y:")
	fmt.Println(yMat)

	// Define the kernel: ConstantKernel * RBF
	constantKernel := &kernels.ConstantKernel{ConstantValue: 1.0, ConstantValueBounds: [2]float64{1e-5, 1e5}}
	rbfKernel := &kernels.RBF{LengthScale: []float64{1.5}, LengthScaleBounds: [][2]float64{{1e-5, 1e5}}}
	productKernel := &kernels.Product{
		KernelOperator: kernels.KernelOperator{
			K1: constantKernel,
			K2: rbfKernel,
		},
	}
	// Create the Gaussian Process Regressor
	gp := gaussianprocess.NewRegressor(productKernel)

	// Print Alpha for debugging
	fmt.Println("Alpha:", gp.Alpha)

	// Fit the model with X and yVec
	err := gp.Fit(X, yMat)
	if err != nil {
		log.Fatalf("Error fitting model: %v", err)
	}

	// Print fitted model info
	fmt.Println("Model fitted successfully.")
	fmt.Println("Xtrain:", gp.Xtrain)
	fmt.Println("Ytrain:", gp.Ytrain)

	// Predict new values
	X_test := mat.NewDense(5, 1, []float64{0, 2, 4, 6, 8})
	y_pred := gp.Predict(X_test, nil)

	// Print the test data and prediction results for debugging
	fmt.Println("Test data X_test:")
	matPrint(X_test)
	fmt.Println("Predicted values:")
	matPrint(y_pred)
}

// Helper function to print matrix
func matPrint(X mat.Matrix) {
	fr, fc := X.Dims()
	for r := 0; r < fr; r++ {
		for c := 0; c < fc; c++ {
			fmt.Printf("%f ", X.At(r, c))
		}
		fmt.Println()
	}
}

the output i am getting is below

Training data X:
0.000000 
1.000000 
2.000000 
3.000000 
4.000000 
5.000000 
6.000000 
7.000000 
8.000000 
9.000000 
Training data y:
&{{10 1 [0.5188212784827827 1.6037047407115053 3.7898678224401436 9.925588854546554 14.542592015266568 24.547637573998195 35.40832007088673 47.42076179321582 64.03240815464373 82.19464262249846] 1} 10 1}
1
Alpha: [1e-10]
2024/07/11 18:52:45 Error fitting model: 1**2 * RBF([1.5])

I cannot understand what that error about fitting model means

I analyzed the fit method's code and cannot see where it can go wrong, or I am just blind

Would really appreciate the help!
Thanks for the hard work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions