Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_update_monotonic_best_scores_minimize():
"""Test if each particle of the particle swarm optimizer monotonically converges for minimization problems."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
scores = {p: [pso._score_all[p]] for p in range(20)}
for i in range(100):
pso.update(params)
for particle in range(20):
scores[particle] = scores[particle] + [pso._score_all[particle]]
assert all(all(scores[particle][i+1] <= scores[particle][i] for i in range(len(scores[particle])-1))
for particle in range(20))
def test_init_correct_dimensions_best_coords():
"""Test if the initialized best coordinates of each particle have the correct dimensions."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
assert pso._best_coords_all.shape == (20, 2)
def test_update_monotonic_best_score_glob_minimize():
"""Test if the particle swarm optimizer monotonically converges for minimization problems."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
scores = [pso.score]
for i in range(100):
pso.update(params)
scores.append(pso.score)
assert all(scores[i+1] <= scores[i] for i in range(len(scores)-1))
def test_init_correct_dimensions_best_score_glob():
"""Test if the initialized best score of all particles have the correct dimension."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
print('best score', pso.score)
assert np.shape(pso.score) == ()
def test_init_different_random_state():
"""Test if the initialized coordinates are not deterministic if random state is not fixed."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
coords0 = pso._coords_all
pso.init(params=params, random_state=2)
coords1 = pso._coords_all
assert any(val0 != val1 for row0, row1 in zip(coords0, coords1) for val0, val1 in zip(row0, row1))
def test_init_correct_dimensions_best_scores():
"""Test if the initialized best scores of each particle have the correct dimensions."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
assert len(pso._score_all) == 20
def test_init_correct_dimensions_best_coords_glob():
"""Test if the initialized best coordinates of all particles combined have the correct dimensions."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
assert pso.coords.shape == (2,)
def test_init_correct_dimensions_velocities():
"""Test if the initialized velocities have the correct dimension."""
pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)
params = {'x': (-1, 1), 'y': (-1, 1)}
pso.init(params=params, random_state=1)
assert pso._velocities.shape == (20, 2)
df = df.loc[(df.species == 'virginica') | (df.species == 'versicolor'), :]
df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75
X_train, X_test = df[df['is_train'] == True], df[df['is_train'] == False]
features = df.columns[:4]
y_train, y_test = pd.factorize(X_train['species'])[0], pd.factorize(X_test['species'])[0]
def get_score(max_depth, min_samples_leaf):
clf = RandomForestClassifier(random_state=1, max_depth=int(max_depth), min_samples_leaf=int(min_samples_leaf))
clf.fit(X_train[features], y_train)
preds_test = clf.predict_proba(X_test[features])[:, 1]
score = roc_auc_score(y_test, preds_test)
print('AUC: {:0.4f}, max depth: {:0.0f}, min_samples_leaf: {:0.0f}'.format(score, max_depth, min_samples_leaf))
return score
bso = ParticleSwarmOptimizer(func=get_score, maximize=True, particles=10)
params = {'max_depth': (1, 20), 'min_samples_leaf': (1, 20)}
bso.optimize(params=params, random_state=1, iterations=10)
print('Best AUC: {:0.4f}, max_depth: {}, min_samples_leaf: {}'.format(bso.score,
int(bso.coords[0]),
int(bso.coords[1])))