给我一份excel数据集的GA优化SVR的有对比图的代码
时间: 2024-02-06 07:09:44 浏览: 26
以下是一份使用遗传算法优化支持向量回归模型的Excel数据集的代码示例,包括对比图:
```python
import numpy as np
import pandas as pd
from sklearn.metrics import r2_score
from sklearn.svm import SVR
from deap import algorithms, base, creator, tools
# 加载数据集
data = pd.read_excel('data.xlsx')
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 定义评价函数
def evaluate(individual):
# 解码个体
C = individual[0]
epsilon = individual[1]
gamma = individual[2]
# 创建SVR模型
model = SVR(C=C, epsilon=epsilon, gamma=gamma)
# 训练模型
model.fit(X, y)
# 预测并计算R2得分
y_pred = model.predict(X)
score = r2_score(y, y_pred)
return score,
# 定义遗传算法参数
POP_SIZE = 50
GEN_NUM = 100
CXPB = 0.5
MUTPB = 0.2
IND_SIZE = 3
MIN_C = 0.1
MAX_C = 100
MIN_EPSILON = 0.01
MAX_EPSILON = 1.0
MIN_GAMMA = 0.001
MAX_GAMMA = 1.0
# 创建适应度函数和个体类
creator.create('FitnessMax', base.Fitness, weights=(1.0,))
creator.create('Individual', list, fitness=creator.FitnessMax)
# 注册遗传算法操作
toolbox = base.Toolbox()
toolbox.register('attr_c', np.random.uniform, MIN_C, MAX_C)
toolbox.register('attr_epsilon', np.random.uniform, MIN_EPSILON, MAX_EPSILON)
toolbox.register('attr_gamma', np.random.uniform, MIN_GAMMA, MAX_GAMMA)
toolbox.register('individual', tools.initCycle, creator.Individual,
(toolbox.attr_c, toolbox.attr_epsilon, toolbox.attr_gamma), IND_SIZE)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register('mate', tools.cxTwoPoint)
toolbox.register('mutate', tools.mutGaussian, mu=0, sigma=0.1, indpb=0.1)
toolbox.register('select', tools.selTournament, tournsize=3)
toolbox.register('evaluate', evaluate)
# 运行遗传算法
pop = toolbox.population(n=POP_SIZE)
best_ind = None
best_score = -np.inf
for gen in range(GEN_NUM):
print('Generation:', gen+1)
offspring = algorithms.varOr(pop, toolbox, cxpb=CXPB, mutpb=MUTPB)
fits = toolbox.map(toolbox.evaluate, offspring)
for fit, ind in zip(fits, offspring):
ind.fitness.values = fit
if fit > best_score:
best_score = fit
best_ind = ind
pop = toolbox.select(offspring, k=len(pop))
print('Best Score:', best_score)
print('Best Individual:', best_ind)
# 输出最优参数和R2得分
C, epsilon, gamma = best_ind
print('Best C:', C)
print('Best Epsilon:', epsilon)
print('Best Gamma:', gamma)
model = SVR(C=C, epsilon=epsilon, gamma=gamma)
model.fit(X, y)
y_pred = model.predict(X)
print('R2 Score:', r2_score(y, y_pred))
# 绘制预测结果和真实结果对比图
import matplotlib.pyplot as plt
plt.scatter(y, y_pred)
plt.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=2)
plt.xlabel('Measured')
plt.ylabel('Predicted')
plt.show()
```
在这个示例中,我们使用遗传算法来寻找最佳的SVR模型参数(C,epsilon,gamma),以最大化模型在数据集上的R2得分。我们使用DEAP库来实现遗传算法,并使用Sklearn库中的SVR类来创建模型。最终,我们输出最佳参数和R2得分,并绘制预测结果和真实结果对比图。