Package: evopreprocess
EvoPreprocessEvoPreprocess is a Python toolkit for sampling datasets, instance weighting, and feature selection. It is compatible withscikit-learnandimbalanced-learn. It is based onNiaPylibrary for the implementation of nature-inspired algorithms and is distributed under GNU General Public License v3.0 license.Getting StartedThese instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.RequirementsPython 3.6+PIPDependenciesEvoSampling requires:numpyscipyscikit-learnimbalanced-learnNiaPy(>=2.0.4)InstallationInstall EvoPreprocess with pip:$pipinstallevopreprocessTo install EvoPreprocess on Fedora:$dnfinstallpython-EvoPreprocessOr directly from the source code:$gitclonehttps://github.com/karakatic/EvoPreprocess.git
$cdevopreprocess
$pythonsetup.pyinstallUsageAfter installation, the package can be imported:$python
>>>importevopreprocess
>>>evopreprocess.__version__Data samplingSimple data sampling examplefromsklearn.datasetsimportload_breast_cancerfromevopreprocess.data_samplingimportEvoSampling# Load classification datadataset=load_breast_cancer()# Print the size of datasetprint(dataset.data.shape,len(dataset.target))# Sample instances of dataset with default settings with EvoSamplingX_resampled,y_resampled=EvoSampling().fit_resample(dataset.data,dataset.target)# Print the size of dataset after samplingprint(X_resampled.shape,len(y_resampled))Data sampling for regression with custom nature-inspired algorithm and other custom settingsimportniapy.algorithms.basicasniafromsklearn.datasetsimportload_bostonfromsklearn.treeimportDecisionTreeRegressorfromevopreprocess.data_samplingimportEvoSampling# Load regression datadataset=load_boston()# Print the size of datasetprint(dataset.data.shape,len(dataset.target))# Sample instances of dataset with custom settings and regression with EvoSamplingX_resampled,y_resampled=EvoSampling(evaluator=DecisionTreeRegressor(),optimizer=nia.EvolutionStrategyMpL,n_folds=5,n_runs=5,n_jobs=4).fit_resample(dataset.data,dataset.target)# Print the size of dataset after samplingprint(X_resampled.shape,len(y_resampled))Data sampling with scikit-learnfromsklearn.datasetsimportload_breast_cancerfromsklearn.metricsimportaccuracy_scorefromsklearn.model_selectionimporttrain_test_splitfromsklearn.treeimportDecisionTreeClassifierfromevopreprocess.data_samplingimportEvoSampling# Set the random seed for the reproducibilityrandom_seed=1111# Load classification datadataset=load_breast_cancer()# Split the dataset to training and testing setX_train,X_test,y_train,y_test=train_test_split(dataset.data,dataset.target,test_size=0.33,random_state=random_seed)# Train the decision tree modelcls=DecisionTreeClassifier(random_state=random_seed)cls.fit(X_train,y_train)# Print the results: shape of the original dataset and the accuracy of decision tree classifier on original dataprint(X_train.shape,accuracy_score(y_test,cls.predict(X_test)),sep=': ')# Sample the data with random_seed setevo=EvoSampling(n_folds=3,random_seed=random_seed)X_resampled,y_resampled=evo.fit_resample(X_train,y_train)# Fit the decision tree modelcls.fit(X_resampled,y_resampled)# Print the results: shape of the original dataset and the accuracy of decision tree classifier on original dataprint(X_resampled.shape,accuracy_score(y_test,cls.predict(X_test)),sep=': ')Instance weightingSimple instance weighting examplefromsklearn.datasetsimportload_breast_cancerfromevopreprocess.data_weightingimportEvoWeighting# Load classification datadataset=load_breast_cancer()# Get weights for the instancesinstance_weights=EvoWeighting(random_seed=123).reweight(dataset.data,dataset.target)# Print the weights for instancesprint(instance_weights)Instance weighting with scikit-learnfromsklearn.datasetsimportload_breast_cancerfromsklearn.metricsimportaccuracy_scorefromsklearn.model_selectionimporttrain_test_splitfromsklearn.treeimportDecisionTreeClassifierfromevopreprocess.data_weightingimportEvoWeighting# Set the random seed for the reproducibilityrandom_seed=1234# Load classification datadataset=load_breast_cancer()# Split the dataset to training and testing setX_train,X_test,y_train,y_test=train_test_split(dataset.data,dataset.target,test_size=0.33,random_state=random_seed)# Train the decision tree model with custom instance weightscls=DecisionTreeClassifier(random_state=random_seed)cls.fit(X_train,y_train)# Print the results: shape of the original dataset and the accuracy of decision tree classifier on original dataprint(X_train.shape,accuracy_score(y_test,cls.predict(X_test)),sep=': ')# Get weights for the instancesinstance_weights=EvoWeighting(random_seed=random_seed).reweight(X_train,y_train)# Fit the decision tree modelcls.fit(X_train,y_train,sample_weight=instance_weights)# Print the results: shape of the original dataset and the accuracy of decision tree classifier on original dataprint(X_train.shape,accuracy_score(y_test,cls.predict(X_test)),sep=': ')Feature selectionSimple feature selection examplefromsklearn.datasetsimportload_breast_cancerfromevopreprocess.feature_selectionimportEvoFeatureSelection# Load classification datadataset=load_breast_cancer()# Print the size of datasetprint(dataset.data.shape)# Run feature selection with EvoFeatureSelectionX_new=EvoFeatureSelection().fit_transform(dataset.data,dataset.target)# Print the size of dataset after feature selectionprint(X_new.shape)Feature selection for regression with custom nature-inspired algorithm and other custom settingsfromsklearn.datasetsimportload_bostonfromsklearn.treeimportDecisionTreeRegressorimportniapy.algorithms.basicasniafromevopreprocess.feature_selectionimportEvoFeatureSelection# Load regression datadataset=load_boston()# Print the size of datasetprint(dataset.data.shape)# Run feature selection with custom settings and regression with EvoFeatureSelectionX_new=EvoFeatureSelection(evaluator=DecisionTreeRegressor(max_depth=2),optimizer=nia.DifferentialEvolution,random_seed=1,n_runs=5,n_folds=5,n_jobs=4).fit_transform(dataset.data,dataset.target)# Print the size of dataset after feature selectionprint(X_new.shape)Feature selection with scikit-learnfromsklearn.datasetsimportload_bostonfromsklearn.metricsimportmean_squared_errorfromsklearn.model_selectionimporttrain_test_splitfromsklearn.treeimportDecisionTreeRegressorfromevopreprocess.feature_selectionimportEvoFeatureSelection# Set the random seed for the reproducibilityrandom_seed=654# Load regression datadataset=load_boston()# Split the dataset to training and testing setX_train,X_test,y_train,y_test=train_test_split(dataset.data,dataset.target,test_size=0.33,random_state=random_seed)# Train the decision tree modelmodel=DecisionTreeRegressor(random_state=random_seed)model.fit(X_train,y_train)# Print the results: shape of the original dataset and the accuracy of decision tree regressor on original dataprint(X_train.shape,mean_squared_error(y_test,model.predict(X_test)),sep=': ')# Sample the data with random_seed setevo=EvoFeatureSelection(evaluator=model,random_seed=random_seed)X_train_new=evo.fit_transform(X_train,y_train)# Fit the decision tree modelmodel.fit(X_train_new,y_train)# Keep only selected feature on test setX_test_new=evo.transform(X_test)# Print the results: shape of the original dataset and the MSE of decision tree regressor on original dataprint(X_train_new.shape,mean_squared_error(y_test,model.predict(X_test_new)),sep=': ')EvoPreprocess as a part of the pipeline (from imbalanced-learn)fromimblearn.pipelineimportPipelinefromsklearn.datasetsimportload_breast_cancerfromsklearn.metricsimportaccuracy_scorefromsklearn.model_selectionimporttrain_test_splitfromsklearn.treeimportDecisionTreeClassifierfromevopreprocess.data_samplingimportEvoSamplingfromevopreprocess.feature_selectionimportEvoFeatureSelection# Set the random seed for the reproducibilityrandom_seed=1111# Load classification datadataset=load_breast_cancer()# Split the dataset to training and testing setX_train,X_test,y_train,y_test=train_test_split(dataset.data,dataset.target,test_size=0.33,random_state=random_seed)# Train the decision tree modelcls=DecisionTreeClassifier(random_state=random_seed)cls.fit(X_train,y_train)# Print the results: shape of the original dataset and the accuracy of decision tree classifier on original dataprint(X_train.shape,accuracy_score(y_test,cls.predict(X_test)),sep=': ')# Make scikit-learn pipeline with feature selection and data samplingpipeline=Pipeline(steps=[('feature_selection',EvoFeatureSelection(n_folds=10,random_seed=random_seed)),('data_sampling',EvoSampling(n_folds=10,random_seed=random_seed)),('classifier',DecisionTreeClassifier(random_state=random_seed))])# Fit the pipelinepipeline.fit(X_train,y_train)# Print the results: the accuracy of the pipelineprint(accuracy_score(y_test,pipeline.predict(X_test)))For more examples please look atExamplesfolder.AuthorEvoPreprocess was programmed and is maintained by Saso Karakatic from University of Maribor.CitingPlain formatKarakatic, S., (2020).
EvoPreprocess - Data Preprocessing Framework with Nature-Inspired Optimization Algorithms.
Mathematics, 8(6), p.900. <https://doi.org/10.3390/math8060900>Bibtex format@article{karakativc2020evopreprocess,
  title={EvoPreprocess—Data Preprocessing Framework with Nature-Inspired Optimization Algorithms},
  author={Karakati{\v{c}}, Sa{\v{s}}o},
  journal={Mathematics},
  volume={8},
  number={6},
  pages={900},
  year={2020},
  publisher={Multidisciplinary Digital Publishing Institute}
}LicenseThis project is licensed under the GNU General Public License v3.0 License - see <http://www.opensource.org/licenses/GNUGeneral Public License v3.0>.