给我一份excel数据集的GA优化SVR预测的有对比图的代码
时间: 2024-02-06 09:09:33 浏览: 31
以下是一份Excel数据集的GA优化SVR预测的代码,包含对比图。
```python
import pandas as pd
import numpy as np
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.model_selection import train_test_split
from deap import algorithms, base, creator, tools
# 读入数据
data = pd.read_excel('data.xlsx')
x = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 数据标准化
scaler = StandardScaler()
x = scaler.fit_transform(x)
# 分割训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
# GA优化SVR参数
creator.create('Fitness', base.Fitness, weights=(-1.0,))
creator.create('Individual', list, fitness=creator.Fitness)
toolbox = base.Toolbox()
toolbox.register('attr_float', np.random.uniform, -1, 1)
toolbox.register('individual', tools.initRepeat, creator.Individual, toolbox.attr_float, n=len(x[0]))
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
C = np.logspace(-3, 3, 10)
gamma = np.logspace(-3, 3, 10)
toolbox.register('mate', tools.cxUniform, indpb=0.5)
toolbox.register('mutate', tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register('select', tools.selTournament, tournsize=3)
def evaluate(individual):
svr = SVR(kernel='rbf', C=10 ** individual[0], gamma=10 ** individual[1])
svr.fit(x_train, y_train)
y_pred = svr.predict(x_test)
r2 = r2_score(y_test, y_pred)
return r2,
toolbox.register('evaluate', evaluate)
population = toolbox.population(n=50)
NGEN = 10
for gen in range(NGEN):
offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1)
fits = toolbox.map(toolbox.evaluate, offspring)
for fit, ind in zip(fits, offspring):
ind.fitness.values = fit
population = toolbox.select(offspring, k=len(population))
# 找到最佳参数
best_ind = tools.selBest(population, k=1)[0]
best_C = 10 ** best_ind[0]
best_gamma = 10 ** best_ind[1]
# 使用最佳参数进行SVR预测
svr = SVR(kernel='rbf', C=best_C, gamma=best_gamma)
svr.fit(x_train, y_train)
y_train_pred = svr.predict(x_train)
y_test_pred = svr.predict(x_test)
# 计算R2和均方误差
r2_train = r2_score(y_train, y_train_pred)
r2_test = r2_score(y_test, y_test_pred)
mse_train = mean_squared_error(y_train, y_train_pred)
mse_test = mean_squared_error(y_test, y_test_pred)
# 绘制预测结果图
import matplotlib.pyplot as plt
plt.plot(y_train, 'b', label='train')
plt.plot(y_test, 'g', label='test')
plt.plot(y_test_pred, 'r', label='prediction')
plt.legend(loc='upper left')
plt.show()
```
在上述代码中,我们首先读入Excel数据集,并对数据进行标准化和分割训练集和测试集。然后使用DEAP库中的遗传算法优化SVR的参数,找到最佳的C和gamma值。最后使用最佳参数进行SVR预测,并计算R2和均方误差,同时绘制预测结果图。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)