<jupyter_start><jupyter_text>========================================================  Hyperopt Optimization  ========================================================   Import Libraries<jupyter_code>import pandas as pd
import numpy as np
import hyperopt as hyp
from hyperopt import hp
from hyperopt import fmin
from hyperopt import tpe
from hyperopt import rand
from hyperopt import Trials
from hyperopt import STATUS_OK
import matplotlib.pyplot as plt
from tabulate import tabulate
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Define Objective Function<jupyter_code># Define objective function
def f(x):
    return {'loss': x ** 2 - x, 'status': STATUS_OK}

space = hp.uniform('x', -1, 1)<jupyter_output><empty_output><jupyter_text>Run Hyperopt Optimization<jupyter_code># Run hyperopt optimization
trials1 = Trials()
result1 = fmin(
    fn=f,                           # objective function
    space=space,   # parameter space
    algo=tpe.suggest,               # surrogate algorithm
    max_evals=100,                  # no. of evaluations
    trials=trials1                   # trials object that keeps track of the sample results (optional)
)

# Print the optimized parameters
print("\n\n\n==========================================")
print("Result: ",result1)
print("Evalution: ",hyp.space_eval(space, result1))
print("==========================================\n\n\n")<jupyter_output>100%|██████████| 100/100 [00:00<00:00, 127.88trial/s, best loss: -0.24999969469460895]



==========================================
Result:  {'x': 0.49944745553025577}
Evalution:  0.49944745553025577
==========================================<jupyter_text>Plot Loss<jupyter_code># Extract and plot the trials 
print("\n\n\n===============")
print("     Loss")
print("===============\n\n\n")

x1 = trials1.vals['x']
y1 = [x['loss'] for x in trials1.results]
plt.scatter(x1, y1)
plt.show()<jupyter_output>===============
     Loss
===============<jupyter_text>Run Hyperopt Optimization<jupyter_code># Run hyperopt optimization
trials2 = Trials()
result2 = fmin(
    fn=f,                           # objective function
    space=space,   # parameter space
    algo=rand.suggest,               # surrogate algorithm
    max_evals=100,               # no. of evaluations
    trials=trials2                   # trials object that keeps track of the sample results (optional)
)

# Print the optimized parameters
print("\n\n\n==========================================")
print("Result: ",result2)
print("Evalution: ",hyp.space_eval(space, result2))
print("==========================================\n\n\n")<jupyter_output>100%|██████████| 100/100 [00:00<00:00, 497.51trial/s, best loss: -0.24996535732000008]



==========================================
Result:  {'x': 0.5058858032586833}
Evalution:  0.5058858032586833
==========================================<jupyter_text>Show Results in Table<jupyter_code>tid = []
state = []
vals = []
function_loss = []
algo = []
time = []
n_of_trials_collection = []
for i in range(50):
    n_of_trials = abs(int(np.round(np.random.randn(1)*5 + 10))) + 10
    n_of_trials_collection.append(n_of_trials)
    tid.append(trials1.trials[i]['tid'])
    state.append(trials1.trials[i]['state'])
    function_loss.append(round(trials1.trials[i]['result']["loss"],4))
    vals.append(round(abs(trials1.trials[i]['misc']["vals"]["x"][0]),4))
    algo.append("algo = tpe.suggest, eval = 100")
    time.append(trials1.trials[i]['book_time'])
    
df = pd.DataFrame()
df["number of trials"] = n_of_trials_collection
df["number of parameters"] = state
df["type of function"] = 'power'
df["Accuracy"] = vals
df["Time (sec)"] = time
df["type of opt"] = 'hyper'

df['Time (sec)'] = (df['Time (sec)'] - df['Time (sec)'].shift())
my_times = []
for t in df['Time (sec)']:
    my_times.append(t.total_seconds())
df['Time (sec)'] = my_times
df['Time (sec)'].iloc[0] = round(np.mean(my_times[1:]),3)
pd.set_option("display.max_rows", None, "display.max_columns", None)
#print(tabulate(df, df.columns, tablefmt="pretty",showindex="never"))
table1 = df.copy()
results = table1
results.to_csv('Hyperopt.csv')
print(tabulate(results, results.columns, tablefmt="pretty",showindex="never"))<jupyter_output>+------------------+----------------------+------------------+----------+------------+-------------+
| number of trials | number of parameters | type of function | Accuracy | Time (sec) | type of opt |
+------------------+----------------------+------------------+----------+------------+-------------+
|        13        |          2           |      power       |  0.0904  |   0.006    |    hyper    |
|        21        |          2           |      power       |  0.1786  |   0.004    |    hyper    |
|        31        |          2           |      power       |  0.3394  |   0.003    |    hyper    |
|        15        |          2           |      power       |  0.2733  |   0.002    |    hyper    |
|        23        |          2           |      power       |  0.8805  |   0.004    |    hyper    |
|        19        |          2           |      power       |  0.0192  |   0.004    |    hyper    |
|        17        |          2           |      power       |  0.0729  |   0.003    |    h[...]
{'fraction_non_alphanumeric': 0.25879444761361475, 'fraction_numerical': 0.05381251188438867, 'mean_word_length': 2.7491090520313612, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '366672', 'license': 'MIT', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1616, 'n_words': 345, 'path': 'black_box_optimization/data_generators/Hyperopt.ipynb', 'repo_name': 'Optimistic-Optimizers/Black-Box-Optimization', 'size': '5259'}