dongtang1909 2013-05-07 14:58
浏览 543

Go语言的线性回归库

I'm looking for a Go library that implements linear regression with MLE or LSE. Has anyone seen one?

There is this stats library, but it doesn't seem to have what I need: https://github.com/grd/statistics

Thanks!

  • 写回答

4条回答 默认 最新

  • dqnrk44682 2013-05-07 16:13
    关注

    Implementing an LSE (Least Squared Error) linear regression is fairly simple.

    Here's an implementation in JavaScript - it should be trivial to port to Go.


    Here's an (untested) port:

    package main
    
    import "fmt"
    
    type Point struct {
        X float64
        Y float64
    }
    
    func linearRegressionLSE(series []Point) []Point {
    
        q := len(series)
    
        if q == 0 {
            return make([]Point, 0, 0)
        }
    
        p := float64(q)
    
        sum_x, sum_y, sum_xx, sum_xy := 0.0, 0.0, 0.0, 0.0
    
        for _, p := range series {
            sum_x += p.X
            sum_y += p.Y
            sum_xx += p.X * p.X
            sum_xy += p.X * p.Y
        }
    
        m := (p*sum_xy - sum_x*sum_y) / (p*sum_xx - sum_x*sum_x)
        b := (sum_y / p) - (m * sum_x / p)
    
        r := make([]Point, q, q)
    
        for i, p := range series {
            r[i] = Point{p.X, (p.X*m + b)}
        }
    
        return r
    }
    
    func main() {
        // ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错