Sklearn-genetic-opt:进化算法驱动的机器学习超参数调优与特征选择
项目地址:https://gitcode.com/gh_mirrors/sk/Sklearn-genetic-opt
项目介绍
Sklearn-genetic-opt 是一个基于进化算法的开源项目,专为 scikit-learn 模型的超参数调优和特征选择设计。它提供了一种替代传统方法(如网格搜索和随机网格搜索)的解决方案,通过进化算法来优化模型的超参数和特征子集,从而提升模型的性能。
项目技术分析
Sklearn-genetic-opt 的核心技术基于进化算法,具体使用了 DEAP(Distributed Evolutionary Algorithms in Python)库中的算法。进化算法通过模拟自然选择和遗传机制,逐步优化模型的超参数和特征子集。项目提供了以下主要功能:
- GASearchCV:用于超参数调优的主类,支持交叉验证优化。
- GAFeatureSelectionCV:用于特征选择的主类。
- 算法:支持多种进化算法,如遗传算法、粒子群优化等。
- 回调函数:提供自定义评估策略,支持早停、日志记录(如 TensorBoard)等功能。
- 调度器:支持自适应学习率调度,优化训练过程。
- 绘图:内置多种可视化工具,帮助用户理解优化过程。
- MLflow 集成:支持与 MLflow 集成,方便实验管理和结果追踪。
项目及技术应用场景
Sklearn-genetic-opt 适用于以下场景:
- 超参数调优:当使用传统网格搜索或随机搜索方法效率低下时,进化算法能够更高效地找到最优超参数组合。
- 特征选择:在处理高维数据时,通过进化算法自动选择最相关的特征子集,减少计算复杂度和过拟合风险。
- 模型优化:适用于各种机器学习任务,如分类、回归等,提升模型性能。
项目特点
- 高效性:相比传统方法,进化算法在处理复杂超参数空间时更具优势,能够更快找到最优解。
- 灵活性:支持多种进化算法和自定义回调函数,满足不同优化需求。
- 可视化:内置丰富的可视化工具,帮助用户直观理解优化过程。
- 集成性:与 MLflow 等工具无缝集成,方便实验管理和结果追踪。
使用示例
超参数调优
from sklearn_genetic import GASearchCV
from sklearn_genetic.space import Continuous, Categorical, Integer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
data = load_digits()
n_samples = len(data.images)
X = data.images.reshape((n_samples, -1))
y = data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
clf = RandomForestClassifier()
param_grid = {'min_weight_fraction_leaf': Continuous(0.01, 0.5, distribution='log-uniform'),
'bootstrap': Categorical([True, False]),
'max_depth': Integer(2, 30),
'max_leaf_nodes': Integer(2, 35),
'n_estimators': Integer(100, 300)}
cv = StratifiedKFold(n_splits=3, shuffle=True)
evolved_estimator = GASearchCV(estimator=clf,
cv=cv,
scoring='accuracy',
population_size=20,
generations=35,
param_grid=param_grid,
n_jobs=-1,
verbose=True,
keep_top_k=4)
# Train and optimize the estimator
evolved_estimator.fit(X_train, y_train)
# Best parameters found
print(evolved_estimator.best_params_)
# Use the model fitted with the best parameters
y_predict_ga = evolved_estimator.predict(X_test)
print(accuracy_score(y_test, y_predict_ga))
# Saved metadata for further analysis
print("Stats achieved in each generation: ", evolved_estimator.history)
print("Best k solutions: ", evolved_estimator.hof)
特征选择
from sklearn_genetic import GAFeatureSelectionCV, ExponentialAdapter
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
import numpy as np
data = load_iris()
X, y = data["data"], data["target"]
# Add random non-important features
noise = np.random.uniform(5, 10, size=(X.shape[0], 5))
X = np.hstack((X, noise))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)
clf = SVC(gamma='auto')
mutation_scheduler = ExponentialAdapter(0.8, 0.2, 0.01)
crossover_scheduler = ExponentialAdapter(0.2, 0.8, 0.01)
evolved_estimator = GAFeatureSelectionCV(
estimator=clf,
scoring="accuracy",
population_size=30,
generations=20,
mutation_probability=mutation_scheduler,
crossover_probability=crossover_scheduler,
n_jobs=-1)
# Train and select the features
evolved_estimator.fit(X_train, y_train)
# Features selected by the algorithm
features = evolved_estimator.support_
print(features)
# Predict only with the subset of selected features
y_predict_ga = evolved_estimator.predict(X_test)
print(accuracy_score(y_test, y_predict_ga))
# Transform the original data to the selected features
X_reduced = evolved_estimator.transform(X_test)
总结
Sklearn-genetic-opt 通过进化算法为 scikit-learn 模型的超参数调优和特征选择提供了强大的工具。其高效、灵活和可视化的特点,使其成为机器学习工程师和数据科学家的理想选择。无论是在学术研究还是工业应用中,Sklearn-genetic-opt 都能显著提升模型的性能和优化效率。