【问题标题】:Minimal linear regression program最小线性回归程序
【发布时间】:2010-02-04 23:45:24
【问题描述】:

我在外部机器上运行一些计算,最后得到 X、Y 对。我想应用线性回归并获得 A、B 和 R2。在这台机器上我不能安装任何东西(它运行 Linux)并且安装了基本的东西,python,bash(当然)等等。

我想知道使用脚本(python、bash 等)或程序(我可以编译 C 和 C++)的最佳方法是什么,无需添加外部库(numpy 等)即可获得线性回归系数)

【问题讨论】:

  • 如果您不能使用库,请自己编写代码。直线梯度和截距的线性回归比一般线性回归更简单:见en.wikipedia.org/wiki/Simple_linear_regression
  • 如果你可以编译,你可以在你的主目录中安装你需要的任何东西(只要你的磁盘配额允许)。但是只需用 Python 自己编写代码,线性回归几乎就是几个列表推导式。

标签: c++ python bash linear-algebra


【解决方案1】:

对于单个、简单、已知的函数(如您的情况:一行),从头开始简单地编写基本的最小二乘例程并不难(但确实需要注意细节)。这是介绍性数值分析课程中非常常见的作业。

所以,在 wikipedia 或 mathworld 或教科书中查找最小二乘法,然后去镇上。

【讨论】:

    【解决方案2】:

    如何将系数提取到文件中,导入到另一台机器,然后使用 Excel/Matlab/任何其他为您执行此操作的程序?

    【讨论】:

      【解决方案3】:

      您好,这是我从 Wikipedia 关于最佳拟合线的文章中获得的解决方案。

      #include <iostream>
      #include <vector>
      
      // Returns true if linear fit was calculated. False otherwise.
      // Algorithm adapted from:
      // https://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line
      template <typename PairIterator>
      bool GetLinearFit(PairIterator begin_it,
                        PairIterator end_it,
                        double* out_slope,
                        double* out_yintercept) {
      
          if (begin_it == end_it) {
              return false;
          }
      
          size_t n = 0;
          double x_avg = 0;
          double y_avg = 0;
      
          for (PairIterator it = begin_it; it != end_it; ++it) {
              x_avg += it->first;
              y_avg += it->second;
              n++;
          }
      
          x_avg /= n;
          y_avg /= n;
      
          double numerator = 0;
          double denominator = 0;
      
          for (PairIterator it = begin_it; it != end_it; ++it) {
              double x_variance = it->first - x_avg;
              double y_variance = it->second - y_avg;
              numerator += (x_variance * y_variance);
              denominator += (x_variance * x_variance);
          }
      
          double slope = numerator / denominator;
          double yintercept = y_avg - slope*x_avg;
      
          *out_slope = slope;
          *out_yintercept= yintercept ;
      
          return true;
      }
      
      // Tests the output of GetLinearFit(...).
      int main() {
          std::vector<std::pair<int, int> > data;
          for (int i = 0; i < 10; ++i) {
            data.push_back(std::pair<int, int>(i+1, 2*i));
          }
      
          double slope = 0;
          double y_intercept = 0;
          GetLinearFit(data.begin(), data.end(), &slope, &y_intercept);
      
          std::cout << "slope: " << slope << "\n";
          std::cout << "y_intercept: " << y_intercept<< "\n";
      
          return 0;
      }
      

      【讨论】:

      • 请注意,在 C++ 中您应该使用引用而不是指针(对于 out_slopeout_yintercept)。
      猜你喜欢
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2017-03-24
      相关资源
      最近更新 更多