I tried to search it but with poor result.
Can someone please explain to me how to perform optimize.minimize on Pandas DataFrame so minimized is the error between categories in DataFrame and result column
Consider this example:
import pandas as pd
df = pd.DataFrame({'prod': ['prod1', 'prod2', 'prod3', 'prod4', 'prod5', 'prod6'],
'cat': ['cat1', 'cat1', 'cat2', 'cat2', 'cat3', 'cat1'],
'dog': ['dog1', 'dog2', 'dog1', 'dog2', 'dog2', 'dog3'],
'result': [20, 10, 30, 50, 45, 120]})
for each cat1, cat2, cat3, dog1, dog2 and dog3 I want to find values that minimize this equation:
import numpy as np
np.average(np.abs(df['result'] - ('min for values in cat column * min for values in dog column'))) / np.average(df['result'])
I am able to replicate this in Excel using Solver
prod cat dog result cat*dog abs
prod1 cat1 dog1 20 17.38 2.61
prod2 cat1 dog2 10 27.34 17.35
prod3 cat2 dog1 30 26.91 3.09
prod4 cat2 dog2 50 42.32 7.67
prod5 cat3 dog2 45 45.00 0.00
prod6 cat1 dog3 120 20.64 99.36
so the end score that I am trying to find is:
average abs of 22 / average result of 45.83 = 0.47
These are the values Solver returned for animals:
cat1 3.59194254
cat2 5.559980313
cat3 5.91078751
dog1 4.840109868
dog2 7.613201994
dog3 5.746396256
How do I replicate this in Python?