Sklearn-genetic-opt:进化算法驱动的机器学习超参数调优与特征选择

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 都能显著提升模型的性能和优化效率。

Sklearn-genetic-opt ML hyperparameters tuning and features selection, using evolutionary algorithms. Sklearn-genetic-opt 项目地址: https://gitcode.com/gh_mirrors/sk/Sklearn-genetic-opt

Hyperopt-sklearn是基于scikit-learn项目的一个子集,其全称是:Hyper-parameter optimization for scikit-learn,即针对scikit-learn项目的超级参数化工具。由于scikit-learn是基于Python的机器学习开源框架,因此Hyperopt-sklearn也基于Python语言。Hyperopt-sklearn的文档称:对于开发者而言,针对不同的训练数据挑选一个合适的分类器(classifier)通常是困难的。而且即使选好了分类器,后面的参数试过程也相当乏味和耗时。更严重的是,还有许多情况是开发者好不容易试好了选定的分类器,却发现一开始的选择本身就是错误的,这本身就浪费了大量的精力和时间。针对该问题,Hyperopt-sklearn提供了一种解决方案。Hyperopt-sklearn支持各种不同的搜索算法(包括随机搜索、Tree of Parzen Estimators、Annealing等),可以搜索所有支持的分类器(KNeightborsClassifier、KNeightborsClassifier、SGDClassifier等)或者在给定的分类器下搜索所有可能的参数配置,并评估最选择。并且Hyperopt-sklearn还支持多种预处理流程,包括TfidfVectorizer,Normalzier和OneHotEncoder等。那么Hyperopt-sklearn的实际效果究竟如何?下表分别展示了使用scikit-learn默认参数和Hyperopt-sklearn化参数运行的分类器的F-score分数,数据源来自20个不同的新闻组稿件。可以看到,经过化的分类器的平均得分都要高于默认参数的情况。另外,Hyperopt-sklearn的编码量也很小,并且维护团队还提供了丰富的参考样例。 标签:Hyperopt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳嵘英Humphrey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值