多元多项式回归 C++带类详细正确完整实现及案例 变量形式为:vector<vector<double>> X vector<double> y
时间: 2023-10-11 10:13:05 浏览: 74
以下是一个基于类的多元多项式回归的实现,实现了最小二乘法(Ordinary Least Squares)来拟合模型。
```cpp
#include <vector>
#include <iostream>
#include <cmath>
class PolynomialRegression {
private:
int degree; // 多项式的次数
std::vector<std::vector<double>> X;
std::vector<double> y;
std::vector<double> coefficients; // 多项式系数
public:
PolynomialRegression(int degree) {
this->degree = degree;
}
// 添加样本
void addSample(std::vector<double> x, double y) {
X.push_back(x);
this->y.push_back(y);
}
// 多项式回归
void fit() {
int n = X.size();
int m = X[0].size();
std::vector<std::vector<double>> X_T(m, std::vector<double>(n));
std::vector<std::vector<double>> X_T_X(m, std::vector<double>(m));
// 计算 X 的转置矩阵
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
X_T[j][i] = X[i][j];
}
}
// 计算 X_T * X 矩阵
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
double sum = 0.0;
for (int k = 0; k < n; k++) {
sum += X_T[i][k] * X[k][j];
}
X_T_X[i][j] = sum;
}
}
// 计算 X_T * y 向量
std::vector<double> X_T_y(m);
for (int i = 0; i < m; i++) {
double sum = 0.0;
for (int j = 0; j < n; j++) {
sum += X_T[i][j] * y[j];
}
X_T_y[i] = sum;
}
// 解方程 X_T * X * coefficients = X_T * y
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
double factor = X_T_X[j][i] / X_T_X[i][i];
for (int k = i; k < m; k++) {
X_T_X[j][k] -= factor * X_T_X[i][k];
}
X_T_y[j] -= factor * X_T_y[i];
}
}
coefficients.resize(m);
for (int i = m - 1; i >= 0; i--) {
double sum = 0.0;
for (int j = i + 1; j < m; j++) {
sum += X_T_X[i][j] * coefficients[j];
}
coefficients[i] = (X_T_y[i] - sum) / X_T_X[i][i];
}
}
// 预测
double predict(std::vector<double> x) {
double y_pred = 0.0;
for (int i = 0; i <= degree; i++) {
y_pred += coefficients[i] * std::pow(x[i], i);
}
return y_pred;
}
};
int main() {
// 创建多项式回归模型,指定多项式的次数
PolynomialRegression model(2);
// 添加样本
std::vector<double> x1 = {1.0, 1.0};
model.addSample(x1, 2.0);
std::vector<double> x2 = {1.0, 2.0};
model.addSample(x2, 3.0);
std::vector<double> x3 = {1.0, 3.0};
model.addSample(x3, 4.0);
// 训练模型
model.fit();
// 预测
std::vector<double> x_test = {1.0, 4.0};
double y_pred = model.predict(x_test);
std::cout << "Predicted value: " << y_pred << std::endl;
return 0;
}
```
这个类实现了 `addSample` 方法来添加样本, `fit` 方法来训练模型, `predict` 方法来预测新的样本点。
在这个例子中,我们创建了一个二次多项式回归模型,并添加了三个样本点(x1, x2 和 x3),然后训练模型并预测了一个新的样本点(x_test)的结果。
阅读全文