Problems in python pandas

prashant.mudgal at accenture.com prashant.mudgal at accenture.com
Tue Sep 16 20:08:39 CEST 2014


Hi All,

I am having some problem in python.

I have written the code
import pandas as pd
import numpy as np
from scipy.optimize import minimize
pd.set_option('display.mpl_style', 'default')
"""Read the input file into the dataframe""" """T1 file contains the decision variables, corresponding investments and their upper and lower bounds"""
df = pd.DataFrame.from_csv('C:\Users\prashant.mudgal\Downloads\T1.csv')
"""T2 file contains the parameters for the decision variables, note that there can be 2 or more entries for the same decison variable""" """ Our objective func will be prep from T2 file , it will be of the form sum (all DV in params file) (a*(b + c * DV_name)** power) """
df2 = pd.DataFrame.from_csv('C:\Users\prashant.mudgal\Downloads\T2.csv')
"""subset of DV"""
decisionVars= df2['DV']
"""subset for coeff """
coeff = df2['coef']
"""subset for power"""
power = df2['p']

x0 = df['Hist_Inv']

bnds = df[['LB','UB']].values




def objFunc(decisionVars,sign=1.0) :
return sign*(sum(coeff.values[0:] *(df2['weight_f'].values[0:] + df2['weight_v'].values[0:] * decisionVars[0:])**power.values[0:]))
"""bounds for the decision variables have been hardcoded """ """ The bounds have been hardcoded and it will be cumbersome if there are thousands of Decision vars """ """ The bounds have been specified in T1.csv file, we want to ream them via the file """
""" Though we are not using constraints as of now, but is it correct form""" """ How to form the constraints for say x + y < 5 ? """ cons = ({'type': 'ineq', 'fun': lambda x: decisionVars[1] - 2 * decisionVars[1] + 2}, {'type': 'ineq', 'fun': lambda x: -decisionVars[2] - 2 * decisionVars[1] + 6}, {'type': 'ineq', 'fun': lambda x: -decisionVars[0] + 2 * decisionVars[1] + 2})
""" the second parameter is the initial values that needs to be optimized""" """ Now, the number of distinct decision vars in the files are 3 in numbers while the total number of decision vars are 12 in number . if we specify x0 as the dataframe of investment (3 in number in the T1 file) , then it gives error that ::: operands could not be broadcast together with shapes (12,) (3,)"""
res = minimize(objFunc, x0,args=(-1.0,),method='SLSQP',bounds = bnds,
           options={'disp': True})
"""Print the results"""
print (res)
T1.csv is like DV LB UB Hist_Inv X1 0.7 1.3 28462739.43 X2 0.7 1.3 177407.18 X3 0.7 1.3 1423271.12
T2.csv is
Count   DV  weight_f    weight_v    p   coef
1   X1  2.310281831 3.661156016 0.5 1828.105881
2   X1  0.693084549 2.20503016  0.5 1460.686147
3   X1  0.207925365 2.030522789 0.5 1436.277144
4   X1  0   5.248353307 0.8 1050.493355
5   X1  0   1.591805116 0.8 983.9964128
6   X1  0   1.933056056 0.8 459.9371809
7   X2  7.322516444 138 0.5 387.4659072
8   X2  3.661258222 139 0.5 606.8684771
9   X2  1.830629111 176.5   0.5 358.8902965
10  X3  164294.4758 77024   0.2 282.0477107
11  X3  98576.68545 122261.4    0.2 345.9217482
12  X3  59146.01127 166242.84   0.2 364.9587162
I create my obj function using vars in T2, there are a lot of vars which are repeated in T2.csv on running optimization I am getting the error operands could not be broadcast together with shapes (12,) (3,) because it is taking all the variables in T2 as different. How should I avoid this issue? X1, X2 AND X3 are the only three vars here.
Please help. I am stuck for past many days on this.

Thanks,
Prashant Mudgal

AI (Accenture Interactive)
+1  917 615 3574(Cell)


________________________________

This message is for the designated recipient only and may contain privileged, proprietary, or otherwise confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the e-mail by you is prohibited. Where allowed by local law, electronic communications with Accenture and its affiliates, including e-mail and instant messaging (including content), may be scanned by our systems for the purposes of information security and assessment of internal compliance with Accenture policy.
______________________________________________________________________________________

www.accenture.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140916/20cf69fc/attachment.html>


More information about the Python-list mailing list