# Optimal combination of variables to minimise output

To be honest I'm not 100% sure how much this is purely a coding issue or a data science issue, but I'll take my chances. I've developed a matrix which is a mixture of various hyperparameters, the objective of these hyperparameters is to minimise the values of a certain output, my aim is to have two combinations: 1 that minimises the output to a value below 10, and another that gives me the lowest output possible.
I can find these values manually using a simple correlation matrix, or just playing around looking at the values, but I'm having problems when trying to come up with ideas as to how make this an automatic process with Python code.

To give an example what the data looks like:

| Parameter 1 | Parameter 2 | Parameter 3 | Parameter 4 | Output |
|--------------|--------------|---------------|--------------|----------|
| 0.5_________ | 1___________ | 0.003_______ | 5___________ | 10 _____ |
| 0.5_________ | 1___________ | 0.003_______ | 15__________ | 8 ______ |
| 0.5_________ | 1___________ | 0.003_______ | 20__________ | 5 ______ |
| 0.5_________ | 1___________ | 0.005_______ | 5___________ | 45 _____ |
| 0.5_________ | 1___________ | 0.005_______ | 15__________ | 23 _____ |
| 0.5_________ | 1___________ | 0.005_______ | 20__________ | 235 ____ |
| 0.5_________ | 3___________ | 0.003_______ | 5___________ | 9 ______ |
| 0.5_________ | 3___________ | 0.003_______ | 15__________ | 7 ______ |
| 0.5_________ | 3___________ | 0.003_______ | 20__________ | 10 _____ |
| 0.5_________ | 3___________ | 0.005_______ | 5___________ | 45 _____ |
| 0.5_________ | 3___________ | 0.005_______ | 15__________ | 150 ____ |
| 0.5_________ | 3___________ | 0.005_______ | 20__________ | 85 _____ |
| 0.1_________ | 1___________ | 0.003_______ | 5___________ | 2 ______ |
| 0.1_________ | 1___________ | 0.003_______ | 15__________ | 3 ______ |
| 0.1_________ | 1___________ | 0.003_______ | 20__________ | 4 ______ |
| 0.1_________ | 1___________ | 0.005_______ | 5___________ | 15 _____ |
| 0.1_________ | 1___________ | 0.005_______ | 15__________ | 11 _____ |
| 0.1_________ | 1___________ | 0.005_______ | 20__________ | 17 _____ |
| 0.1_________ | 3___________ | 0.003_______ | 5___________ | 3 ______ |
| 0.1_________ | 3___________ | 0.003_______ | 15__________ | 1 ______ |
| 0.1_________ | 3___________ | 0.003_______ | 20__________ | 4 ______ |
| 0.1_________ | 3___________ | 0.005_______ | 5___________ | 75 _____ |
| 0.1_________ | 3___________ | 0.005_______ | 15__________ | 250 ____ |
| 0.1_________ | 3___________ | 0.005_______ | 20__________ | 95 _____ |

So, you can see that parameters 1 and 3 are significant, as smaller values in those parameters correlate with a smaller value in the Output, which is easily noticeable when working on this manually. However, I've got no idea how to automatise this in Python. I don't know if this purely a coding issue, or if this involves using some specific library/model to be able to get the lowest output values.

If anyone could guide me with this, I would be quite grateful.

Here's a sample file: Sample data

Thanks in advance.

## 1 Answer

Imports and load data:

import pandas as pd
data = pd.read_csv('sample_parameters.csv')


To get the rows/parameters below 10, you can filter as follows:

rows_below10 = data[data.Output < 10]


The rows that yield the smallest Output can be obtained as follows (there are multiple rows that meet this condition):

min_rows = data[data.Output == data.Output.min()]


Visualisation of the first 3000 rows, sorted by Output (black line demarcates Output > 10.

#Visualise
from matplotlib import pyplot as plt

f, axs = plt.subplots(ncols=data.shape[1], figsize=(8, 4), layout='tight')

data_sorted = data.sort_values(by='Output')[:3000]
for col, ax in zip(data_sorted.columns, axs):
im = ax.matshow(data_sorted[[col]].values, aspect='auto', cmap='OrRd')
ax.axis('off')
ax.set_title(col, fontsize=8)

cb = f.colorbar(mappable=im, ax=axs[-1], label='output')
f.subplots_adjust(wspace=0.1)
f.suptitle(f'First {len(data_sorted)} rows (sorted)')

#Mark the location of Output > 10
axs[-1].axhline(
np.argwhere(data_sorted.Output < 10)[-1],
color='black', linewidth=2, linestyle=':'
)


The scikit-learn library has functionality for automatically selecting/tuning hyperparameters. It has options like GridSearchCV for running a grid search over all possible hyperparameter combinations, and RandomizedSearchCV for sampling hyperparameter combinations and scoring them. There are other schemes too.

These methods use cross-validation as a way of evaluating each hyperparameter set. CV trains each model on a training split, scores it the val split, and repeats this n_splits times to obtain a robust average. This is done automatically for each hyperparameter being scored.

Two examples from the sklearn examples page: 1, 2.

• If you could upload some sample or synthetic data I might be able to code up an example with it. Commented May 20 at 13:07
• I've added a link with sample data. I'll be very grateful for your example. @muhammedyunus Commented May 20 at 13:51
• Looking at the data, it seems like you have already tried the parameters, and you want to identify the best combination? My answer was about automating the whole process, including generating the parameter combinations. I will take a look at the data and let you know if I find a solution. Commented May 20 at 13:54
• Precisely, I'm sorry if I didn't explain myself properly. I've already identified the parameters, and what I need is to find: 1) the combinations of parameters in order to select the Output values with 10 or less and 2) the combination of parameters that would lead to the lowest output value. Commented May 20 at 13:59
• Thank you! This is really helpful!!! @muhammedyunus Commented May 20 at 14:42