Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Polynomial-regression/Go/polynomial-regression-1.go
Normal file
41
Task/Polynomial-regression/Go/polynomial-regression-1.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
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}
|
||||
|
||||
degree = 2
|
||||
|
||||
a = Vandermonde(x, degree+1)
|
||||
b = mat.NewDense(len(y), 1, y)
|
||||
c = mat.NewDense(degree+1, 1, nil)
|
||||
)
|
||||
|
||||
var qr mat.QR
|
||||
qr.Factorize(a)
|
||||
|
||||
const trans = false
|
||||
err := qr.SolveTo(c, trans, b)
|
||||
if err != nil {
|
||||
log.Fatalf("could not solve QR: %+v", err)
|
||||
}
|
||||
fmt.Printf("%.3f\n", mat.Formatted(c))
|
||||
}
|
||||
|
||||
func Vandermonde(a []float64, d int) *mat.Dense {
|
||||
x := mat.NewDense(len(a), d, nil)
|
||||
for i := range a {
|
||||
for j, p := 0, 1.0; j < d; j, p = j+1, p*a[i] {
|
||||
x.Set(i, j, p)
|
||||
}
|
||||
}
|
||||
return x
|
||||
}
|
||||
41
Task/Polynomial-regression/Go/polynomial-regression-2.go
Normal file
41
Task/Polynomial-regression/Go/polynomial-regression-2.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
var xGiven = []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
var yGiven = []float64{1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}
|
||||
var degree = 2
|
||||
|
||||
func main() {
|
||||
m := len(yGiven)
|
||||
n := degree + 1
|
||||
y := matrix.MakeDenseMatrix(yGiven, m, 1)
|
||||
x := matrix.Zeros(m, n)
|
||||
for i := 0; i < m; i++ {
|
||||
ip := float64(1)
|
||||
for j := 0; j < n; j++ {
|
||||
x.Set(i, j, ip)
|
||||
ip *= xGiven[i]
|
||||
}
|
||||
}
|
||||
|
||||
q, r := x.QR()
|
||||
qty, err := q.Transpose().Times(y)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
c := make([]float64, n)
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
c[i] = qty.Get(i, 0)
|
||||
for j := i + 1; j < n; j++ {
|
||||
c[i] -= c[j] * r.Get(i, j)
|
||||
}
|
||||
c[i] /= r.Get(i, i)
|
||||
}
|
||||
fmt.Println(c)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue