用python实现遗传算法的特征选择代码
时间: 2024-01-16 07:04:23 浏览: 86
基于 Python 实现特征选择的遗传算法【100010249】
5星 · 资源好评率100%
以下是一个使用Python实现遗传算法进行特征选择的示例代码:
```python
import random
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# 加载数据集
iris = load_iris()
X, y = iris.data, iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义适应度函数
def fitness(features, X_train, X_test, y_train, y_test):
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train[:, features], y_train)
return knn.score(X_test[:, features], y_test)
# 定义遗传算法函数
def genetic_algorithm(X_train, X_test, y_train, y_test, n_population, n_generation, n_feature):
# 初始化种群
population = np.random.randint(2, size=(n_population, n_feature))
for i in range(n_generation):
# 计算适应度
fitness_scores = [fitness(features, X_train, X_test, y_train, y_test) for features in population]
# 选择
idx = np.argsort(fitness_scores)[::-1]
population = population[idx][:n_population//2]
# 交叉
for j in range(0, n_population//2, 2):
cross_point = random.randint(1, n_feature-1)
population[j][cross_point:], population[j+1][cross_point:] = \
population[j+1][cross_point:], population[j][cross_point:]
# 变异
mutation_idx = np.random.randint(n_population//2, size=n_population//10)
mutation_bits = np.random.randint(0, 2, size=(len(mutation_idx), n_feature))
population[mutation_idx] = mutation_bits
# 计算最终适应度
fitness_scores = [fitness(features, X_train, X_test, y_train, y_test) for features in population]
best_features = population[np.argmax(fitness_scores)]
return best_features
# 运行遗传算法
best_features = genetic_algorithm(X_train, X_test, y_train, y_test, n_population=20, n_generation=100, n_feature=X.shape[1])
# 输出结果
print("最佳特征:", best_features)
```
阅读全文