#abc-svm Demo1
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.svm import OneClassSVM
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
# 蜂群算法类
class ArtificialBeeColony:
def __init__(self, fitness_func, param_ranges,kernel_options, population_size=30, max_generations=1):
self.fitness_func = fitness_func
self.param_ranges = param_ranges
self.kernel_options = kernel_options # 添加kernel选项
self.population_size = population_size
self.max_generations = max_generations
self.population = self.initialize_population()
self.best_solution = None
self.best_fitness = -np.inf
def initialize_population(self):
population = []
for _ in range(self.population_size):
solution = {}
for param_name, (low, high, step) in self.param_ranges.items():
if isinstance(low, list): # Handle categorical variables
solution[param_name] = np.random.choice(low)
else:
solution[param_name] = low + step * np.random.rand() * (high - low)
# 随机选择一个kernel
solution['kernel'] = np.random.choice(self.kernel_options)
population.append(solution)
return population
def update_population(self, new_solutions):
self.population = new_solutions
current_best = max(new_solutions, key=lambda x: self.fitness_func(x))
current_fitness = self.fitness_func(current_best)
if current_fitness > self.best_fitness:
self.best_fitness = current_fitness
self.best_solution = current_best
def optimize(self):
for generation in range(self.max_generations):
new_population = []
for solution in self.population:
new_solution = self.search_for_new_solution(solution)
new_population.append(new_solution)
self.update_population(new_population)
print(f"Generation {generation + 1}: Best Fitness = {self.best_fitness}")
return self.best_solution, self.best_fitness
def search_for_new_solution(self, solution):
new_solution = {}
for param_name, value in solution.items():
if param_name == 'kernel':
# 对于kernel参数,从kernel_options列表中随机选择一个值
new_solution[param_name] = np.random.choice(self.kernel_options)
else:
low, high, step = self.param_ranges[param_name]
if isinstance(low, list): # Handle categorical variables
new_value = np.random.choice(low)
else:
new_value = value + step * (np.random.rand() - 0.5) * (high - low)
new_value = max(low, min(new_value, high)) # Ensure value is within bounds
new_solution[param_name] = new_value
return new_solution
import pandas as pd
# 创建数据集,路径需要修改成你自己的路径
# data = make_blobs(n_samples=500, centers=1, n_features=2, random_state=42)
# 数据excel 读取
orignal = pd.read_excel('C:/Users/11003189/Desktop/py/data.xlsx', sheet_name='点') # 读取数据
print(orignal)
col =['X','Y']
col1 =['Unnamed: 3']
data =orignal[col]
target =orignal[col1]
array =target.to_numpy()
print(array)
from sklearn.metrics import r2_score
# 适应度函数
def fitness_function(params):
# 选取X_train 为正类
# X_train, X_test, _, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# y_test = np.where(y_test == 0, -1, 1) # 将标签转换为-1和1
nu = params['nu']
kernel = params['kernel']
gamma = params['gamma']
# 创建和训练One-Class SVM模型
ocsvm = OneClassSVM(nu=nu, kernel=kernel, gamma=gamma)
# ocsvm.fit(X_train)
ocsvm.fit(data,target)
# 预测测试集并计算ROC AUC分数作为适应度值
# y_pred = ocsvm.predict(X_test)
predictions = ocsvm.predict(data)
# fitness =r2_score(y_test, y_pred)
count =0
for index, element in enumerate(predictions):
if(element ==array[index][0]):
count=count+1
return count
# return fitness
# 参数搜索范围
param_ranges = {
'nu': (0.1, 0.9, 0.1), # nu 的搜索范围,步长为 0.1
# 'kernel': ['rbf', 'linear'], # 核函数的选择列表
'gamma': (0.01, 1, 0.1) # gamma 的搜索范围,步长为 0.1
}
kernel_options = ['rbf', 'linear'] # 可能的kernel值列表
# 创建并运行人工蜂群算法
abc = ArtificialBeeColony(fitness_function, param_ranges, kernel_options)
best_solution, best_fitness = abc.optimize()
# Best Solution: {'nu': 0.10587043848153328, 'gamma': 0.2575143995541765, 'kernel': 'rbf'}
# Best Fitness: 1.0
# 输出最佳解和最佳适应度
print(f"Best Solution: {best_solution}")
print(f"Best Fitness: {best_fitness}")
print(best_solution['nu'])
print(best_solution['gamma'])
print(best_solution['kernel'])
# 最优模型创建
ocsvm = OneClassSVM(nu=best_solution['nu'], kernel=best_solution['kernel'], gamma=best_solution['gamma'])
ocsvm.fit(data,target)
predictions = ocsvm.predict(data)
# 绘图
import matplotlib.pyplot as plt
import numpy as np
# 预测数据生成图
colors =np.where(predictions == -1, 0, 1)# 0黑色 1亮色
# print(colors)
col2 =['X']
col3 =['Y']
x= orignal[col2]
y= orignal[col3]
fig, ax = plt.subplots()
scatter = ax.scatter(x, y, c=colors)
plt.colorbar(scatter) # 显示颜色条
plt.show()# 如果不预览就不用打开
# plt.savefig('predictions.png')
# 真实数据生成图
colors =np.where(target == -1, 0, 1)# 0黑色 1亮色
# print(colors)
col2 =['X']
col3 =['Y']
x= orignal[col2]
y= orignal[col3]
fig, ax = plt.subplots()
scatter = ax.scatter(x, y, c=colors)
plt.colorbar(scatter) # 显示颜色条
plt.show()# 如果不预览就不用打开
# plt.savefig('predictions.png')