<jupyter_start><jupyter_text>Custom design of experiments (custom DOE)Reference:    https://datachemeng.com/designofexperimentscodes/<jupyter_code>import numpy as np
import pandas as pd
from numpy import matlib
import matplotlib.pyplot as plt
import seaborn as sns
import itertools

%matplotlib inline<jupyter_output>C:\Users\mmm\Anaconda3\lib\site-packages\statsmodels\tools\_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>Determine variables<jupyter_code># Experiment conditions
variables = {'var_1': [100.0, 150.0, 200.0],        # process 1 temperature  (example)
             'var_2': [1.0, 2.0, 3.0],              # process 1 time         (example)
             'var_3': [300.0, 350.0, 400.0],        # process 2 temperature  (example)
             'var_4': [2.0, 2.5, 3.0],              # process 2 time         (example)
             'var_5': [200.0, 225.0, 250.0],        # process 3 temperature  (example)
             }
# Make experiment condition dictionary for future use
val_name = variables.keys()
process_name = ['temp_1', 'time_1', 'temp_2', 'time_2', 'cooling_rate', 'additive']
process_cond = dict(zip(val_name, process_name))
process_cond<jupyter_output><empty_output><jupyter_text>Make all possible experiments for later random search<jupyter_code>all_experiments = list(itertools.product(variables['var_1'], variables['var_2'], variables['var_3'],
                                         variables['var_4'], variables['var_5']))

print('Number of all possible combinations = ', len(all_experiments))
# Convert to DataFrame for future use
all_experiments_df = pd.DataFrame(all_experiments)
all_experiments_df.columns = process_cond.values()

print('Shape of the DataFrame', all_experiments_df.shape)
all_experiments_df.head()<jupyter_output>Shape of the DataFrame (243, 5)<jupyter_text>Now, preparation for experiment data is done. DOE<jupyter_code># DOE preparation
np.random.seed(111)                                     # fix random seed
experiment_index = np.arange(len(all_experiments_df))
number_of_random_searches = 10000                       # iteration to find better combinations
number_of_experiments = 12                              # number of experiments that can be conducted
# Standardization function
def auto_scale(data_frame):
    autoscaled_all_experiments = (data_frame - data_frame.mean(axis=0)) / data_frame.std(axis=0)
    return np.array(autoscaled_all_experiments)<jupyter_output><empty_output><jupyter_text>DOE optimal type = [1] Maximize determinant of dot product (X.T, X)<jupyter_code>#del(best_optimal_monitor, timing_of_best)

best_optimal_monitor = []
timing_of_best = []
for random_search_number in range(number_of_random_searches):
    new_selected_experiment_numbers = np.random.randint(0, len(all_experiments_df), number_of_experiments)
    new_selected_experiments = all_experiments_df.loc[new_selected_experiment_numbers]
    autoscaled_new_selected_experiments = auto_scale(new_selected_experiments)
    X = autoscaled_new_selected_experiments
    
    optimal_value = np.linalg.det(np.dot(X.T, X))
               
    if random_search_number == 0:
        best_optimal_value = optimal_value
        selected_experiment_indexes = new_selected_experiment_numbers.copy()
        
    if best_optimal_value < optimal_value:
        best_optimal_value = optimal_value
        selected_experiment_indexes = new_selected_experiment_numbers.copy()
        print(random_search_number, '\t', best_optimal_value)
        
        best_optimal_monitor.append(best_optimal_value)
        timing_of_best.append(random_search_number)
        
    continue

final_selected_experiments = all_experiments_df.loc[selected_experiment_indexes]
plt.plot(timing_of_best, best_optimal_monitor)
plt.xlabel('Times that the max value appeared in iteration')
plt.ylabel('Max determinant of dot product (X.T, X)')
plt.show()
colormap = 'Spectral'
plt.figure(figsize=(6, 6))
plt.title('Correlations of Features', y=1.05, size=15)
sns.heatmap(final_selected_experiments.astype(float).corr(), linewidths = 0.1, vmax = 1.0, vmin = -1.0,
            square=True, cmap=colormap, linecolor='white', annot = True)
plt.ylim(final_selected_experiments.astype(float).corr().shape[0], 0)
plt.show()
x = np.corrcoef(final_selected_experiments.transpose()).flatten()
final_selected_experiments.to_csv('final_selected_experiments_type[1].csv')
corr = [-1.0 if i > 0.95 else np.abs(i) for i in x]
np.max(corr)<jupyter_output><empty_output><jupyter_text>DOE optimal type = [2] Minimize sum of diagonal of inverse matrix of dot product (X.T, X)<jupyter_code>del best_optimal_monitor, timing_of_best

best_optimal_monitor = []
timing_of_best = []
for random_search_number in range(number_of_random_searches):
    new_selected_experiment_numbers = np.random.randint(0, len(all_experiments_df), number_of_experiments)
    new_selected_experiments = all_experiments_df.loc[new_selected_experiment_numbers]
    autoscaled_new_selected_experiments = auto_scale(new_selected_experiments)
    X = autoscaled_new_selected_experiments
    
    optimal_value = np.sum(np.trace(np.linalg.inv(np.dot(X.T, X))))
               
    if random_search_number == 0:
        best_optimal_value = optimal_value
        selected_experiment_indexes = new_selected_experiment_numbers.copy()
        
    if best_optimal_value > optimal_value:
        best_optimal_value = optimal_value
        selected_experiment_indexes = new_selected_experiment_numbers.copy()
        print(random_search_number, '\t', best_optimal_value)
        
        best_optimal_monitor.append(best_optimal_value)
        timing_of_best.append(random_search_number)
        
    continue
    
final_selected_experiments = all_experiments_df.loc[selected_experiment_indexes]
plt.plot(timing_of_best, best_optimal_monitor)
plt.xlabel('Times that the min value appeared in iteration')
plt.ylabel(' Min sum of diag of inv of dot product (X.T, X)')
plt.show()
colormap = 'Spectral'
plt.figure(figsize=(6, 6))
plt.title('Correlations of Features', y=1.05, size=15)
sns.heatmap(final_selected_experiments.astype(float).corr(), linewidths = 0.1, vmax = 1.0, vmin = -1.0,
            square=True, cmap=colormap, linecolor='white', annot = True)
plt.ylim(final_selected_experiments.astype(float).corr().shape[0], 0)
plt.show()
x = np.corrcoef(final_selected_experiments.transpose()).flatten()
final_selected_experiments.to_csv('final_selected_experiments_type[2].csv')
corr = [-1.0 if i > 0.95 else np.abs(i) for i in x]
np.max(corr)<jupyter_output><empty_output><jupyter_text>Check counting the same raws between [1] and [2]<jupyter_code>final_1 = pd.read_csv('final_selected_experiments_type[1].csv')
final_2 = pd.read_csv('final_selected_experiments_type[2].csv')

df_same = pd.merge(final_1.iloc[:, 1:7], final_2.iloc[:, 1:7], how = 'inner')
print('The identical raws')
df_same<jupyter_output>The identical raws<jupyter_text>APPENDIX - comparison with orthogonal array and other optimal types<jupyter_code># See correlations in L18 orthogonal array for example
orthogonal = pd.read_csv('L18_example.csv')
orthogonal = orthogonal.iloc[:, 1:9]
orthogonal.head()
colormap = 'Spectral'
plt.figure(figsize=(6, 6))
plt.title('Correlations of Features', y=1.05, size=15)
sns.heatmap(orthogonal.astype(float).corr(), linewidths = 0.1, vmax = 1.0, vmin = -1.0,
            square=True, cmap=colormap, linecolor='white', annot = True)
plt.ylim(orthogonal.astype(float).corr().shape[0], 0)
plt.show()<jupyter_output><empty_output><jupyter_text>DOE optimal type = [3] Maximize minimum value of eigenvalue of dot product (X.T, X)<jupyter_code>del best_optimal_monitor, timing_of_best

best_optimal_monitor = []
timing_of_best = []
for random_search_number in range(number_of_random_searches):
    new_selected_experiment_numbers = np.random.randint(0, len(all_experiments_df), number_of_experiments)
    new_selected_experiments = all_experiments_df.loc[new_selected_experiment_numbers]
    autoscaled_new_selected_experiments = auto_scale(new_selected_experiments)
    X = autoscaled_new_selected_experiments
    
    w, v = np.linalg.eig(np.dot(X.T, X))
    optimal_value = np.min(w)
               
    if random_search_number == 0:
        best_optimal_value = optimal_value
        selected_experiment_indexes = new_selected_experiment_numbers.copy()
        
    if best_optimal_value < optimal_value:
        best_optimal_value = optimal_value
        selected_experiment_indexes = new_selected_experiment_numbers.copy()
        print(random_search_number, '\t', best_optimal_value)
        
        best_optimal_monitor.append(best_optimal_value)
        timing_of_best.append(random_search_number)
        
    continue
    
final_selected_experiments = all_experiments_df.loc[selected_experiment_indexes]
plt.plot(timing_of_best, best_optimal_monitor)
plt.xlabel('Times that the min value appeared in iteration')
plt.ylabel('Max min value of eig of dot product (X.T, X)')
plt.show()
colormap = 'Spectral'
plt.figure(figsize=(6, 6))
plt.title('Correlations of Features', y=1.05, size=15)
sns.heatmap(final_selected_experiments.astype(float).corr(), linewidths = 0.1, vmax = 1.0, vmin = -1.0,
            square=True, cmap=colormap, linecolor='white', annot = True)
plt.ylim(final_selected_experiments.astype(float).corr().shape[0], 0)
plt.show()
x = np.corrcoef(final_selected_experiments.transpose()).flatten()
corr = [-1.0 if i > 0.95 else np.abs(i) for i in x]
np.max(corr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10619007919966653, 'fraction_numerical': 0.01979991663192997, 'mean_word_length': 4.68542654028436, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '151859', 'license': 'MIT', 'n_tokens_mistral': 3399, 'n_tokens_neox': 3177, 'n_words': 609, 'path': 'Custom_design_of_experiments.ipynb', 'repo_name': 'dropbyakiba/Bayesian-Optimization', 'size': '9596'}