コード例 #1
0
func main() {
	// task 1: show qr decomp of wp example
	a := mat64.NewDense(3, 3, []float64{
		12, -51, 4,
		6, 167, -68,
		-4, 24, -41,
	})
	var qr mat64.QR
	qr.Factorize(a)
	var q, r mat64.Dense
	q.QFromQR(&qr)
	r.RFromQR(&qr)
	fmt.Printf("q: %.3f\n\n", mat64.Formatted(&q, mat64.Prefix("   ")))
	fmt.Printf("r: %.3f\n\n", mat64.Formatted(&r, mat64.Prefix("   ")))

	// task 2: use qr decomp for polynomial regression example
	x := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	y := []float64{1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}
	a = Vandermonde(x, 2)
	b := mat64.NewDense(11, 1, y)
	qr.Factorize(a)
	var f mat64.Dense
	f.SolveQR(&qr, false, b)
	fmt.Printf("polyfit: %.3f\n",
		mat64.Formatted(&f, mat64.Prefix("         ")))
}
コード例 #2
0
func ExampleCholesky() {
	// Construct a symmetric positive definite matrix.
	tmp := mat64.NewDense(4, 4, []float64{
		2, 6, 8, -4,
		1, 8, 7, -2,
		2, 2, 1, 7,
		8, -2, -2, 1,
	})
	var a mat64.SymDense
	a.SymOuterK(1, tmp)

	fmt.Printf("a = %0.4v\n", mat64.Formatted(&a, mat64.Prefix("    ")))

	// Compute the cholesky factorization.
	var chol mat64.Cholesky
	if ok := chol.Factorize(&a); !ok {
		fmt.Println("a matrix is not positive semi-definite.")
	}

	// Find the determinant.
	fmt.Printf("\nThe determinant of a is %0.4g\n\n", chol.Det())

	// Use the factorization to solve the system of equations a * x = b.
	b := mat64.NewVector(4, []float64{1, 2, 3, 4})
	var x mat64.Vector
	if err := x.SolveCholeskyVec(&chol, b); err != nil {
		fmt.Println("Matrix is near singular: ", err)
	}
	fmt.Println("Solve a * x = b")
	fmt.Printf("x = %0.4v\n", mat64.Formatted(&x, mat64.Prefix("    ")))

	// Extract the factorization and check that it equals the original matrix.
	var t mat64.TriDense
	t.LFromCholesky(&chol)
	var test mat64.Dense
	test.Mul(&t, t.T())
	fmt.Println()
	fmt.Printf("L * L^T = %0.4v\n", mat64.Formatted(&a, mat64.Prefix("          ")))

	// Output:
	// a = ⎡120  114   -4  -16⎤
	//     ⎢114  118   11  -24⎥
	//     ⎢ -4   11   58   17⎥
	//     ⎣-16  -24   17   73⎦
	//
	// The determinant of a is 1.543e+06
	//
	// Solve a * x = b
	// x = ⎡  -0.239⎤
	//     ⎢  0.2732⎥
	//     ⎢-0.04681⎥
	//     ⎣  0.1031⎦
	//
	// L * L^T = ⎡120  114   -4  -16⎤
	//           ⎢114  118   11  -24⎥
	//           ⎢ -4   11   58   17⎥
	//           ⎣-16  -24   17   73⎦
}
コード例 #3
0
ファイル: layer.go プロジェクト: evilrobot69/NeuralGo
func (self *Layer) DebugString() string {
	return fmt.Sprintf(
		"name: %v\nweight: %v\ninput: %v\noutput: %v\ndeltas: %v\nderivatives: "+
			"%v\n", self.Name,
		mat64.Formatted(self.Weight, mat64.Prefix("        ")),
		mat64.Formatted(self.Input, mat64.Prefix("        ")),
		mat64.Formatted(self.Output, mat64.Prefix("        ")),
		mat64.Formatted(self.Deltas, mat64.Prefix("        ")),
		mat64.Formatted(self.Derivatives, mat64.Prefix("             ")))
}
コード例 #4
0
func showLU(a *mat64.Dense) {
	fmt.Printf("a: %v\n\n", mat64.Formatted(a, mat64.Prefix("   ")))
	var lu mat64.LU
	lu.Factorize(a)
	var l, u mat64.TriDense
	l.LFrom(&lu)
	u.UFrom(&lu)
	fmt.Printf("l: %.5f\n\n", mat64.Formatted(&l, mat64.Prefix("   ")))
	fmt.Printf("u: %.5f\n\n", mat64.Formatted(&u, mat64.Prefix("   ")))
	fmt.Println("p:", lu.Pivot(nil))
}
コード例 #5
0
func ExampleExcerpt() {
	// Excerpt allows diagnostic display of very large
	// matrices and vectors.

	// The big matrix is too large to properly print...
	big := mat64.NewDense(100, 100, nil)
	for i := 0; i < 100; i++ {
		big.Set(i, i, 1)
	}

	// so only print corner excerpts of the matrix.
	fmt.Printf("excerpt big identity matrix: %v\n\n",
		mat64.Formatted(big, mat64.Prefix(" "), mat64.Excerpt(3)))

	// The long vector is also too large, ...
	long := mat64.NewVector(100, nil)
	for i := 0; i < 100; i++ {
		long.SetVec(i, float64(i))
	}

	// ... so print end excerpts of the vector,
	fmt.Printf("excerpt long column vector: %v\n\n",
		mat64.Formatted(long, mat64.Prefix(" "), mat64.Excerpt(3)))
	// or its transpose.
	fmt.Printf("excerpt long row vector: %v\n",
		mat64.Formatted(long.T(), mat64.Prefix(" "), mat64.Excerpt(3)))

	// Output:
	// excerpt big identity matrix: Dims(100, 100)
	//  ⎡1  0  0  ...  ...  0  0  0⎤
	//  ⎢0  1  0            0  0  0⎥
	//  ⎢0  0  1            0  0  0⎥
	//  .
	//  .
	//  .
	//  ⎢0  0  0            1  0  0⎥
	//  ⎢0  0  0            0  1  0⎥
	//  ⎣0  0  0  ...  ...  0  0  1⎦
	//
	// excerpt long column vector: Dims(100, 1)
	//  ⎡ 0⎤
	//  ⎢ 1⎥
	//  ⎢ 2⎥
	//  .
	//  .
	//  .
	//  ⎢97⎥
	//  ⎢98⎥
	//  ⎣99⎦
	//
	// excerpt long row vector: Dims(1, 100)
	//  [ 0   1   2  ...  ...  97  98  99]

}
コード例 #6
0
func ExampleCholeskySymRankOne() {
	a := mat64.NewSymDense(4, []float64{
		1, 1, 1, 1,
		0, 2, 3, 4,
		0, 0, 6, 10,
		0, 0, 0, 20,
	})
	fmt.Printf("A = %0.4v\n", mat64.Formatted(a, mat64.Prefix("    ")))

	// Compute the Cholesky factorization.
	var chol mat64.Cholesky
	if ok := chol.Factorize(a); !ok {
		fmt.Println("matrix a is not positive definite.")
	}

	x := mat64.NewVector(4, []float64{0, 0, 0, 1})
	fmt.Printf("\nx = %0.4v\n", mat64.Formatted(x, mat64.Prefix("    ")))

	// Rank-1 update the factorization.
	chol.SymRankOne(&chol, 1, x)
	// Rank-1 update the matrix a.
	a.SymRankOne(a, 1, x)

	var au mat64.SymDense
	au.FromCholesky(&chol)

	// Print the matrix that was updated directly.
	fmt.Printf("\nA' =        %0.4v\n", mat64.Formatted(a, mat64.Prefix("            ")))
	// Print the matrix recovered from the factorization.
	fmt.Printf("\nU'^T * U' = %0.4v\n", mat64.Formatted(&au, mat64.Prefix("            ")))

	// Output:
	// A = ⎡ 1   1   1   1⎤
	//     ⎢ 1   2   3   4⎥
	//     ⎢ 1   3   6  10⎥
	//     ⎣ 1   4  10  20⎦
	//
	// x = ⎡0⎤
	//     ⎢0⎥
	//     ⎢0⎥
	//     ⎣1⎦
	//
	// A' =        ⎡ 1   1   1   1⎤
	//             ⎢ 1   2   3   4⎥
	//             ⎢ 1   3   6  10⎥
	//             ⎣ 1   4  10  21⎦
	//
	// U'^T * U' = ⎡ 1   1   1   1⎤
	//             ⎢ 1   2   3   4⎥
	//             ⎢ 1   3   6  10⎥
	//             ⎣ 1   4  10  21⎦
}
コード例 #7
0
func ExampleFormatted() {
	a := mat64.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})

	// Create a matrix formatting value with a prefix ...
	fa := mat64.Formatted(a, mat64.Prefix("    "))

	// and then print with and without zero value elements.
	fmt.Printf("with all values:\na = %v\n\n", fa)
	fmt.Printf("with only non-zero values:\na = % v\n\n", fa)

	// Modify the matrix...
	a.Set(0, 2, 0)

	// and print it without zero value elements.
	fmt.Printf("after modification with only non-zero values:\na = % v\n\n", fa)

	// Output:
	// with all values:
	// a = ⎡1  2  3⎤
	//     ⎢0  4  5⎥
	//     ⎣0  0  6⎦
	//
	// with only non-zero values:
	// a = ⎡1  2  3⎤
	//     ⎢.  4  5⎥
	//     ⎣.  .  6⎦
	//
	// after modification with only non-zero values:
	// a = ⎡1  2  .⎤
	//     ⎢.  4  5⎥
	//     ⎣.  .  6⎦
}
コード例 #8
0
ファイル: data_test.go プロジェクト: erubboli/mlt
func TestDataContainer(t *testing.T) {

	file, _ := os.Open("fixtures/2.csv")

	data := NewData(file, "target")
	if data.Cols != 4 {
		t.Error("Expected:", 4)
		t.Error("Actual:  ", data.Cols)
	}

	if data.Rows != 3 {
		t.Error("Expected:", 3)
		t.Error("Actual:  ", data.Rows)
	}

	if data.Labels[0] != "id" {
		t.Error("Expected:", "id")
		t.Error("Actual:  ", data.Labels[0])
	}

	if data.Data.At(2, 3) != 0.001 {
		t.Error("Expected:", "0.001")
		t.Error("Actual:  ", data.Data.At(2, 3))
	}

	fmt.Printf("data: %0.4v\n", mat64.Formatted(data.Data, mat64.Prefix("      ")))

}
コード例 #9
0
ファイル: pca_example_test.go プロジェクト: sbinet/gonum-stat
func ExamplePrincipalComponents() {
	// iris is a truncated sample of the Fisher's Iris dataset.
	n := 10
	d := 4
	iris := mat64.NewDense(n, d, []float64{
		5.1, 3.5, 1.4, 0.2,
		4.9, 3.0, 1.4, 0.2,
		4.7, 3.2, 1.3, 0.2,
		4.6, 3.1, 1.5, 0.2,
		5.0, 3.6, 1.4, 0.2,
		5.4, 3.9, 1.7, 0.4,
		4.6, 3.4, 1.4, 0.3,
		5.0, 3.4, 1.5, 0.2,
		4.4, 2.9, 1.4, 0.2,
		4.9, 3.1, 1.5, 0.1,
	})

	// Calculate the principal component direction vectors
	// and variances.
	vecs, vars, ok := stat.PrincipalComponents(iris, nil)
	if !ok {
		return
	}
	fmt.Printf("variances = %.4f\n\n", vars)

	// Project the data onto the first 2 principal components.
	k := 2
	var proj mat64.Dense
	proj.Mul(iris, vecs.View(0, 0, d, k))

	fmt.Printf("proj = %.4f", mat64.Formatted(&proj, mat64.Prefix("       ")))

	// Output:
	// variances = [0.1666 0.0207 0.0079 0.0019]
	//
	// proj = ⎡-6.1686   1.4659⎤
	//        ⎢-5.6767   1.6459⎥
	//        ⎢-5.6699   1.3642⎥
	//        ⎢-5.5643   1.3816⎥
	//        ⎢-6.1734   1.3309⎥
	//        ⎢-6.7278   1.4021⎥
	//        ⎢-5.7743   1.1498⎥
	//        ⎢-6.0466   1.4714⎥
	//        ⎢-5.2709   1.3570⎥
	//        ⎣-5.7533   1.6207⎦
}
コード例 #10
0
ファイル: main.go プロジェクト: rwcarlsen/tournament
func main() {
	flag.Parse()
	log.SetFlags(0)

	var tourn Tournament

	if *demo {
		tourn = Tournament{
			{"bob-r", "joe-c"},
			{"bob-r", "tim-c"},
			{"bob-r", "tim-c"},
			{"bob-r", "tim-c"},
			{"joe-r", "tim-c"},
			{"joe-r", "bob-c"},
			{"tim-r", "joe-c"},
			{"tim-r", "bob-c"},
			{"bob-c", "joe-r"},
			{"bob-c", "tim-r"},
			{"joe-c", "tim-r"},
			{"joe-c", "bob-r"},
			{"tim-c", "joe-r"},
			{"tim-c", "bob-r"},
		}
	} else {
		matches, err := ParseMatches(os.Stdin)
		if err != nil {
			log.Fatal(err)
		}
		tourn = Tournament(matches)
	}

	if *graph {
		tourn.Graph(os.Stdout)
		return
	} else if *matrix {
		prefix := "tournmat = "
		fmt.Printf("%v%v\n", prefix, mat64.Formatted(tourn.Matrix(), mat64.Prefix(strings.Repeat(" ", len(prefix)))))
		return
	} else if *eigvect {
		eig := mat64.Eigen(tourn.Matrix(), 1e-10)
		prefix := "eigvects = "
		fmt.Printf("%v%.4v\n", prefix, mat64.Formatted(eig.V, mat64.Prefix(strings.Repeat(" ", len(prefix)))))
		return
	} else if *eigval {
		eig := mat64.Eigen(tourn.Matrix(), 1e-10)
		prefix := "eigvals = "
		fmt.Printf("%v%.4v\n", prefix, mat64.Formatted(eig.D(), mat64.Prefix(strings.Repeat(" ", len(prefix)))))
		return
	}

	ranks := tourn.Ranks()
	if ranks == nil {
		log.Fatalf("no valid eigenvector ranking found")
	}

	players := tourn.Players()
	for i, rank := range ranks {
		fmt.Printf("%v\t%.2v\n", players[i], rank)
	}

}
コード例 #11
0
ファイル: utils.go プロジェクト: kingzbauer/kmeans
func printMatrix(M mat.Matrix) fmt.Formatter {
	return mat.Formatted(M, mat.Prefix(""))
}