| Python | import numpy as np;import statsmodels.api as sm;
 from statsmodels.sandbox.regression.predstd import wls_prediction_std;
 import matplotlib.pyplot as plt;
 
 #第4章第4.2.2节,例4.10
 rawX = np.array([[1.9, 2.9, 2.1, 3.6, 4.5, 3.8, 4.7, 4.1], [8.2, 8.3, 8.7, 9.7, 9.3, 9.6, 9.2, 9.6], [3.9, 4.8, 4.8, 4.6, 4.7, 4.3, 4.5, 3.8]]);
 rawX = rawX.transpose();
 X = sm.add_constant(rawX); #增加常量
 y = np.array([9.1, 9.6, 10.5, 11, 10.8, 11.2, 10.6, 9.7]);
  model = sm.OLS(y, X);  # 建立最小二乘模型(OLS)results = model.fit();  # 返回模型拟合结果
 yFit = results.fittedvalues;  # 模型拟合的 y值
 prstd, ivLow, ivUp = wls_prediction_std(results); # 返回标准偏差和置信区间
 # OLS model: Y = b0 + b1*X + ... + e
 print(results.summary());  # 输出回归分析的摘要
 print("\nOLS model: Y = b0 + b1 * x1 + b2 * x2 + b3 * x3");  # b0: 回归直线的截距,b1: 回归直线的斜率
 print('Parameters: ', results.params);  # 输出:拟合模型的系数
 
 
 | 
    
      | C++ | #include "orsci.h" #include "orsci_dm.h"
 using namespace orsci;
 using namespace dm;
 mdouble rawX = "1.9, 8.2, 3.9; 2.9, 8.3, 4.8; 2.1, 8.7, 4.8; 3.6, 9.7, 4.6;  4.5, 9.3, 4.7; 3.8, 9.6, 4.3; 4.7, 9.2, 4.5; 4.1, 9.6, 3.8;";
 coldouble y = "9.1, 9.6, 10.5, 11, 10.8, 11.2, 10.6, 9.7";
 cout << rawX.col(0).corr_pearson(y) << ", " << rawX.col(1).corr_pearson(y) << ", " << rawX.col(2).corr_pearson(y) << endl;
 dmt::regression::TLinearRegressor m;
 cout << m.VIF(rawX) << endl;
 m.train(y, rawX);
 cout << m.toSummary() << endl;
 
 |