import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 加载数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义目标函数(SVM分类器的准确率)
def objective_function(params):
C, gamma = params
# 使用SVM进行分类
clf = SVC(C=C, gamma=gamma, random_state=42)
clf.fit(X_train, y_train)
# 计算准确率
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
return -accuracy # PSO是一个最小化算法,因此使用负准确率
# 定义共享粒子群算法
def social_pso(objective_function, num_particles=10, max_iter=50, search_space=[(0.1, 10), (0.1, 1)]):
# 初始化粒子位置和速度
particles_position = np.random.uniform(low=search_space[0][0], high=search_space[0][1], size=(num_particles, 1))
particles_velocity = np.zeros((num_particles, 1))
# 初始化最佳个体位置和适应值
best_particle_position = np.copy(particles_position)
best_particle_fitness = np.array([objective_function(p) for p in particles_position])
# 初始化全局最佳个体位置和适应值
global_best_position = np.copy(best_particle_position[np.argmin(best_particle_fitness)])
global_best_fitness = np.min(best_particle_fitness)
# 粒子群优化迭代
for iteration in range(max_iter):
for i in range(num_particles):
# 更新粒子速度和位置
inertia_term = 0.5 * particles_velocity[i]
cognitive_term = 1.5 * np.random.random() * (best_particle_position[i] - particles_position[i])
social_term = 1.5 * np.random.random() * (global_best_position - particles_position[i])
particles_velocity[i] = inertia_term + cognitive_term + social_term
particles_position[i] += particles_velocity[i]
# 对位置进行修正,确保在搜索空间内
particles_position[i] = np.clip(particles_position[i], search_space[0][0], search_space[0][1])
# 更新个体最佳位置和适应值
current_fitness = objective_function(particles_position[i])
if current_fitness < best_particle_fitness[i]:
best_particle_fitness[i] = current_fitness
best_particle_position[i] = particles_position[i]
# 更新全局最佳位置和适应值
if current_fitness < global_best_fitness:
global_best_fitness = current_fitness
global_best_position = particles_position[i]
# 输出每轮迭代的结果
print(f"Iteration {iteration + 1}: Global Best Fitness = {global_best_fitness}")
return global_best_position, global_best_fitness
# 调用共享粒子群算法进行SVM分类器参数优化
best_params, best_accuracy = social_pso(objective_function)
print("\nOptimization Result:")
print(f"Best Parameters: C = {best_params[0]}, gamma = {best_params[1]}")
print(f"Best Accuracy: {best_accuracy}")
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score# 加载数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# 定义目标函数(SVM分类器的准确率)
def objective_function(params):C, gamma = params# 使用SVM进行分类clf = SVC(C=C, gamma=gamma, random_state=42)clf.fit(X_train, y_train)# 计算准确率y_pred = clf.predict(X_test)accuracy = accuracy_score(y_test, y_pred)return -accuracy # PSO是一个最小化算法,因此使用负准确率# 定义共享粒子群算法
def social_pso(objective_function, num_particles=10, max_iter=50, search_space=[(0.1, 10), (0.1, 1)]):# 初始化粒子位置和速度particles_position = np.random.uniform(low=search_space[0][0], high=search_space[0][1], size=(num_particles, 1))particles_velocity = np.zeros((num_particles, 1))# 初始化最佳个体位置和适应值best_particle_position = np.copy(particles_position)best_particle_fitness = np.array([objective_function(p) for p in particles_position])# 初始化全局最佳个体位置和适应值global_best_position = np.copy(best_particle_position[np.argmin(best_particle_fitness)])global_best_fitness = np.min(best_particle_fitness)# 粒子群优化迭代for iteration in range(max_iter):for i in range(num_particles):# 更新粒子速度和位置inertia_term = 0.5 * particles_velocity[i]cognitive_term = 1.5 * np.random.random() * (best_particle_position[i] - particles_position[i])social_term = 1.5 * np.random.random() * (global_best_position - particles_position[i])particles_velocity[i] = inertia_term + cognitive_term + social_termparticles_position[i] += particles_velocity[i]# 对位置进行修正,确保在搜索空间内particles_position[i] = np.clip(particles_position[i], search_space[0][0], search_space[0][1])# 更新个体最佳位置和适应值current_fitness = objective_function(particles_position[i])if current_fitness < best_particle_fitness[i]:best_particle_fitness[i] = current_fitnessbest_particle_position[i] = particles_position[i]# 更新全局最佳位置和适应值if current_fitness < global_best_fitness:global_best_fitness = current_fitnessglobal_best_position = particles_position[i]# 输出每轮迭代的结果print(f"Iteration {iteration + 1}: Global Best Fitness = {global_best_fitness}")return global_best_position, global_best_fitness# 调用共享粒子群算法进行SVM分类器参数优化
best_params, best_accuracy = social_pso(objective_function)print("\nOptimization Result:")
print(f"Best Parameters: C = {best_params[0]}, gamma = {best_params[1]}")
print(f"Best Accuracy: {best_accuracy}")