给我一份excel数据集的GA优化SVR预测的有对比图的代码
时间: 2024-02-06 10:09:44 浏览: 99
遗传算法GA优化支持向量机回归算法SVR,python写,自带数据集
好的,以下是使用遗传算法优化支持向量回归(SVR)进行预测的代码,包括对比图:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from deap import base, creator, tools, algorithms
# 读取数据集
data = pd.read_excel("data.xlsx")
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 创建FitnessMin类和Individual类
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
# 初始化toolbox
toolbox = base.Toolbox()
toolbox.register("attr_float", np.random.uniform, -10, 10)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=len(X[0]))
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evaluate_svr, X=X, y=y)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
# 定义评估函数
def evaluate_svr(individual, X, y):
# 将个体转换为numpy数组并reshape
individual = np.asarray(individual)
individual = np.reshape(individual, (-1, len(X[0])))
# 分割训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 创建SVR模型
svr = SVR(kernel="rbf", C=individual[0][0], gamma=individual[0][1])
# 训练SVR模型
svr.fit(X_train, y_train)
# 计算预测值
y_pred = svr.predict(X_test)
# 计算均方误差和R2分数
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
return mse,
# 定义主函数
def main():
pop_size = 50 # 种群数量
n_gen = 20 # 迭代代数
cx_prob = 0.5 # 交叉概率
mut_prob = 0.2 # 变异概率
# 创建种群
pop = toolbox.population(n=pop_size)
# 运行遗传算法
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("min", np.min)
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=cx_prob, mutpb=mut_prob, ngen=n_gen, stats=stats, halloffame=hof, verbose=True)
# 获取最佳个体
best = hof[0]
# 训练最佳个体的SVR模型
best = np.asarray(best)
best = np.reshape(best, (-1, len(X[0])))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
svr = SVR(kernel="rbf", C=best[0][0], gamma=best[0][1])
svr.fit(X_train, y_train)
# 绘制预测结果对比图
y_pred_train = svr.predict(X_train)
y_pred_test = svr.predict(X_test)
plt.scatter(np.arange(len(y_train)), y_train, label="true_train")
plt.plot(np.arange(len(y_train)), y_pred_train, label="pred_train")
plt.scatter(np.arange(len(y_test))+len(y_train), y_test, label="true_test")
plt.plot(np.arange(len(y_test))+len(y_train), y_pred_test, label="pred_test")
plt.legend()
plt.show()
if __name__ == "__main__":
main()
```
需要注意的是,此代码使用的是DEAP库实现遗传算法,需要先安装DEAP库。运行过程中需要将`evaluate_svr`函数和`main`函数中的SVR参数进行调整,以适应不同的数据集。
阅读全文