Skip to content

Commit

Permalink
Updated libraries to newest versions.
Browse files Browse the repository at this point in the history
Stability fixes.
  • Loading branch information
karakatic committed Nov 21, 2020
1 parent 9df6278 commit c00d597
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion EvoPreprocess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

name = 'EvoPreprocess'
__project__ = 'EvoPreprocess'
__version__ = '0.3.1'
__version__ = '0.3.2'
__all__ = ['feature_selection', 'data_sampling', 'data_weighting']
13 changes: 6 additions & 7 deletions EvoPreprocess/data_sampling/SamplingBenchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import random

import numpy as np
import pandas as pd
from NiaPy.benchmarks import Benchmark
from sklearn.base import ClassifierMixin
from sklearn.metrics import mean_squared_error, f1_score
from sklearn.naive_bayes import GaussianNB
from sklearn.utils import safe_indexing
from sklearn.utils import safe_indexing, check_X_y


class SamplingBenchmark(Benchmark):
Expand Down Expand Up @@ -56,10 +55,7 @@ def __init__(self,
self.Upper = 1
super().__init__(self.Lower, self.Upper)

if isinstance(X, pd.DataFrame):
X = X.values
if isinstance(y, pd.Series):
y = y.values
X, y = check_X_y(X, y, force_all_finite=False)

self.X_train, self.X_valid = X[train_indices, :], X[valid_indices, :]
self.y_train, self.y_valid = y[train_indices], y[valid_indices]
Expand Down Expand Up @@ -97,7 +93,10 @@ def to_phenotype(genotype):
setting = np.cumsum(genotype[-5:])
appearances = genotype[:-5]

return np.digitize(appearances, setting / setting[-1])
if setting[-1] == 0:
return np.digitize(appearances, setting)
else:
return np.digitize(appearances, setting / setting[-1])

@staticmethod
def genotype_to_map(genotype):
Expand Down
16 changes: 10 additions & 6 deletions EvoPreprocess/data_weighting/WeightingBenchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
# License: GNU General Public License v3.0
import random

import pandas as pd
from NiaPy.benchmarks import Benchmark
from sklearn.base import ClassifierMixin
from sklearn.metrics import accuracy_score, mean_squared_error
from sklearn.naive_bayes import GaussianNB
from sklearn.utils import check_X_y


class WeightingBenchmark(Benchmark):
Expand Down Expand Up @@ -48,10 +48,7 @@ def __init__(self,
self.Upper = 2 # Max weight of the instance, should be tested
super().__init__(self.Lower, self.Upper)

if isinstance(X, pd.DataFrame):
X = X.values
if isinstance(y, pd.Series):
y = y.values
X, y = check_X_y(X, y, force_all_finite=False)

self.X_train, self.X_valid = X[train_indices], X[valid_indices]
self.y_train, self.y_valid = y[train_indices], y[valid_indices]
Expand All @@ -65,10 +62,17 @@ def __init__(self,

def function(self):
def evaluate(D, sol):
cls = self.evaluator.fit(self.X_train, self.y_train, sample_weight=sol)
weights = WeightingBenchmark.to_phenotype(sol, self.Upper)
cls = self.evaluator.fit(self.X_train, self.y_train, sample_weight=weights)
y_predicted = cls.predict(self.X_valid)
acc = self.metric(self.y_valid, y_predicted)
acc = (1 - acc) if self.evaluator is ClassifierMixin else acc
return acc

return evaluate

@staticmethod
def to_phenotype(sol, Upper):
max_value = sol[-1] / Upper
weights = sol[:-1]
return weights * max_value
7 changes: 2 additions & 5 deletions EvoPreprocess/feature_selection/FeatureSelectionBenchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import random

import numpy as np
import pandas as pd
from NiaPy.benchmarks import Benchmark
from sklearn.base import ClassifierMixin
from sklearn.metrics import f1_score
from sklearn.metrics import mean_squared_error
from sklearn.naive_bayes import GaussianNB
from sklearn.utils import check_X_y


class FeatureSelectionBenchmark(Benchmark):
Expand Down Expand Up @@ -58,10 +58,7 @@ def __init__(self,
super().__init__(self.Lower, self.Upper)

self.split = split
if isinstance(X, pd.DataFrame):
X = X.values
if isinstance(y, pd.Series):
y = y.values
X, y = check_X_y(X, y, force_all_finite=False)

self.X_train, self.X_valid = X[train_indices, :], X[valid_indices, :]
self.y_train, self.y_valid = y[train_indices], y[valid_indices]
Expand Down
14 changes: 9 additions & 5 deletions Examples/ExampleSamplingCustomOptimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ class RandomSearch(Algorithm):
Name = ['RandomSearch', 'RS']

def runIteration(self, task, pop, fpop, xb, fxb, **dparams):
pop = task.Lower + self.Rand.rand(self.NP, task.D) * task.bRange
fpop = apply_along_axis(task.eval, 1, pop)
return pop, fpop, {}
try:
pop = task.Lower + self.Rand.rand(self.NP, task.D) * task.bRange
fpop = apply_along_axis(task.eval, 1, pop)
xb, fxb = self.getBest(pop, fpop, xb, fxb)
return pop, fpop, xb, fxb, {}
except Exception as x:
print(x)
return None, None, None


class CustomSamplingBenchmark(SamplingBenchmark):
Expand All @@ -24,8 +29,7 @@ class CustomSamplingBenchmark(SamplingBenchmark):

def function(self):
def evaluate(D, sol):
phenotype = SamplingBenchmark.map_to_phenotype(
CustomSamplingBenchmark.to_phenotype(sol))
phenotype = SamplingBenchmark.map_to_phenotype(CustomSamplingBenchmark.to_phenotype(sol))
X_sampled = safe_indexing(self.X_train, phenotype)
y_sampled = safe_indexing(self.y_train, phenotype)

Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ These instructions will get you a copy of the project up and running on your loc
### Dependencies
EvoSampling requires:

- numpy(>=1.8.2)
- scipy(>=0.17)
- pandas
- scikit-learn(>=0.19.0)
- imbalanced-learn(>=0.3.1)
- NiaPy(>=2.0.0rc5)
- numpy
- scipy
- scikit-learn
- imbalanced-learn
- NiaPy(>=2.0.0rc11)

### Installation
Install EvoPreprocess with pip:
Expand Down
11 changes: 5 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
numpy>=1.8.2
pandas
scipy>=0.17
scikit-learn>=0.19.0
imbalanced-learn>=0.3.1
NiaPy>=2.0.0rc5
numpy
scipy
scikit-learn
imbalanced-learn
NiaPy>=2.0.0rc11
11 changes: 5 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ def read_package_variable(key, filename='__init__.py'):
'Topic :: Scientific/Engineering :: Information Analysis'
],
install_requires=[
'numpy>=1.8.2',
'pandas',
'scipy>=0.17',
'scikit-learn>=0.19.0'
'imbalanced-learn>=0.3.1'
'NiaPy>=2.0.0rc5'
'numpy',
'scipy',
'scikit-learn'
'imbalanced-learn'
'NiaPy>=2.0.0rc11'
],
keywords=[
'Evolutionary Algorithms',
Expand Down

0 comments on commit c00d597

Please sign in to comment.