Particle Swarm Optimization¶
Installation¶
In [1]:
!pip install zoofs
Requirement already satisfied: zoofs in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (0.1.2) Requirement already satisfied: plotly in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from zoofs) (5.3.1) Requirement already satisfied: pandas in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from zoofs) (1.3.2) Requirement already satisfied: numpy in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from zoofs) (1.21.2) Requirement already satisfied: scipy in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from zoofs) (1.7.1) Requirement already satisfied: pytz>=2017.3 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from pandas->zoofs) (2021.1) Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from pandas->zoofs) (2.8.2) Requirement already satisfied: six>=1.5 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from python-dateutil>=2.7.3->pandas->zoofs) (1.16.0) Requirement already satisfied: tenacity>=6.2.0 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from plotly->zoofs) (8.0.1)
WARNING: You are using pip version 21.1.1; however, version 21.2.4 is available. You should consider upgrading via the 'c:\users\user\appdata\local\programs\python\python39\python.exe -m pip install --upgrade pip' command.
Load Breast cancer dataset¶
In [2]:
from sklearn.datasets import load_breast_cancer
import pandas as pd
data = load_breast_cancer()
X_train=pd.DataFrame(data['data'],columns=data['feature_names'])
y_train=pd.Series(data['target'])
Importing Zoofs Algo¶
In [3]:
from zoofs import ParticleSwarmOptimization
Setting up Objective function and fitting algo¶
In [4]:
from sklearn.metrics import log_loss
# define your own objective function, make sure the function receives four parameters,
# fit your model and return the objective value !
def objective_function_topass(model,X_train, y_train, X_valid, y_valid):
model.fit(X_train,y_train)
P=log_loss(y_valid,model.predict_proba(X_valid))
return P
# create object of algorithm
algo_object=ParticleSwarmOptimization(objective_function_topass,n_iteration=25,
population_size=20,minimize=True)
import lightgbm as lgb
lgb_model = lgb.LGBMClassifier()
# fit the algorithm
algo_object.fit(lgb_model,X_train, y_train, X_train, y_train,verbose=True)
#plot your results
Best value of metric across iteration Best value of metric across population Iteration 0 0.0005296967375056357 0.0005296967375056357 Iteration 1 0.0005350875802894996 0.0005296967375056357 Iteration 2 0.0004900565872079092 0.0004900565872079092 Iteration 3 0.00048332067099894437 0.00048332067099894437 Iteration 4 0.00045637897622823905 0.00045637897622823905 Iteration 5 0.000466775244080281 0.00045637897622823905 Iteration 6 0.0004918998489345662 0.00045637897622823905 Iteration 7 0.0004921178797640744 0.00045637897622823905 Iteration 8 0.00042340845980268525 0.00042340845980268525 Iteration 9 0.00042474680152597216 0.00042340845980268525 Iteration 10 0.0003983627188019335 0.0003983627188019335 Iteration 11 0.0004249661758924971 0.0003983627188019335 Iteration 12 0.0004264664684658149 0.0003983627188019335 Iteration 13 0.00046151713507627454 0.0003983627188019335 Iteration 14 0.0004123962375142004 0.0003983627188019335 Iteration 15 0.00043259241757591187 0.0003983627188019335 Iteration 16 0.00039191564861833434 0.00039191564861833434 Iteration 17 0.0004517608567341095 0.00039191564861833434 Iteration 18 0.00040691262960006647 0.00039191564861833434 Iteration 19 0.0004248262025278297 0.00039191564861833434 Iteration 20 0.0004102547126767602 0.00039191564861833434 Iteration 21 0.00041051318455042024 0.00039191564861833434 Iteration 22 0.00037769760489395467 0.00037769760489395467 Iteration 23 0.0004249105077075491 0.00037769760489395467 Iteration 24 0.000432562624556134 0.00037769760489395467
Out[4]:
['mean perimeter', 'mean area', 'mean smoothness', 'mean concavity', 'mean concave points', 'mean fractal dimension', 'radius error', 'texture error', 'perimeter error', 'area error', 'smoothness error', 'compactness error', 'concavity error', 'concave points error', 'symmetry error', 'fractal dimension error', 'worst radius', 'worst texture', 'worst perimeter', 'worst smoothness', 'worst compactness', 'worst symmetry', 'worst fractal dimension']
Plotting the Results¶
In [5]:
algo_object.plot_history()