#!/usr/bin/env python # coding: utf-8 # # Particle Swarm Optimization # # Installation # In[1]: get_ipython().system('pip install zoofs') # # 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 # # Plotting the Results # In[5]: algo_object.plot_history()