Genetic 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: scipy in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from zoofs) (1.7.1) Requirement already satisfied: numpy in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from zoofs) (1.21.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: 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 [4]:
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 [5]:
from zoofs import GeneticOptimization
Setting up Objective function and fitting algo¶
In [6]:
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=GeneticOptimization(objective_function_topass,n_iteration=20,
population_size=20,selective_pressure=2,elitism=4,
mutation_rate=0.1,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.0005260710188630122 0.0005260710188630122 Iteration 1 0.0005260710188630122 0.0005260710188630122 Iteration 2 0.000510693921051442 0.000510693921051442 Iteration 3 0.000510693921051442 0.000510693921051442 Iteration 4 0.000505133084737278 0.000505133084737278 Iteration 5 0.000505133084737278 0.000505133084737278 Iteration 6 0.0004841698982551718 0.0004841698982551718 Iteration 7 0.0004841698982551718 0.0004841698982551718 Iteration 8 0.0004841698982551718 0.0004841698982551718 Iteration 9 0.0004841698982551718 0.0004841698982551718 Iteration 10 0.0004841698982551718 0.0004841698982551718 Iteration 11 0.0004841698982551718 0.0004841698982551718 Iteration 12 0.0004841698982551718 0.0004841698982551718 Iteration 13 0.0004841698982551718 0.0004841698982551718 Iteration 14 0.0004724901340559412 0.0004724901340559412 Iteration 15 0.0004724901340559412 0.0004724901340559412 Iteration 16 0.0004841698982551718 0.0004724901340559412 Iteration 17 0.0004841698982551718 0.0004724901340559412 Iteration 18 0.0004841698982551718 0.0004724901340559412 Iteration 19 0.0004841698982551718 0.0004724901340559412
Out[6]:
['mean radius', 'mean texture', 'mean perimeter', 'mean area', 'mean smoothness', 'mean compactness', 'mean concavity', 'mean concave points', 'mean symmetry', '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 area', 'worst smoothness', 'worst compactness', 'worst concavity', 'worst concave points', 'worst symmetry', 'worst fractal dimension']
Plotting the Results¶
In [7]:
algo_object.plot_history()