Package: geneticml
geneticmlThis package contains a simple and lightweight genetic algorithm for optimization of any machine learning model.InstallationUse pip to install the package from PyPI:pipinstallgeneticmlUsageThis package provides a easy way to create estimators and perform the optimization with genetic algorithms. The example below describe in details how to create a simulation with genetic algorithms using evolutionary approach to train asklearn.neural_network.MLPClassifier. A full list of examples could be foundhere.fromgeneticml.algorithmsimport(DataLoader,DefaultEstimatorMethods,EstimatorBuilder)fromgeneticml.optimizersimportGeneticOptimizerfromgeneticml.strategyimportEvolutionaryStrategy,StrategyParametersimportsklearn.metricsasmetricsfromsklearn.datasetsimportload_irisfromsklearn.exceptionsimportConvergenceWarning,UndefinedMetricWarningfromsklearn.neural_networkimportMLPClassifierif__name__=="__main__":seed=11412# Creates an estimatorestimator=EstimatorBuilder()\.of(model_type=MLPClassifier)\.fit_with(DefaultEstimatorMethods.fit)\.predict_with(DefaultEstimatorMethods.predict)\.build()# Defines a strategy for the optimizationstrategy=EvolutionaryStrategy(estimatort=estimator,parameters=parameters,retain=0.4,random_select=0.1,mutate_chance=0.2,max_children=2,random_state=seed)# Creates the optimizeroptimizer=GeneticOptimizer(strategy=strategy)# Loads the datadata=load_iris()# Defines the metricgreater_is_better=True# Create the simulation using the optimizer and the strategymodels=optimizer.simulate(train_data=DataLoader(data=data.data,target=data.target),generations=generations,population=population,evaluation_function=metrics.accuracy_score,greater_is_better=greater_is_better,verbose=True)The estimator is the way you define an algorithm or a class that will be used for model instantiationestimator=EstimatorBuilder().of(model_type=MLPClassifier).fit_with(func=fit).predict_with(func=predict).build()You can use the default functions provided by thegeneticml.algorithms.DefaultEstimatorMethodsor you can speficy your own functions to perform the operations. If you want to create your own, please note that these functions need to use the same signature than the below ones. This happens because the algorithm is generic and needs to know how to perform the fit and predict functions for the models.# Creates a custom fit methoddeffit(model,x,y):returnmodel.fit(x,y)# Creates a custom predict methoddefpredict(model,x):returnmodel.predict(x)Custom strategyYou can create custom strategies for the optimizers by extending thegeneticml.strategy.BaseStrategyand implementing theexecute(...)function.classMyCustomStrategy(BaseStrategy):def__init__(self,estimator_type:Type[BaseEstimator])->None:super().__init__(estimator_type)defexecute(self,population:List[Type[T]])->List[T]:returnpopulationThe custom strategies will allow you to create optimization strategies to archive your goals. We currently have the evolutionary strategy but you can define your own :)Custom optimizerYou can create custom optimizers by extending thegeneticml.optimizers.BaseOptimizerand implementing thesimulate(...)function.classMyCustomOptimizer(BaseOptimizer):def__init__(self,strategy:Type[BaseStrategy])->None:super().__init__(strategy)defsimulate(self,data,target,verbose:bool=True)->List[T]:"""Generate a network with the genetic algorithm.Parameters:data (?): The data used to train the algorithmtarget (?): The targets used to train the algorithmverbose (bool): True if should verbose or False if notReturns:(List[BaseEstimator]): A list with the final population sorted by their loss"""estimators=self._strategy.create_population()forxinestimators:x.fit(data,target)y_pred=x.predict(target)passCustom optimizers will let you define how you want your algorithm to optimize the selected strategy. You can also combine custom strategies and optimizers to archive your desire objective.TestingThe following are the steps to create a virtual environment into a folder named "venv" and install the requirements.# Create virtualenvpython3-mvenvvenv# activate virtualenvsourcevenv/bin/activate# update packagespipinstall--upgradepipsetuptoolswheel# install requirementspythonsetup.pyinstallTests can be run withpython setup.py testwhen the virtualenv is active.ContributingAll contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.A detailed overview on how to contribute can be found in the contributing guide. There is also an overview on GitHub.If you are simply looking to start working with the geneticml codebase, navigate to the GitHub "issues" tab and start looking through interesting issues. Or maybe through using geneticml you have an idea of your own or are looking for something in the documentation and thinking ‘this can be improved’...you can do something about it!Feel free to ask questions on the mailing the contributors.