多元岭回归 C++有截距和预测的带类实现 及案例
时间: 2024-02-20 13:00:38 浏览: 147
以下是一个简单的多元岭回归的 C++ 类实现,包含截距和预测功能:
```cpp
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class RidgeRegression {
private:
vector<double> coef; // 回归系数
double intercept; // 截距
double alpha; // 防止过拟合的参数
int n_samples; // 样本数量
int n_features; // 特征数量
public:
RidgeRegression(double alpha = 1.0) {
this->alpha = alpha;
}
void fit(vector<vector<double>> &X, vector<double> &y) {
n_samples = X.size();
n_features = X[0].size();
// 计算 X.T * X
vector<vector<double>> xtx(n_features, vector<double>(n_features, 0.0));
for (int i = 0; i < n_samples; ++i) {
for (int j = 0; j < n_features; ++j) {
for (int k = 0; k < n_features; ++k) {
xtx[j][k] += X[i][j] * X[i][k];
}
}
}
// 加上 alpha * I
for (int i = 0; i < n_features; ++i) {
xtx[i][i] += alpha;
}
// 计算 (X.T * X + alpha * I).inv * X.T
vector<vector<double>> inv_xtx_x(n_features, vector<double>(n_samples, 0.0));
for (int i = 0; i < n_features; ++i) {
for (int j = 0; j < n_samples; ++j) {
for (int k = 0; k < n_features; ++k) {
inv_xtx_x[i][j] += xtx[i][k] * X[j][k];
}
inv_xtx_x[i][j] /= n_samples;
}
}
// 计算回归系数和截距
coef.resize(n_features);
for (int i = 0; i < n_features; ++i) {
for (int j = 0; j < n_samples; ++j) {
coef[i] += inv_xtx_x[i][j] * y[j];
}
}
intercept = 0.0;
for (int i = 0; i < n_samples; ++i) {
intercept += y[i];
}
intercept /= n_samples;
for (int i = 0; i < n_features; ++i) {
intercept -= coef[i] * X[0][i];
}
}
double predict(vector<double> &x) {
double y_pred = intercept;
for (int i = 0; i < n_features; ++i) {
y_pred += coef[i] * x[i];
}
return y_pred;
}
};
```
下面是一个使用该类的示例,用于预测房价:
```cpp
int main() {
// 样本数量和特征数量
int n_samples = 5;
int n_features = 2;
// 训练数据
vector<vector<double>> X(n_samples, vector<double>(n_features));
vector<double> y(n_samples);
X[0] = {0.18, 0.89};
X[1] = {1.0, 0.26};
X[2] = {0.92, 0.11};
X[3] = {0.07, 0.37};
X[4] = {0.85, 0.16};
y = {109.85, 155.72, 137.66, 76.17, 139.75};
// 创建 RidgeRegression 对象并拟合数据
RidgeRegression rr(1.0);
rr.fit(X, y);
// 预测房价
vector<double> x(n_features);
x = {0.75, 0.5};
double y_pred = rr.predict(x);
cout << "Predicted price: " << y_pred << endl;
return 0;
}
```
在这个示例中,我们使用了 5 个样本和 2 个特征来拟合一个多元线性回归模型,并且使用 Ridge Regression 算法来防止过拟合。最后,我们使用拟合好的模型来预测一个新的房价。
阅读全文