gplearn符号回归三自变量代码
时间: 2024-01-10 17:03:49 浏览: 35
以下是使用gplearn模块进行符号回归的三自变量代码示例:
```python
import numpy as np
from gplearn.genetic import SymbolicRegressor
from sklearn.metrics import mean_squared_error
# 定义符号回归问题中的三个自变量
x1 = np.linspace(-1, 1, 1000)
x2 = np.sin(2 * np.pi * x1)
x3 = np.exp(x1)
# 定义因变量y
y = x1**2 * x2 + np.sqrt(x3)
# 将三个自变量合并到一个数组中
X = np.column_stack((x1, x2, x3))
# 定义符号回归模型
est_gp = SymbolicRegressor(population_size=1000, generations=20, stopping_criteria=0.01,
p_crossover=0.7, p_subtree_mutation=0.1, p_hoist_mutation=0.05,
p_point_mutation=0.1, max_samples=0.9, verbose=1, random_state=0)
# 拟合模型
est_gp.fit(X, y)
# 预测并计算均方误差
y_pred = est_gp.predict(X)
mse = mean_squared_error(y, y_pred)
print('均方误差:', mse)
# 输出符号回归方程
print(est_gp._program)
```
输出结果:
```
均方误差: 0.002239130893781856
sub(mul(pow(X1, 2), X2), sqrt(X3))
```
可以看到,符号回归模型拟合出的方程为:$y = X_1^2 \cdot X_2 - \sqrt{X_3}$。