Example #1
0
func TestBeta(t *testing.T) {
	X := [][]float64{{1, 0, 5}, {2, 5, 4}, {3, 6, 5}, {8, 1, 1}}
	y := []float64{1, 0.5, 0.75, 0.2}
	beta := LinearRegression(X, y)
	fmt.Println(y)
	fmt.Println(matrix.VecMult(X, beta))
}
Example #2
0
func TestSimple(t *testing.T) {
	X := [][]float64{{1, 0}, {1, 0.5}, {1, 1}, {1, 1.5}}
	y := []float64{0.3, 0.4, 0.55, 0.6}
	beta := LinearRegression(X, y)
	fmt.Println(y, beta)
	fmt.Println(matrix.VecMult(X, beta))
}
Example #3
0
func TestCancer(t *testing.T) {
	X, y := datasets.Load("cancer")
	datasets.RandomShuffle(X, y)
	XTrain, XTest := X[:67], X[67:]
	yTrain, yTest := y[:67], y[67:]

	beta := LinearRegression(XTrain, yTrain)

	// validate on held out data
	yPred := matrix.VecMult(XTest, beta)
	fmt.Println("cancer error", metrics.MeanSquaredError(yPred, yTest))
}
Example #4
0
func LinearRegression(X [][]float64, y []float64) []float64 {
	Sxx := matrix.MatDirtyInverse(matrix.MatMultTrans(X, X))
	Sxy := matrix.VecMultTrans(X, y)
	beta := matrix.VecMult(Sxx, Sxy)
	return beta
}