.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "getting_started/auto_examples/plot_VHCT_Landmine.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_getting_started_auto_examples_plot_VHCT_Landmine.py: Real-life Data ============================ In this example, we run the VHCT algorithm to tune hyperparameters for Support Vector Machine (SVM) .. GENERATED FROM PYTHON SOURCE LINES 7-19 .. code-block:: default from PyXAB.algos.VHCT import VHCT from PyXAB.partition.BinaryPartition import BinaryPartition from PyXAB.utils.plot import plot_regret # Useful functions and classes for the learning process from sklearn import svm from sklearn.metrics import roc_auc_score import numpy as np import pickle .. GENERATED FROM PYTHON SOURCE LINES 20-22 We first define the objective as maximizing the ROC_AUC score on the testing dataset after training the SVM using the hyperparameters with the training dataset. .. GENERATED FROM PYTHON SOURCE LINES 22-44 .. code-block:: default class obj_func_landmine(): def __init__(self, X_train, Y_train, X_test, Y_test): self.X_train = X_train # Training X self.Y_train = Y_train # Training Y self.X_test = X_test # Testing X self.Y_test = Y_test # Testing Y self.fmax = 1 def f(self, point): C = point[0] # First parameter gam = point[1] # Second parameter clf = svm.SVC(kernel="rbf", C=C, gamma=gam, probability=True) # The machine learning model is SVM clf.fit(self.X_train, self.Y_train) # Fit the model using training data pred = clf.predict_proba(self.X_test) # Make prediction on the testing score = roc_auc_score(self.Y_test, pred[:, 1]) # The reward is the ROC_AUC score return score .. GENERATED FROM PYTHON SOURCE LINES 45-47 Input the data and then split the data into the training dataset and the testing dataset. Then define the objective function using the datasets. .. GENERATED FROM PYTHON SOURCE LINES 47-59 .. code-block:: default landmine_data = pickle.load(open("../../PyXAB/landmine/landmine_formated_data.pkl", "rb")) all_X_train, all_Y_train, all_X_test, all_Y_test = landmine_data["all_X_train"], landmine_data["all_Y_train"], \ landmine_data["all_X_test"], landmine_data["all_Y_test"] X_train = all_X_train[0] Y_train = np.squeeze(all_Y_train[0]) X_test = all_X_test[0] Y_test = np.squeeze(all_Y_test[0]) target = obj_func_landmine(X_train=X_train, X_test=X_test, Y_train=Y_train, Y_test=Y_test) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Define the number of rounds, the domain, the partition, and the algorithm for the learning process .. GENERATED FROM PYTHON SOURCE LINES 61-72 .. code-block:: default T = 500 domain = [[1e-4, 10.0], [1e-2, 10.0]] partition = BinaryPartition algo = VHCT(domain=domain, rho=0.5, partition=partition) # To plot the regret, we can initialize the cumulative regret and the cumulative regret list cumulative_regret = 0 cumulative_regret_list = [] .. GENERATED FROM PYTHON SOURCE LINES 73-75 In each iteration of the learning process, the algorithm calls the ``pull(t)`` function to obtain a point, and then the reward for the point is returned to the algorithm by calling ``receive_reward(t, reward)``. .. GENERATED FROM PYTHON SOURCE LINES 75-88 .. code-block:: default for t in range(1, T+1): point = algo.pull(t) reward = target.f(point) algo.receive_reward(t, reward) inst_regret = target.fmax - target.f(point) cumulative_regret += inst_regret cumulative_regret_list.append(cumulative_regret) # plot the regret plot_regret(np.array(cumulative_regret_list), name='VHCT') .. image-sg:: /getting_started/auto_examples/images/sphx_glr_plot_VHCT_Landmine_001.png :alt: plot VHCT Landmine :srcset: /getting_started/auto_examples/images/sphx_glr_plot_VHCT_Landmine_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.222 seconds) .. _sphx_glr_download_getting_started_auto_examples_plot_VHCT_Landmine.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_VHCT_Landmine.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_VHCT_Landmine.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_