예제 #1
0
def test_coord_history_correct_dimension():
    """Test if the saved particles coordinate history has the correct dimensions."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.optimize(params, iterations=20)

    history = pso.coords_history

    assert len(history) == 21
    assert all(history[i].shape == (20, 2) for i in range(20))
예제 #2
0
# !/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

from mlopt.optimization import ParticleSwarmOptimizer
from examples.visualization.gif import create_gif


def ackley_func(x, y):
    return -20*np.exp(-0.2*np.sqrt(0.5*(x**2+y**2)))-np.exp(0.5*(np.cos(2*np.pi*x)+np.cos(2*np.pi*y)))+np.exp(1)+20


if __name__ == '__main__':

    linspace = np.linspace(-4.0, 4.0, 80)

    bso = ParticleSwarmOptimizer(func=ackley_func, maximize=False, particles=20)
    bso.optimize(params={'x': (-4.0, 4.0), 'y': (-4.0, 4.0)}, inertia=0.8, c_cog=2, c_soc=2,
                 learning_rate=0.01, random_state=None, iterations=300)
    lst_coords = bso.coords_history

    create_gif(obj_func=ackley_func, linspace=linspace, coords=lst_coords, xlim=(-4, 4), ylim=(-4, 4))
예제 #3
0
from mlopt.optimization import ParticleSwarmOptimizer


if __name__ == '__main__':
    iris = load_iris()

    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
    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])))