Convert a for loop from MATLAB into Python - python
I have tried to convert a MATLAB code into Python but not getting the appropriate results. The results of MATLAB are right but the results of Python code are wrong.
My MATLAB code is:
xc =[27.6851 27.6573 27.6358 27.6499 27.6448 27.6435 27.6480 27.6418 27.6556 27.6506 27.6066 27.6004
27.5988 27.5939 27.5892 27.5755 27.5774 27.5968 27.5907 27.5861 27.5776 27.5554 27.5509 27.5210 27.5094
27.5109 27.4952 27.4777 27.4617 27.4298 27.4275 27.4186 27.4338 27.4247 27.4132 27.4111 27.4014 27.3751
27.3411 27.3276 27.3043 27.2914 27.2840 27.2216 27.1806 27.1619 27.1599 27.1727 27.1644 27.1724 27.1722
27.1741 27.1807 27.1814 27.1791 27.1720 27.1653 27.1621 27.1590 27.1598 27.1645 27.1587 27.1500 27.1573
27.1635 27.1605 27.1503 27.1489 27.1509 27.1503 27.1454 27.1167 27.0813 27.0524 27.0040 26.9938 26.9731
26.9935 26.9870 26.9876 26.9887 26.9920 26.9851 26.9736 26.9906 27.0207 27.0270 27.0220 27.0224 27.0241
27.0213 27.0152 27.0182 27.0235 27.0431 27.0516 27.0493 27.0405 27.0424 27.0383 27.0353 27.0293 27.0288
27.0333 27.0403 27.0421 27.0449 27.0723 27.1126 27.1903 27.2248 27.2522 27.2646 27.2721 27.2726 27.2959
27.3411 27.3833 27.4242 27.4889 27.5974 27.6362 28.0603];
yc = [22.7222 22.7192 22.6895 22.6662 22.6459 22.6377 22.6498 22.6579 22.6530 22.6664 22.6903 22.6904
22.7133 22.7365 22.7414 22.7678 22.7831 22.7956 22.8158 22.8497 22.8633 22.8916 22.9066 22.9493
22.9893 23.0000 23.0118 23.0371 23.0545 23.0857 23.0978 23.1105 23.1083 23.1255 23.1350 23.1422
23.1623 23.1828 23.2156 23.2344 23.2622 23.2705 23.2906 23.3335 23.4233 23.4887 23.5721 23.6112
23.6368 23.6530 23.6813 23.7030 23.7298 23.7612 23.7900 23.8237 23.8459 23.8881 23.9266 23.9494
23.9866 24.0173 24.0344 24.0827 24.1190 24.1501 24.1796 24.1883 24.1985 24.2336 24.3291 24.3946
24.4475 24.4868 24.5159 24.5969 24.6645 24.7714 24.8320 24.8987 25.0400 25.1576 25.2470 25.3148
25.4278 25.5148 25.5629 25.5927 25.6133 25.6391 25.6453 25.6630 25.6738 25.6874 25.7278 25.7371
25.7590 25.7740 25.7956 25.8046 25.8755 25.8226 25.8929 25.8323 25.8631 25.7839 25.7516 25.7103
25.7021 25.6988 25.7069 25.7092 25.7137 25.7180 25.7247 25.6927 25.7097 25.7337 25.7497 25.7821
25.8509 25.9075 25.6166];
idx_start = [1,5,9,100,105,1];
idx_end = [5,9,100,105,123,123];
Length_idx=idx_end-idx_start+1;
for i=1:length(idx_start)
if Length_idx(i)>=12 %Minimum number of points to calculate MSD
for j=1:Length_idx(i)
msd(j)=mean((xc(idx_start(i)+j:1:idx_end(i))-xc(idx_start(i):1:idx_end(i)-j)).^2 + (yc(idx_start(i)+j:1:idx_end(i))-yc(idx_start(i):1:idx_end(i)-j)).^2 );
end
end
end
msd
I have tried to convert MATLAB code into Python. My Python code is:
import numpy as np
x = np.array([[27.685125],[27.657279],[27.635751],[27.649908],[27.64476 ],[27.643473],[27.648036],[27.641835],[27.655641],[27.65061 ],[27.606618],[27.600417],
[27.598779],[27.593865],[27.589185],[27.575496],[27.577368],[27.59679 ],[27.590706],[27.586143],[27.577602],[27.555372],[27.550926],[27.520974],
[27.509391],[27.510912],[27.495234],[27.477684],[27.461655],[27.429831],[27.427491],[27.418599],[27.433809],[27.424683],[27.413217],[27.411111],
[27.4014 ],[27.375075],[27.341145],[27.327573],[27.30429 ],[27.29142 ],[27.284049],[27.221571],[27.180621],[27.161901],[27.159912],[27.172665],
[27.164358],[27.172431],[27.172197],[27.174069],[27.180738],[27.18144 ],[27.1791 ],[27.171963],[27.165294],[27.162135],[27.158976],[27.159795],
[27.164475],[27.158742],[27.149967],[27.157338],[27.163539],[27.160497],[27.150318],[27.148914],[27.150903],[27.150318],[27.145404],[27.116739],
[27.081288],[27.052389],[27.003951],[26.993772],[26.973063],[26.993538],[26.986986],[26.987571],[26.988741],[26.992017],[26.985114],[26.973648],
[26.990613],[27.020682],[27.027 ],[27.021969],[27.022437],[27.024075],[27.021267],[27.015183],[27.018225],[27.02349 ],[27.043146],[27.05157 ],
[27.049347],[27.040455],[27.042444],[27.038349],[27.035307],[27.02934 ],[27.028755],[27.033318],[27.040338],[27.042093],[27.044901],[27.072279],
[27.112644],[27.190332],[27.224847],[27.252225],[27.264627],[27.272115],[27.272583],[27.295866],[27.341145],[27.383265],[27.424215],[27.488916],
[27.597375],[27.636219],[28.060344]])
y = np.array([[22.722219],[22.719177],[22.689459],[22.666176],[22.645935],[22.637745],[22.649796],[22.657869],[22.652955],[22.66641 ],[22.690278],[22.690395],
[22.713327],[22.736493],[22.741407],[22.767849],[22.783059],[22.795578],[22.815819],[22.849749],[22.863321],[22.891635],[22.906611],[22.949316],
[22.98933 ],[22.999977],[23.011794],[23.037066],[23.054499],[23.085738],[23.097789],[23.110542],[23.108319],[23.125518],[23.134995],[23.142249],
[23.162256],[23.182848],[23.215608],[23.234445],[23.262174],[23.270481],[23.290605],[23.333544],[23.423283],[23.488686],[23.572107],[23.611185],
[23.636808],[23.652954],[23.681268],[23.70303 ],[23.729823],[23.761179],[23.789961],[23.823657],[23.845887],[23.888124],[23.926617],[23.949432],
[23.986638],[24.017292],[24.034374],[24.082695],[24.118965],[24.150087],[24.179571],[24.188346],[24.198525],[24.233625],[24.329097],[24.394617],
[24.447501],[24.486813],[24.515946],[24.59691 ],[24.664536],[24.771357],[24.831963],[24.898653],[25.039989],[25.157574],[25.246962],[25.314822],
[25.427844],[25.514775],[25.562862],[25.592697],[25.613289],[25.639146],[25.645347],[25.663014],[25.673778],[25.68735 ],[25.727832],[25.737075],
[25.758954],[25.774047],[25.795575],[25.804584],[25.875486],[25.822602],[25.892919],[25.832313],[25.863084],[25.783875],[25.751583],[25.710282],
[25.702092],[25.698816],[25.706889],[25.709229],[25.713675],[25.718004],[25.724673],[25.692732],[25.709697],[25.733682],[25.749711],[25.78212 ],
[25.850916],[25.907544],[25.616565]])
idx_start = np.array([1,5,9,100,105,1])
idx_end = np.array([5,9,100,105,123,123])
length_idx = idx_end - idx_start + 1
for i in range(0, len(idx_start)):
msd = []
if length_idx[i] >= 12 :
for j in range(1, length_idx[i] + 1):
t1 = np.power(x[idx_start[i] + j: idx_end[i] , :] -
x[idx_start[i]: idx_end[i] - j , :], 2)
t2 = np.power(y[idx_start[i] + j: idx_end[i] , :] -
y[idx_start[i]: idx_end[i] - j , :], 2)
msd.append(np.mean(t1 + t2, axis=0)[0])
my_formatted_list = [ '%.4f' % elem for elem in msd ]
print(my_formatted_list)
where x and y are column vectors of shape (123,1)
Expected output is:
0.0045 0.0101 0.0195 0.0313 0.0457 0.0626
0.0820 0.1037 0.1279 0.1547 0.1843 0.2165
0.2510 0.2881 0.3269 0.3679 0.4110 0.4571
0.5054 0.5567 0.6098 0.6659 0.7240 0.7849
0.8483 0.9145 0.9835 1.0555 1.1301 1.2071
1.2871 1.3702 1.4570 1.5474 1.6413 1.7388
1.8401 1.9448 2.0513 2.1604 2.2724 2.3870
2.5028 2.6187 2.7348 2.8518 2.9672 3.0842
3.2018 3.3232 3.4471 3.5734 3.7012 3.8291
3.9595 4.0931 4.2304 4.3702 4.5125 4.6570
4.8028 4.9518 5.1040 5.2577 5.4142 5.5726
5.7326 5.8968 6.0646 6.2351 6.4043 6.5733
6.7430 6.9142 7.0873 7.2593 7.4274 7.5850
7.7358 7.8770 8.0065 8.1259 8.2378 8.3426
8.4342 8.5148 8.5897 8.6610 8.7312 8.7994
8.8684 8.9370 9.0059 9.0730 9.1317 9.1890
9.2402 9.2890 9.3325 9.3662 9.3730 9.3910
9.3818 9.3880 9.3741 9.3820 9.3972 9.4253
9.4538 9.4908 9.5201 9.5433 9.5653 9.5774
9.5797 9.6126 9.6359 9.6288 9.6105 9.5703
9.3530 8.5180 NaN
But I am getting this:
['0.0045', '0.0102', '0.0196', '0.0315', '0.0460', '0.0631',
'0.0826', '0.1046', '0.1289', '0.1560', '0.1859', '0.2184',
'0.2533', '0.2907', '0.3298', '0.3712', '0.4148', '0.4613',
'0.5101', '0.5619', '0.6154', '0.6720', '0.7305', '0.7919',
'0.8560', '0.9228', '0.9923', '1.0650', '1.1401', '1.2180',
'1.2988', '1.3830', '1.4708', '1.5622', '1.6573', '1.7558',
'1.8581', '1.9636', '2.0714', '2.1814', '2.2948', '2.4108',
'2.5270', '2.6427', '2.7591', '2.8762', '2.9928', '3.1109',
'3.2302', '3.3529', '3.4784', '3.6063', '3.7356', '3.8649',
'3.9966', '4.1318', '4.2704', '4.4115', '4.5559', '4.7019',
'4.8495', '5.0009', '5.1545', '5.3101', '5.4686', '5.6290',
'5.7925', '5.9603', '6.1306', '6.2997', '6.4687', '6.6380',
'6.8089', '6.9816', '7.1532', '7.3241', '7.4885', '7.6452',
'7.7942', '7.9251', '8.0454', '8.1575', '8.2637', '8.3568',
'8.4395', '8.5151', '8.5871', '8.6570', '8.7247', '8.7936',
'8.8610', '8.9297', '8.9982', '9.0597', '9.1184', '9.1722',
'9.2206', '9.2653', '9.3070', '9.3208', '9.3411', '9.3363',
'9.3441', '9.3384', '9.3498', '9.3692', '9.4034', '9.4409',
'9.4806', '9.5217', '9.5574', '9.5851', '9.6120', '9.6265',
'9.6660', '9.7075', '9.7310', '9.7276', '9.6802', '9.4572',
'8.5573', 'nan', 'nan']
Related
Someone can help me check do i convert correctly the Chandelier Exit indicator from pine script code to python?
These pine script code is from trading view and write by everget. This indicator is Chandelier Exit. I am doing back test by using this indicator so I need convert this pine script code to python in order to get the buy and sell signal. I am using GBPJPY data from 31/12/2019 to 1/1/2022, 4hrs charts. But the buysignal and sellsignal total just have 6 signal, i feel like something wrong with the result. Therefore, I am thinking do I convert correctly the code from pine script to python? Python code : # ATR calculation atr_period = 2 h, l, c_prev,c = df['high'], df['low'], df['close'].shift(1),df['close'] tr = np.max([h - l, (c_prev - h).abs(), (c_prev - l).abs()], axis=0) df['atr'] = pd.Series(tr).rolling(2).mean().bfill().values # CE calculation df['c1'] = pd.Series(h).rolling(2).max().bfill().values # Get the max value within two row df['c2'] = pd.Series(l).rolling(2).min().bfill().values # Get the min value within two row df['cel'] = df['c1'] - df['atr']*4 df['ces'] = df['c2'] + df['atr']*4 sh = df['ces'].to_numpy(dtype='float') # Convert to array lo = df['cel'].to_numpy(dtype='float') sh_prev = (df['ces'].shift(1)).to_numpy(dtype='float') # Shift downward to get the previous data in ces,cel and close lo_prev = (df['cel'].shift(1)).to_numpy(dtype='float') c_prev = (df['close'].shift(1)).to_numpy(dtype='float') shnew = np.empty(3126, dtype=float) # Create new array to save new data lonew = np.empty(3126, dtype=float) for i in range(len(df['close'])): # Compare the previous close to previous cel if c_prev[i] > lo_prev[i]: lonew[i] = max(lo[i], lo_prev[i]) else: lonew[i] = lo[i] for i in range(len(df['close'])):# Compare the previous close to previous ces if c_prev[i] < sh_prev[i]: shnew[i] = min(sh[i], sh_prev[i]) else: shnew[i] = sh[i] # Create array for direction # Follow the CE indicator to identify the direction direc = np.empty(3126, dtype=int) direc[3125] = 1 # Put a initial data into the last array for i in range(len(df['close'])): # Compare the close to CE to get the direction if c[i] > shnew[i]: direc[i] = 1 elif c[i] < lonew[i]: direc[i] = -1 else: direc[i] = direc[i-1] dfdir = pd.DataFrame(direc,columns=['direc'],index=df.index) # Convert the direction to dataframe df = pd.merge(df, dfdir,left_index=True,right_index=True) # Merge the direction into ori dataframe df['direc_prev'] = df['direc'].shift(1) # Shift downward to get the previous data df['buysignal'] = np.logical_and(df['direc'] == 1,df['direc_prev'] == -1) # Get the buy signal (long) df['sellsignal'] = np.logical_and(df['direc'] == -1,df['direc_prev'] == 1) # Get the sell signal (short) Pine script code in trading view: //#version=4 // Copyright (c) 2019-present, Alex Orekhov (everget) // Chandelier Exit script may be freely distributed under the terms of the GPL-3.0 license. study("Chandelier Exit", shorttitle="CE", overlay=true) length = input(title="ATR Period", type=input.integer, defval=22) mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0) showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true) useClose = input(title="Use Close Price for Extremums ?", type=input.bool, defval=true) highlightState = input(title="Highlight State ?", type=input.bool, defval=true) atr = mult * atr(length) longStop = (useClose ? highest(close, length) : highest(length)) - atr // if useClose = false // longstop = highest(length) - atr (want this) // else // longstop = highest(close, length) longStopPrev = nz(longStop[1], longStop) // nz(x,y) = if previous value is valid then take x else y longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop // if close[1] > longStopPrev // longstop = max(longStop, longStopPrev) // else // longstop = longstop shortStop = (useClose ? lowest(close, length) : lowest(length)) + atr shortStopPrev = nz(shortStop[1], shortStop) shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop var int dir = 1 dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir // if close > shortStopPrev // dir = 1 // elseif close < longStopPrev // dir = -1 //else // dir = dir[-1] var color longColor = color.green var color shortColor = color.red buySignal = dir == 1 and dir[1] == -1 // and = 1 & 1 > 1 plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0) sellSignal = dir == -1 and dir[1] == 1 plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)
How to derive scipy's global minimum for a specific equation with trust region reflective algorithm
A new data set was created by adding random variables from a specific data set. We want to solve the problem of matching two inconsistent data sets. So I tried to solve the optimization problem using scipy's minimum_squares function. The code and data used are as follows. from numba import jit, prange from scipy.optimize import minimize,least_squares import scipy.io as sio import numpy as np import matplotlib.pyplot as plt from scipy.optimize import basinhopping def Data_loader(name,normalize): global X global Y arr = sio.loadmat(name) X = arr['X'] Y = X R = np.array([[1.0171, 0.0249, 0.1269], [0.1865, 0.9383, -0.3441], [0.0172, 0.5278, 1.0347]]) # R = np.random.rand(3,3) X = np.random.rand(1)*np.dot(X,np.transpose(R))+1 X = X*0.1 X = X[0:len(X) - 20] Y = Y[19:len(Y)] if normalize==True : n = X.shape[0] m = Y.shape[0] x_d = np.mean(X, axis=0) y_d = np.mean(Y, axis=0) x = X - np.tile(x_d, [n, 1]) y = Y - np.tile(y_d, [m, 1]) x_scale = np.sqrt(np.sum(np.sum(np.power(X,2),axis=0))/n) y_scale = np.sqrt(np.sum(np.sum(np.power(Y, 2), axis=0))/m) x = x / x_scale y = y / y_scale else : x=X y=Y return x,y,x_scale,y_scale,x_d,y_d X_data,Y_data,x_scale,y_scale,x_d,y_d = Data_loader(name='cpd_data3D_face.mat',normalize=True) #jit(nopython=True, parallel=True) def P_cal(X,Y,sigma2,outlier): N = X.shape[0] M = Y.shape[0] D = X.shape[1] P = np.zeros(shape = (N,M),dtype=np.float64) Np = np.zeros(shape = (N,M),dtype=np.float64) Pmn = np.zeros(shape = (N,M),dtype=np.float64) ksig = -2.0 * sigma2 outlier_tmp = (outlier*M*((-ksig*3.1415926535)**(0.5*D)))/((1-outlier)*N) for n in prange(0,N): sp=np.zeros(shape = (1,),dtype=np.float64) for m in prange(0,M): razn=0 for d in prange(0,D): diff = X[n,d] - Y[m,d] diff = diff*diff razn += diff P[n,m] = np.exp(razn/ksig) sp = P.sum(axis=1)[n] + outlier_tmp for m in prange(0,M): Np[n,m] = P[n,m]/sp razn = 0 for d in prange(0,D): diff = X[n,d] - Y[m,d] diff = diff*diff razn += diff Pmn[n,m] = (P[n,m]/sp) * razn return Pmn, Np def model(xin): global T N = X_data.shape[0] M = Y_data.shape[0] D = X_data.shape[1] B = np.array([[xin[0], xin[1], xin[2]], [xin[3], xin[4], xin[5]], [xin[6], xin[7], xin[8]]]) t = np.array([xin[9], xin[10], xin[11]]) sigma2 = xin[12] outlier = 0.6 T = np.dot(Y_data,np.transpose(B)) + np.tile(np.transpose(t), [M, 1]) Pmn, Np = P_cal(X_data, T, sigma2, outlier) Q = ((1 / sigma2) * (np.sum(Pmn))) + (((np.sum(Np) * D) / 2) * np.log(sigma2)) return Q def opt_function(xin): Q= model(xin) print(Q) Q= 10000+Q return Q def plot_function(X, Y, T) : X_x = X[1:X.shape[0], 0] X_y = X[1:X.shape[0], 1] X_z = X[1:X.shape[0], 2] Y_x = Y[1:Y.shape[0], 0] Y_y = Y[1:Y.shape[0], 1] Y_z = Y[1:Y.shape[0], 2] T_x = T[1:T.shape[0], 0] T_y = T[1:T.shape[0], 1] T_z = T[1:T.shape[0], 2] fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection='3d') ax.scatter(X_x, X_y, X_z) ax.scatter(Y_x, Y_y, Y_z) ax.set_title('before') fig2 = plt.figure(figsize=(10, 10)) ax = fig2.add_subplot(111, projection='3d') ax.scatter(X_x, X_y, X_z) ax.scatter(T_x, T_y, T_z) ax.set_title('after') plt.show() def transformer_data(T,Y, xin, x_scale, y_scale, x_d, y_d): M = Y.shape[0] Trans_R = np.array([xin[0], xin[1], xin[2], xin[3], xin[4], xin[5], xin[6], xin[7], xin[8]]) Trans_R = Trans_R.reshape((3, 3)) Trans_t = np.array([xin[9], xin[10], xin[11]]) Trans_s = 1*(x_scale/y_scale) Trans_t = (x_scale*np.transpose(Trans_t)) + np.transpose(x_d) - Trans_s*(Trans_R#np.transpose(y_d)) Trans_T = T * x_scale + np.tile(x_d, [M, 1]) return Trans_T x0 = np.array([1,0,0,0,1,0,0,0,1,0,0,0,0.0004]) Optimum = least_squares(opt_function,x0,jac='2-point',method='trf',xtol=1e-15,ftol=1e-15,gtol=1e-15,verbose=2) print(Optimum) Transformer_T = transformer_data(T,Y,Optimum.x,x_scale,y_scale,x_d,y_d) plot_function(X,Y,Transformer_T) [cpd_data3D_face.mat] -1.26805102825165 0.703154087066650 0.250609993934631 -1.24199450016022 0.766805052757263 0.647247254848480 -1.23889672756195 1.06739985942841 -0.188671588897705 -1.21881270408630 0.807438790798187 1.16736936569214 -1.18697428703308 0.626793265342712 -0.182836949825287 -1.16291773319244 0.822238981723785 1.83739590644836 -1.14834547042847 0.493062376976013 0.635148286819458 -1.13797473907471 0.331227302551270 0.219468995928764 -1.13209247589111 1.01279878616333 -0.797961413860321 -1.11951982975006 0.473790675401688 1.03894352912903 -1.06242597103119 0.390692919492722 1.47615230083466 -1.04522788524628 0.358604818582535 0.543326914310455 -1.04382777214050 0.319657772779465 0.706707119941711 -1.03485214710236 0.254935413599014 0.932691574096680 -1.01772892475128 0.258871674537659 1.21425902843475 -1.01639711856842 0.200742736458778 -0.224685385823250 -1.01449811458588 0.528465926647186 -0.686351001262665 -0.983937680721283 0.189417555928230 1.00790750980377 -0.983821272850037 0.745310366153717 -1.11274755001068 -0.978638589382172 0.299691230058670 0.629687130451202 -0.922493815422058 0.150767520070076 0.828504979610443 -0.909300625324249 0.283628404140472 0.559927582740784 -0.906097352504730 0.933652937412262 -1.29045069217682 -0.891153812408447 0.289580315351486 0.480834931135178 -0.889832675457001 0.129670828580856 0.226587772369385 -0.879191935062408 0.845484137535095 2.29874420166016 -0.854662895202637 0.191300705075264 0.728563904762268 -0.837804734706879 0.327339112758637 1.77278757095337 -0.818458557128906 0.118494018912315 1.31940019130707 -0.813382029533386 0.144385874271393 -0.574904143810272 -0.806623399257660 0.0640521720051765 1.12445175647736 -0.806076943874359 0.179906263947487 0.362056881189346 -0.794836580753326 1.03445231914520 -1.48189103603363 -0.791478812694550 0.239165067672730 0.551705360412598 -0.790624439716339 0.193809807300568 0.438241928815842 -0.787293195724487 0.173615008592606 0.588202476501465 -0.780423700809479 0.399599254131317 -1.30588448047638 -0.775464117527008 -0.00958730466663837 -0.174502417445183 -0.767135858535767 0.172049850225449 0.568943262100220 -0.763602852821350 0.184372812509537 0.565283775329590 -0.762090682983398 0.196064323186874 0.504386007785797 -0.760000824928284 0.188478738069534 0.540778219699860 -0.755698919296265 0.209235891699791 0.508155345916748 -0.746246218681335 0.0437246896326542 0.911044776439667 -0.704111039638519 0.711774110794067 -1.47775316238403 -0.656321167945862 0.119024306535721 -1.07423388957977 -0.650056242942810 0.0959970802068710 0.784161567687988 -0.644072175025940 -0.00454827398061752 -0.505615711212158 -0.602208197116852 0.0996995121240616 0.379885524511337 -0.600404739379883 0.0790802314877510 0.659766137599945 -0.600255548954010 0.106224447488785 0.449432760477066 -0.598033308982849 0.120790801942348 0.452769994735718 -0.596876800060272 0.102229975163937 0.541571617126465 -0.595354616641998 0.0876417979598045 0.651539027690888 -0.594505965709686 0.0156574435532093 -0.975354671478272 -0.593853473663330 0.0964264348149300 0.647029995918274 -0.593455016613007 0.889857470989227 -1.61020958423615 -0.592627882957459 0.101232483983040 0.321590214967728 -0.573011338710785 -0.0138365831226110 -0.755739450454712 -0.556460380554199 -0.00661153765395284 0.212208628654480 -0.543379187583923 0.113693088293076 -1.46306395530701 -0.519296109676361 -0.0184836350381374 -1.20552349090576 -0.489446550607681 0.0686619207262993 0.649044036865234 -0.489271312952042 -0.0751322507858276 -0.480372875928879 -0.488831192255020 0.0591186434030533 0.653709352016449 -0.484343349933624 0.0449181720614433 0.664057910442352 -0.477669984102249 0.0742370337247849 0.549954950809479 -0.464891046285629 -0.0795564651489258 -0.776580691337585 -0.459231346845627 0.543196737766266 -1.60204350948334 -0.457071840763092 0.0801357030868530 0.468159794807434 -0.453608423471451 0.0624924153089523 0.466883003711700 -0.453008860349655 0.0603442415595055 0.380578845739365 -0.450547099113464 -0.137127891182899 -0.103212133049965 -0.449798971414566 0.0621318705379963 0.336125582456589 -0.414782941341400 -0.0911946594715118 -0.962513506412506 -0.404184430837631 -0.105692356824875 -0.845229566097260 -0.401245862245560 0.0562914907932282 0.555313587188721 -0.400264382362366 0.0315503329038620 0.797742426395416 -0.399596065282822 0.0716907083988190 0.495797544717789 -0.397346645593643 0.0558350458741188 0.493942171335220 -0.396078765392303 0.800601005554199 -1.71100533008575 -0.395203620195389 0.0524718016386032 0.630690515041351 -0.395150482654572 0.0250202100723982 0.647774577140808 -0.394768565893173 0.0421436578035355 0.636364042758942 -0.394747585058212 -0.106679096817970 -0.805121719837189 -0.392148017883301 -0.118094518780708 0.929876744747162 -0.385803490877152 -0.149832293391228 -0.273493885993958 -0.364510416984558 0.881731152534485 2.62541174888611 -0.360802918672562 -0.143175244331360 -0.298475503921509 -0.360712468624115 -0.165030494332314 -0.683113873004913 -0.354962378740311 -0.158224657177925 -0.731766164302826 -0.351477324962616 -0.153869569301605 -0.548111081123352 -0.350320786237717 -0.155996680259705 1.20539724826813 -0.347673654556274 0.0197845026850700 0.360736131668091 -0.343419760465622 0.0446846485137939 0.414639592170715 -0.341843873262405 0.0630866438150406 0.510752856731415 -0.341169834136963 0.0492043718695641 0.502054572105408 -0.337658882141113 0.0351469479501247 0.561841547489166 -0.337361693382263 -0.133780807256699 -0.763273239135742 -0.336719214916229 0.0249884724617004 0.577858805656433 -0.336661010980606 0.0514110624790192 0.550272345542908 -0.335812389850616 -0.0870144963264465 0.227063819766045 -0.334714591503143 -0.117261730134487 -0.773931920528412 -0.334714591503143 -0.117261730134487 -0.768631875514984 -0.327723145484924 0.200987175107002 1.97596061229706 -0.321362793445587 -0.147063419222832 -1.27225828170776 -0.319569408893585 -0.0633241608738899 -1.53213012218475 -0.313458234071732 -0.168399006128311 -0.838223099708557 -0.307326167821884 -0.179740279912949 -0.134934157133102 -0.301624149084091 -0.0517277717590332 1.45116829872131 -0.296892076730728 0.0488278791308403 0.533883988857269 -0.296568334102631 -0.140135616064072 -1.04919373989105 -0.295273929834366 0.0496075637638569 0.512347042560577 -0.293163061141968 -0.172096326947212 -0.910953700542450 -0.284124016761780 0.0332795977592468 0.505501687526703 -0.282951623201370 0.0313751697540283 0.540355861186981 -0.269329160451889 0.0239753406494856 0.553192079067230 -0.267992317676544 -0.181957572698593 -0.160023182630539 -0.267015933990479 -0.177904888987541 -0.321233123540878 -0.265541255474091 -0.245476022362709 -0.144584536552429 -0.262671560049057 -0.202741548418999 -0.0718050673604012 -0.261472642421722 0.0231583528220654 0.492826491594315 -0.248518571257591 -0.191129758954048 -0.211526885628700 -0.243474200367928 -0.315041571855545 -0.0893090441823006 -0.230440512299538 -0.228538274765015 -0.0157361794263124 -0.228599637746811 -0.249311119318008 -0.0108560575172305 -0.226721569895744 -0.278264611959457 -0.213387131690979 -0.224844038486481 -0.332080185413361 -0.176096573472023 -0.223889231681824 0.318416297435761 -1.66368579864502 -0.214929193258286 -0.266498714685440 -0.676058530807495 -0.210813075304031 -0.223832860589027 -0.255875170230866 -0.207094073295593 -0.281632810831070 -0.595061719417572 -0.204060018062592 -0.0362701974809170 0.500299334526062 -0.199752956628799 -0.219154447317123 -0.731922388076782 -0.198813900351524 -0.259246885776520 -0.518303036689758 -0.197991833090782 -0.199447497725487 -0.743696689605713 -0.197991833090782 -0.199447497725487 -0.738389372825623 -0.197726234793663 -0.252106904983521 -0.839765965938568 -0.193233251571655 -0.370242148637772 -0.0653356760740280 -0.190501421689987 -0.284990489482880 0.0164594296365976 -0.189626261591911 -0.382326304912567 -0.151752114295959 -0.188193947076797 -0.00516866426914930 0.692332863807678 -0.187763944268227 -0.247889831662178 -0.938535273075104 -0.186157137155533 -0.312341511249542 -0.215669736266136 -0.184836089611053 -0.161518707871437 0.312147319316864 -0.175839409232140 -0.193330869078636 -1.04541945457459 -0.169654235243797 -0.245794147253037 -0.369660794734955 -0.165510952472687 -0.0105156786739826 0.591866850852966 -0.163967385888100 0.744191050529480 -1.78527390956879 -0.163914248347282 -0.269649654626846 -0.244318157434464 -0.159129753708839 -0.366783618927002 -0.183630362153053 -0.153681874275208 -0.152272656559944 0.826754629611969 -0.140536934137344 -0.201197594404221 1.15596377849579 -0.133895203471184 -0.233975455164909 -1.29469370841980 -0.132924452424049 -0.456851601600647 -0.132863938808441 -0.130474045872688 -0.456304907798767 -0.0475655905902386 -0.118909314274788 -0.346768766641617 -0.195299670100212 -0.117933601140976 -0.151625871658325 -1.55685293674469 -0.115535654127598 -0.404653489589691 0.0411310084164143 -0.113641686737537 -0.379890978336334 0.0627729669213295 -0.108262777328491 -0.130481123924255 0.724745750427246 -0.0882218107581139 -0.447865873575211 -0.161400064826012 -0.0852775499224663 -0.299838036298752 -0.391834944486618 -0.0817874670028687 -0.306500911712647 -0.520046114921570 -0.0744881182909012 -0.330223441123962 -0.562329113483429 -0.0678362473845482 -0.302734911441803 -0.270571678876877 -0.0670988634228706 -0.528554797172546 -0.130210265517235 -0.0566540732979775 -0.283123373985291 -0.943625330924988 -0.0483675263822079 -0.303127199411392 -0.828308999538422 -0.0482353903353214 -0.329624116420746 -0.647095203399658 -0.0457053110003471 -0.543731153011322 -0.0194005407392979 -0.0454770512878895 -0.506025552749634 0.0706802755594254 -0.0451158955693245 -0.352757841348648 -0.214075580239296 -0.0422411337494850 -0.172186344861984 0.619287312030792 -0.0417529530823231 -0.456273496150970 0.111095674335957 -0.0337586030364037 -0.300846278667450 0.405985265970230 -0.0330427736043930 -0.261952579021454 -0.736106753349304 -0.0330427736043930 -0.261952579021454 -0.730799376964569 -0.0326184481382370 -0.289149373769760 -0.717697024345398 -0.0299390628933907 -0.481825888156891 -0.177739560604095 -0.0276746246963739 -0.431670397520065 -0.168245419859886 0.0121531123295426 -0.549932301044464 -0.00995034258812666 0.0121531123295426 -0.541036725044251 -0.122998714447021 0.0121531123295426 -0.512953639030457 0.0811826586723328 0.0121531123295426 -0.486913204193115 -0.174502417445183 0.0121531123295426 -0.465853661298752 0.124627687036991 0.0121531123295426 -0.426710635423660 -0.205699533224106 0.0121531123295426 -0.366242557764053 -0.241559505462646 0.0121531123295426 -0.336180478334427 -0.658293366432190 0.0121531123295426 -0.331316232681274 -0.596917092800140 0.0121531123295426 -0.318112850189209 0.404608368873596 0.0121531123295426 -0.316532135009766 -0.281554996967316 0.0121531123295426 -0.302485466003418 -0.736324012279511 0.0121531123295426 -0.296682208776474 -0.830315709114075 0.0121531123295426 -0.292714387178421 -0.534635245800018 0.0121531123295426 -0.277532249689102 -0.937846839427948 0.0121531123295426 -0.276381313800812 -0.391942352056503 0.0121531123295426 -0.261135667562485 -0.756581723690033 0.0121531123295426 -0.261135667562485 -0.751281678676605 0.0121531123295426 -0.259597063064575 -1.31272983551025 0.0121531123295426 -0.216226130723953 -1.03725826740265 0.0121531123295426 -0.211855024099350 0.920858681201935 0.0121531123295426 -0.198895767331123 -1.54220521450043 0.0121531123295426 -0.190407738089561 1.15325403213501 0.0121531123295426 -0.184148013591766 0.634032607078552 0.0121531123295426 -0.182100847363472 0.750254631042481 0.0121531123295426 -0.0808928310871124 1.42999994754791 0.0121531123295426 0.176368087530136 2.03060126304626 0.0121531123295426 0.222836643457413 -1.69991946220398 0.0121531123295426 0.750662505626679 -1.84563207626343 0.0121531123295426 0.938862383365631 2.70322513580322 0.0519808456301689 -0.431670397520065 -0.168245419859886 0.0542452894151211 -0.481825888156891 -0.177739560604095 0.0569246746599674 -0.289149373769760 -0.717697024345398 0.0573489926755428 -0.261952579021454 -0.736106753349304 0.0573489926755428 -0.261952579021454 -0.730799376964569 0.0580648221075535 -0.300846278667450 0.405985265970230 0.0660591870546341 -0.456273496150970 0.111095674335957 0.0665473639965057 -0.172186344861984 0.619287312030792 0.0694221258163452 -0.352757841348648 -0.214075580239296 0.0697832852602005 -0.506025552749634 0.0706802755594254 0.0700115412473679 -0.543731153011322 -0.0194005407392979 0.0725416243076325 -0.329624116420746 -0.647095203399658 0.0726737603545189 -0.303127199411392 -0.828308999538422 0.0809603035449982 -0.283123373985291 -0.943625330924988 0.0914050936698914 -0.528554797172546 -0.130210265517235 0.0921424329280853 -0.302734911441803 -0.270571678876877 0.0987943112850189 -0.330223441123962 -0.562329113483429 0.106093667447567 -0.306500911712647 -0.520046114921570 0.109583787620068 -0.299838036298752 -0.391834944486618 0.112528048455715 -0.447865873575211 -0.161400064826012 0.132569059729576 -0.130481123924255 0.724745750427246 0.137947961688042 -0.379890978336334 0.0627729669213295 0.139841854572296 -0.404653489589691 0.0411310084164143 0.142239809036255 -0.151625871658325 -1.55685293674469 0.143215626478195 -0.346768766641617 -0.195299670100212 0.154780268669128 -0.456304907798767 -0.0475655905902386 0.157230705022812 -0.456851601600647 -0.132863938808441 0.158201426267624 -0.233975455164909 -1.29469370841980 0.164843082427979 -0.201197594404221 1.15596377849579 0.177988156676292 -0.152272656559944 0.826754629611969 0.183435946702957 -0.366783618927002 -0.183630362153053 0.188220471143723 -0.269649654626846 -0.244318157434464 0.188273563981056 0.744191050529480 -1.78527390956879 0.189817219972610 -0.0105156786739826 0.591866850852966 0.193960443139076 -0.245794147253037 -0.369660794734955 0.200145587325096 -0.193330869078636 -1.04541945457459 0.209142416715622 -0.161518707871437 0.312147319316864 0.210463374853134 -0.312341511249542 -0.215669736266136 0.212070122361183 -0.247889831662178 -0.938535273075104 0.212500244379044 -0.00516866426914930 0.692332863807678 0.213932514190674 -0.382326304912567 -0.151752114295959 0.214807629585266 -0.284990489482880 0.0164594296365976 0.217539504170418 -0.370242148637772 -0.0653356760740280 0.222032532095909 -0.252106904983521 -0.839765965938568 0.222298085689545 -0.199447497725487 -0.743696689605713 0.222298085689545 -0.199447497725487 -0.738389372825623 0.223120197653770 -0.259246885776520 -0.518303036689758 0.224059239029884 -0.219154447317123 -0.731922388076782 0.228366270661354 -0.0362701974809170 0.500299334526062 0.231400310993195 -0.281632810831070 -0.595061719417572 0.235119298100472 -0.223832860589027 -0.255875170230866 0.239235371351242 -0.266498714685440 -0.676058530807495 0.248195409774780 0.318416297435761 -1.66368579864502 0.249150261282921 -0.332080185413361 -0.176096573472023 0.251027762889862 -0.278264611959457 -0.213387131690979 0.252905875444412 -0.249311119318008 -0.0108560575172305 0.254746764898300 -0.228538274765015 -0.0157361794263124 0.267780452966690 -0.315041571855545 -0.0893090441823006 0.272824734449387 -0.191129758954048 -0.211526885628700 0.285778939723969 0.0231583528220654 0.492826491594315 0.286977857351303 -0.202741548418999 -0.0718050673604012 0.289847403764725 -0.245476022362709 -0.144584536552429 0.291322171688080 -0.177904888987541 -0.321233123540878 0.292298495769501 -0.181957572698593 -0.160023182630539 0.293635338544846 0.0239753406494856 0.553192079067230 0.307257831096649 0.0313751697540283 0.540355861186981 0.308430194854736 0.0332795977592468 0.505501687526703 0.317469328641892 -0.172096326947212 -0.910953700542450 0.319580137729645 0.0496075637638569 0.512347042560577 0.320874541997910 -0.140135616064072 -1.04919373989105 0.321198403835297 0.0488278791308403 0.533883988857269 0.325930356979370 -0.0517277717590332 1.45116829872131 0.331632345914841 -0.179740279912949 -0.134934157133102 0.337764531373978 -0.168399006128311 -0.838223099708557 0.343875676393509 -0.0633241608738899 -1.53213012218475 0.345669031143189 -0.147063419222832 -1.27225828170776 0.352029412984848 0.200987175107002 1.97596061229706 0.359020859003067 -0.117261730134487 -0.773931920528412 0.359020859003067 -0.117261730134487 -0.768631875514984 0.360118597745895 -0.0870144963264465 0.227063819766045 0.360967248678207 0.0514110624790192 0.550272345542908 0.361025452613831 0.0249884724617004 0.577858805656433 0.361667960882187 -0.133780807256699 -0.763273239135742 0.361965119838715 0.0351469479501247 0.561841547489166 0.365476071834564 0.0492043718695641 0.502054572105408 0.366150081157684 0.0630866438150406 0.510752856731415 0.367725998163223 0.0446846485137939 0.414639592170715 0.371979862451553 0.0197845026850700 0.360736131668091 0.374627053737640 -0.155996680259705 1.20539724826813 0.375783562660217 -0.153869569301605 -0.548111081123352 0.379268586635590 -0.158224657177925 -0.731766164302826 0.385018706321716 -0.165030494332314 -0.683113873004913 0.385109037160873 -0.143175244331360 -0.298475503921509 0.388816595077515 0.881731152534485 2.62541174888611 0.410109728574753 -0.149832293391228 -0.273493885993958 0.416454315185547 -0.118094518780708 0.929876744747162 0.419053852558136 -0.106679096817970 -0.805121719837189 0.419074833393097 0.0421436578035355 0.636364042758942 0.419456660747528 0.0250202100723982 0.647774577140808 0.419509917497635 0.0524718016386032 0.630690515041351 0.420385032892227 0.800601005554199 -1.71100533008575 0.421652972698212 0.0558350458741188 0.493942171335220 0.423902332782745 0.0716907083988190 0.495797544717789 0.424570620059967 0.0315503329038620 0.797742426395416 0.425552159547806 0.0562914907932282 0.555313587188721 0.428490787744522 -0.105692356824875 -0.845229566097260 0.439089149236679 -0.0911946594715118 -0.962513506412506 0.474105179309845 0.0621318705379963 0.336125582456589 0.474853336811066 -0.137127891182899 -0.103212133049965 0.477315068244934 0.0603442415595055 0.380578845739365 0.477914631366730 0.0624924153089523 0.466883003711700 0.481377989053726 0.0801357030868530 0.468159794807434 0.483537673950195 0.543196737766266 -1.60204350948334 0.489197254180908 -0.0795564651489258 -0.776580691337585 0.501976311206818 0.0742370337247849 0.549954950809479 0.508649528026581 0.0449181720614433 0.664057910442352 0.513137459754944 0.0591186434030533 0.653709352016449 0.513577580451965 -0.0751322507858276 -0.480372875928879 0.513752877712250 0.0686619207262993 0.649044036865234 0.543602466583252 -0.0184836350381374 -1.20552349090576 0.567685365676880 0.113693088293076 -1.46306395530701 0.580766677856445 -0.00661153765395284 0.212208628654480 0.597317516803742 -0.0138365831226110 -0.755739450454712 0.616934120655060 0.101232483983040 0.321590214967728 0.617761254310608 0.889857470989227 -1.61020958423615 0.618159651756287 0.0964264348149300 0.647029995918274 0.618812203407288 0.0156574435532093 -0.975354671478272 0.619660854339600 0.0876417979598045 0.651539027690888 0.621182978153229 0.102229975163937 0.541571617126465 0.622339546680450 0.120790801942348 0.452769994735718 0.624561727046967 0.106224447488785 0.449432760477066 0.624710798263550 0.0790802314877510 0.659766137599945 0.626514434814453 0.0996995121240616 0.379885524511337 0.668378472328186 -0.00454827398061752 -0.505615711212158 0.674362480640411 0.0959970802068710 0.784161567687988 0.680627405643463 0.119024306535721 -1.07423388957977 0.728417158126831 0.711774110794067 -1.47775316238403 0.770552396774292 0.0437246896326542 0.911044776439667 0.780005037784576 0.209235891699791 0.508155345916748 0.784307062625885 0.188478738069534 0.540778219699860 0.786396920681000 0.196064323186874 0.504386007785797 0.787909030914307 0.184372812509537 0.565283775329590 0.791442155838013 0.172049850225449 0.568943262100220 0.799770295619965 -0.00958730466663837 -0.174502417445183 0.804729878902435 0.399599254131317 -1.30588448047638 0.811599373817444 0.173615008592606 0.588202476501465 0.814930796623230 0.193809807300568 0.438241928815842 0.815785050392151 0.239165067672730 0.551705360412598 0.819142878055573 1.03445231914520 -1.48189103603363 0.830383181571960 0.179906263947487 0.362056881189346 0.830929577350617 0.0640521720051765 1.12445175647736 0.837688326835632 0.144385874271393 -0.574904143810272 0.842764794826508 0.118494018912315 1.31940019130707 0.862110912799835 0.327339112758637 1.77278757095337 0.878969132900238 0.191300705075264 0.728563904762268 0.903498232364655 0.845484137535095 2.29874420166016 0.914138853549957 0.129670828580856 0.226587772369385 0.915460050106049 0.289580315351486 0.480834931135178 0.930403351783752 0.933652937412262 -1.29045069217682 0.933606922626495 0.283628404140472 0.559927582740784 0.946800053119659 0.150767520070076 0.828504979610443 1.00294494628906 0.299691230058670 0.629687130451202 1.00812780857086 0.745310366153717 -1.11274755001068 1.00824403762817 0.189417555928230 1.00790750980377 1.03880441188812 0.528465926647186 -0.686351001262665 1.04070341587067 0.200742736458778 -0.224685385823250 1.04203522205353 0.258871674537659 1.21425902843475 1.05915844440460 0.254935413599014 0.932691574096680 1.06813418865204 0.319657772779465 0.706707119941711 1.06953442096710 0.358604818582535 0.543326914310455 1.08673226833344 0.390692919492722 1.47615230083466 1.14382600784302 0.473790675401688 1.03894352912903 1.15639877319336 1.01279878616333 -0.797961413860321 1.16228091716766 0.331227302551270 0.219468995928764 1.17265176773071 0.493062376976013 0.635148286819458 1.18722403049469 0.822238981723785 1.83739590644836 1.21128082275391 0.626793265342712 -0.182836949825287 1.24311912059784 0.807438790798187 1.16736936569214 1.26320302486420 1.06739985942841 -0.188671588897705 1.26630091667175 0.766805052757263 0.647247254848480 1.29235732555389 0.703154087066650 0.250609993934631 [problem in code] That global optimization solution has a value of around -8000. However, in that code it finds a solution up to about -3000 and then exits. I tried to use the trust region reflective algorithm because the formula to be optimized is non-linear, and as a result, it failed to find the global minimum. [Question content] I want to find the global minimum. I'd appreciate it if you could give me some feedback to help solve the problem.
How to efficiently calculate pairwise ratios on rows using NumPy?
I'm trying to speed up my calculation of calculating pairwise values. However, I'm having trouble visualizing how to do this in NumPy without being overly verbose and explicit. Is there a way to use vector multiplication or broadcasting to speed up this operation? Essentially what I'm doing is iterating through each row, then through all combinations of features in each row, and then taking the ratio (followed by log transform). import pandas as pd import numpy as np from collections import * # Get data data = {'BC0a': {'1001.2': 4122,'1010.1': 6766,'1010.2': 11734,'1018.2': 5805,'1021.2': 9504,'1026.1': 18111,'1026.2': 1350,'1029.1': 3638,'1029.2': 3967,'1048.2': 9225,'1049.2': 8670,'1057.2': 21796,'1065.1': 5117,'1065.2': 7811,'1067.1': 17563,'1067.2': 8418,'1071.1': 10088,'1071.2': 9878,'1072.1': 13159,'1072.2': 8358,'1073.1': 5368,'1073.2': 2615,'1080.2': 9397,'1086.2': 1303,'2018.1': 2414,'2018.2': 1771,'2028.2': 13710,'2031.2': 3264,'2034.1': 10264,'2034.2': 4670,'2036.1': 7219,'2050.2': 11707,'3038.1': 1388,'3038.2': 13104,'3041.1': 4085,'3041.2': 9160,'3042.1': 8629,'3042.2': 11298,'A-1501-03.A': 4083,'A-1501-03.B': 7952,'A-1505-99.A': 4714,'A-1505-99.B': 4938,'A-1507-168.B': 9238,'M-1506-106.B': 10223,'M-1506-108.A': 2704,'M-1506-108.B': 5056,'M-1506-110.A': 20939,'M-1506-110.B': 18435,'M-1507-113.B': 23343,'M-1507-118.A': 5895,'M-1507-118.B': 5758,'M-1507-119.A': 4404,'M-1507-119.B': 5842,'M-1507-120.A': 9952,'M-1507-120.B': 7825,'M-1507-124.A': 12450,'M-1507-125.B': 28041,'M-1507-126.A': 7669,'M-1507-133.B': 4964,'M-1507-134.B': 1380,'M-1507-136.A': 5557,'M-1507-147.A': 3113,'M-1507-147.B': 1340,'M-1507-154.A': 2848,'M-1507-154.B': 17416,'M-1507-155.A': 5535,'M-1507-155.B': 3792,'S-1409-41.B': 3476,'S-1409-52.B': 3584,'S-1409-57.A': 2065,'S-1410-61.B': 10758,'S-1410-62.A': 5886,'S-1410-62.B': 7680,'S-1410-67.A': 4104,'S-1410-70.A': 2970,'S-1410-70.B': 9990,'S-1410-71.A': 14024,'S-1410-71.B': 11120,'S-1410-72.A': 9835,'S-1410-72.B': 7124,'S-1410-75.A': 1693,'S-1410-75.B': 3334,'S-1410-76.A': 3849,'S-1410-76.B': 12268,'S-1410-84.A': 16988,'S-1410-84.B': 19123,'S-1504-86.A': 15359},'BC0b': {'1001.2': 5935,'1010.1': 34253,'1010.2': 8016,'1018.2': 7557,'1021.2': 25213,'1026.1': 5462,'1026.2': 742,'1029.1': 11265,'1029.2': 2133,'1048.2': 7906,'1049.2': 27375,'1057.2': 7353,'1065.1': 8505,'1065.2': 18643,'1067.1': 22754,'1067.2': 7441,'1071.1': 15505,'1071.2': 18355,'1072.1': 9349,'1072.2': 21841,'1073.1': 8474,'1073.2': 6273,'1080.2': 7820,'1086.2': 2231,'2018.1': 1978,'2018.2': 6516,'2028.2': 12559,'2031.2': 2537,'2034.1': 13301,'2034.2': 3128,'2036.1': 12757,'2050.2': 17553,'3038.1': 1286,'3038.2': 13197,'3041.1': 11141,'3041.2': 19938,'3042.1': 11437,'3042.2': 12662,'A-1501-03.A': 36185,'A-1501-03.B': 38130,'A-1505-99.A': 16054,'A-1505-99.B': 14821,'A-1507-168.B': 26276,'M-1506-106.B': 9390,'M-1506-108.A': 8822,'M-1506-108.B': 10076,'M-1506-110.A': 32208,'M-1506-110.B': 31193,'M-1507-113.B': 72979,'M-1507-118.A': 5367,'M-1507-118.B': 4684,'M-1507-119.A': 66612,'M-1507-119.B': 13348,'M-1507-120.A': 33713,'M-1507-120.B': 8025,'M-1507-124.A': 16145,'M-1507-125.B': 28633,'M-1507-126.A': 18907,'M-1507-133.B': 4885,'M-1507-134.B': 42507,'M-1507-136.A': 17102,'M-1507-147.A': 5015,'M-1507-147.B': 114078,'M-1507-154.A': 16207,'M-1507-154.B': 12175,'M-1507-155.A': 9714,'M-1507-155.B': 14034,'S-1409-41.B': 4865,'S-1409-52.B': 2994,'S-1409-57.A': 3328,'S-1410-61.B': 12545,'S-1410-62.A': 12655,'S-1410-62.B': 3591,'S-1410-67.A': 3109,'S-1410-70.A': 14745,'S-1410-70.B': 16226,'S-1410-71.A': 3979,'S-1410-71.B': 2415,'S-1410-72.A': 25956,'S-1410-72.B': 19851,'S-1410-75.A': 5027,'S-1410-75.B': 1560,'S-1410-76.A': 28781,'S-1410-76.B': 11400,'S-1410-84.A': 15048,'S-1410-84.B': 5149,'S-1504-86.A': 15101},'BC1': {'1001.2': 289306,'1010.1': 161496,'1010.2': 69964,'1018.2': 47739,'1021.2': 124848,'1026.1': 68643,'1026.2': 17652,'1029.1': 35123,'1029.2': 131421,'1048.2': 178320,'1049.2': 97477,'1057.2': 72803,'1065.1': 66952,'1065.2': 116619,'1067.1': 70103,'1067.2': 60019,'1071.1': 355549,'1071.2': 253142,'1072.1': 72188,'1072.2': 181514,'1073.1': 132905,'1073.2': 123315,'1080.2': 105312,'1086.2': 26294,'2018.1': 63484,'2018.2': 341118,'2028.2': 65058,'2031.2': 140441,'2034.1': 84939,'2034.2': 49515,'2036.1': 145002,'2050.2': 103857,'3038.1': 13941,'3038.2': 79499,'3041.1': 42726,'3041.2': 124551,'3042.1': 40899,'3042.2': 40565,'A-1501-03.A': 137056,'A-1501-03.B': 166252,'A-1505-99.A': 202502,'A-1505-99.B': 61795,'A-1507-168.B': 144826,'M-1506-106.B': 92482,'M-1506-108.A': 134468,'M-1506-108.B': 167032,'M-1506-110.A': 101216,'M-1506-110.B': 100130,'M-1507-113.B': 156389,'M-1507-118.A': 178511,'M-1507-118.B': 114049,'M-1507-119.A': 127491,'M-1507-119.B': 96218,'M-1507-120.A': 227099,'M-1507-120.B': 97021,'M-1507-124.A': 63121,'M-1507-125.B': 126164,'M-1507-126.A': 62577,'M-1507-133.B': 33532,'M-1507-134.B': 104437,'M-1507-136.A': 25218,'M-1507-147.A': 180090,'M-1507-147.B': 67312,'M-1507-154.A': 78943,'M-1507-154.B': 73775,'M-1507-155.A': 111586,'M-1507-155.B': 146237,'S-1409-41.B': 134373,'S-1409-52.B': 122611,'S-1409-57.A': 139990,'S-1410-61.B': 326334,'S-1410-62.A': 360458,'S-1410-62.B': 448642,'S-1410-67.A': 250696,'S-1410-70.A': 72425,'S-1410-70.B': 192340,'S-1410-71.A': 113476,'S-1410-71.B': 160124,'S-1410-72.A': 82178,'S-1410-72.B': 118238,'S-1410-75.A': 70348,'S-1410-75.B': 277236,'S-1410-76.A': 130693,'S-1410-76.B': 256835,'S-1410-84.A': 103621,'S-1410-84.B': 56052,'S-1504-86.A': 108613},'BC10': {'1001.2': 33754,'1010.1': 16476,'1010.2': 8935,'1018.2': 1221,'1021.2': 80753,'1026.1': 716,'1026.2': 334,'1029.1': 1008,'1029.2': 1008,'1048.2': 8115,'1049.2': 27643,'1057.2': 6681,'1065.1': 1020,'1065.2': 1269,'1067.1': 5771,'1067.2': 1800,'1071.1': 4905,'1071.2': 6513,'1072.1': 12068,'1072.2': 22154,'1073.1': 889,'1073.2': 380,'1080.2': 5208,'1086.2': 1359,'2018.1': 1577,'2018.2': 5306,'2028.2': 2675,'2031.2': 1284,'2034.1': 2081,'2034.2': 1229,'2036.1': 2548,'2050.2': 4020,'3038.1': 334,'3038.2': 943,'3041.1': 429,'3041.2': 3665,'3042.1': 845,'3042.2': 1700,'A-1501-03.A': 2357,'A-1501-03.B': 2798,'A-1505-99.A': 3528,'A-1505-99.B': 1093,'A-1507-168.B': 13760,'M-1506-106.B': 2380,'M-1506-108.A': 8673,'M-1506-108.B': 7305,'M-1506-110.A': 12250,'M-1506-110.B': 9403,'M-1507-113.B': 56811,'M-1507-118.A': 9763,'M-1507-118.B': 3591,'M-1507-119.A': 15052,'M-1507-119.B': 2156,'M-1507-120.A': 13108,'M-1507-120.B': 3872,'M-1507-124.A': 5413,'M-1507-125.B': 15676,'M-1507-126.A': 5279,'M-1507-133.B': 5271,'M-1507-134.B': 1087,'M-1507-136.A': 3641,'M-1507-147.A': 4673,'M-1507-147.B': 1316,'M-1507-154.A': 6519,'M-1507-154.B': 3229,'M-1507-155.A': 5602,'M-1507-155.B': 10569,'S-1409-41.B': 2057,'S-1409-52.B': 1593,'S-1409-57.A': 13432,'S-1410-61.B': 7979,'S-1410-62.A': 5516,'S-1410-62.B': 1783,'S-1410-67.A': 6542,'S-1410-70.A': 4102,'S-1410-70.B': 6543,'S-1410-71.A': 22790,'S-1410-71.B': 12016,'S-1410-72.A': 11283,'S-1410-72.B': 1319,'S-1410-75.A': 1938,'S-1410-75.B': 2400,'S-1410-76.A': 2461,'S-1410-76.B': 10497,'S-1410-84.A': 5015,'S-1410-84.B': 1202,'S-1504-86.A': 57069},'BC100': {'1001.2': 3286,'1010.1': 10947,'1010.2': 7687,'1018.2': 2765,'1021.2': 38762,'1026.1': 33677,'1026.2': 13154,'1029.1': 5563,'1029.2': 9174,'1048.2': 13501,'1049.2': 23698,'1057.2': 3075,'1065.1': 18148,'1065.2': 24837,'1067.1': 8748,'1067.2': 11369,'1071.1': 4122,'1071.2': 41568,'1072.1': 67820,'1072.2': 42658,'1073.1': 12069,'1073.2': 38510,'1080.2': 25111,'1086.2': 3059,'2018.1': 11287,'2018.2': 46781,'2028.2': 12872,'2031.2': 43654,'2034.1': 12293,'2034.2': 4663,'2036.1': 7529,'2050.2': 26721,'3038.1': 5641,'3038.2': 27469,'3041.1': 18354,'3041.2': 24639,'3042.1': 14481,'3042.2': 19247,'A-1501-03.A': 39180,'A-1501-03.B': 58928,'A-1505-99.A': 16431,'A-1505-99.B': 17782,'A-1507-168.B': 26221,'M-1506-106.B': 2450,'M-1506-108.A': 35528,'M-1506-108.B': 45668,'M-1506-110.A': 47633,'M-1506-110.B': 76327,'M-1507-113.B': 16337,'M-1507-118.A': 2003,'M-1507-118.B': 80529,'M-1507-119.A': 33802,'M-1507-119.B': 59446,'M-1507-120.A': 29498,'M-1507-120.B': 24406,'M-1507-124.A': 2117,'M-1507-125.B': 42188,'M-1507-126.A': 12968,'M-1507-133.B': 15551,'M-1507-134.B': 72534,'M-1507-136.A': 5452,'M-1507-147.A': 311,'M-1507-147.B': 102750,'M-1507-154.A': 18811,'M-1507-154.B': 14461,'M-1507-155.A': 4477,'M-1507-155.B': 6166,'S-1409-41.B': 3421,'S-1409-52.B': 6475,'S-1409-57.A': 140259,'S-1410-61.B': 17793,'S-1410-62.A': 74388,'S-1410-62.B': 21366,'S-1410-67.A': 5405,'S-1410-70.A': 65814,'S-1410-70.B': 155655,'S-1410-71.A': 10657,'S-1410-71.B': 8941,'S-1410-72.A': 6123,'S-1410-72.B': 24783,'S-1410-75.A': 2620,'S-1410-75.B': 337,'S-1410-76.A': 45598,'S-1410-76.B': 5859,'S-1410-84.A': 9742,'S-1410-84.B': 20088,'S-1504-86.A': 5997},'BC101': {'1001.2': 12807,'1010.1': 16089,'1010.2': 28135,'1018.2': 7601,'1021.2': 29398,'1026.1': 8161,'1026.2': 2873,'1029.1': 3002,'1029.2': 23365,'1048.2': 24211,'1049.2': 12970,'1057.2': 18223,'1065.1': 5070,'1065.2': 15738,'1067.1': 11808,'1067.2': 18002,'1071.1': 10263,'1071.2': 20660,'1072.1': 30239,'1072.2': 23493,'1073.1': 9962,'1073.2': 9259,'1080.2': 57914,'1086.2': 10509,'2018.1': 8176,'2018.2': 13699,'2028.2': 23270,'2031.2': 11688,'2034.1': 7456,'2034.2': 4892,'2036.1': 5897,'2050.2': 24212,'3038.1': 822,'3038.2': 8498,'3041.1': 3023,'3041.2': 12399,'3042.1': 3497,'3042.2': 11858,'A-1501-03.A': 2538,'A-1501-03.B': 4517,'A-1505-99.A': 3186,'A-1505-99.B': 6374,'A-1507-168.B': 9800,'M-1506-106.B': 9571,'M-1506-108.A': 7972,'M-1506-108.B': 12036,'M-1506-110.A': 21647,'M-1506-110.B': 18908,'M-1507-113.B': 13387,'M-1507-118.A': 20837,'M-1507-118.B': 35744,'M-1507-119.A': 6812,'M-1507-119.B': 13414,'M-1507-120.A': 24473,'M-1507-120.B': 9791,'M-1507-124.A': 30794,'M-1507-125.B': 29891,'M-1507-126.A': 9887,'M-1507-133.B': 7909,'M-1507-134.B': 3400,'M-1507-136.A': 5674,'M-1507-147.A': 74549,'M-1507-147.B': 2853,'M-1507-154.A': 5432,'M-1507-154.B': 23947,'M-1507-155.A': 5888,'M-1507-155.B': 4172,'S-1409-41.B': 14725,'S-1409-52.B': 19069,'S-1409-57.A': 12003,'S-1410-61.B': 21601,'S-1410-62.A': 18475,'S-1410-62.B': 21780,'S-1410-67.A': 30727,'S-1410-70.A': 3549,'S-1410-70.B': 21779,'S-1410-71.A': 185412,'S-1410-71.B': 78044,'S-1410-72.A': 8771,'S-1410-72.B': 1100,'S-1410-75.A': 6739,'S-1410-75.B': 53418,'S-1410-76.A': 24381,'S-1410-76.B': 68226,'S-1410-84.A': 12141,'S-1410-84.B': 4067,'S-1504-86.A': 12996},'BC102': {'1001.2': 11219,'1010.1': 6212,'1010.2': 5608,'1018.2': 771,'1021.2': 4191,'1026.1': 1567,'1026.2': 378,'1029.1': 584,'1029.2': 534,'1048.2': 3344,'1049.2': 27804,'1057.2': 179,'1065.1': 2359,'1065.2': 6919,'1067.1': 2335,'1067.2': 301,'1071.1': 1975,'1071.2': 4508,'1072.1': 6226,'1072.2': 6786,'1073.1': 5397,'1073.2': 1984,'1080.2': 422,'1086.2': 261,'2018.1': 700,'2018.2': 298,'2028.2': 1575,'2031.2': 4126,'2034.1': 5626,'2034.2': 972,'2036.1': 773,'2050.2': 9878,'3038.1': 462,'3038.2': 1571,'3041.1': 5059,'3041.2': 3557,'3042.1': 35644,'3042.2': 9463,'A-1501-03.A': 997,'A-1501-03.B': 8698,'A-1505-99.A': 1430,'A-1505-99.B': 2856,'A-1507-168.B': 2674,'M-1506-106.B': 380,'M-1506-108.A': 6261,'M-1506-108.B': 4569,'M-1506-110.A': 3987,'M-1506-110.B': 2212,'M-1507-113.B': 391,'M-1507-118.A': 411,'M-1507-118.B': 2462,'M-1507-119.A': 2932,'M-1507-119.B': 5597,'M-1507-120.A': 8961,'M-1507-120.B': 4788,'M-1507-124.A': 2628,'M-1507-125.B': 2169,'M-1507-126.A': 3431,'M-1507-133.B': 4702,'M-1507-134.B': 667,'M-1507-136.A': 5041,'M-1507-147.A': 152,'M-1507-147.B': 819,'M-1507-154.A': 233,'M-1507-154.B': 8963,'M-1507-155.A': 2123,'M-1507-155.B': 1654,'S-1409-41.B': 1625,'S-1409-52.B': 2464,'S-1409-57.A': 3097,'S-1410-61.B': 4198,'S-1410-62.A': 4627,'S-1410-62.B': 652,'S-1410-67.A': 682,'S-1410-70.A': 1507,'S-1410-70.B': 2692,'S-1410-71.A': 8955,'S-1410-71.B': 3057,'S-1410-72.A': 5078,'S-1410-72.B': 4436,'S-1410-75.A': 2217,'S-1410-75.B': 76,'S-1410-76.A': 2578,'S-1410-76.B': 1591,'S-1410-84.A': 430,'S-1410-84.B': 3293,'S-1504-86.A': 1022},'BC103': {'1001.2': 47,'1010.1': 4121,'1010.2': 2117,'1018.2': 2568,'1021.2': 25001,'1026.1': 2212,'1026.2': 1535,'1029.1': 4580,'1029.2': 1152,'1048.2': 8221,'1049.2': 27980,'1057.2': 1380,'1065.1': 11013,'1065.2': 23179,'1067.1': 3541,'1067.2': 2241,'1071.1': 9779,'1071.2': 15474,'1072.1': 8528,'1072.2': 10340,'1073.1': 5147,'1073.2': 14658,'1080.2': 2112,'1086.2': 406,'2018.1': 5170,'2018.2': 30349,'2028.2': 6192,'2031.2': 5190,'2034.1': 12565,'2034.2': 2884,'2036.1': 6942,'2050.2': 17112,'3038.1': 540,'3038.2': 11259,'3041.1': 4713,'3041.2': 14039,'3042.1': 31265,'3042.2': 23319,'A-1501-03.A': 6818,'A-1501-03.B': 14055,'A-1505-99.A': 8326,'A-1505-99.B': 9245,'A-1507-168.B': 6074,'M-1506-106.B': 2501,'M-1506-108.A': 19348,'M-1506-108.B': 40895,'M-1506-110.A': 26179,'M-1506-110.B': 10612,'M-1507-113.B': 7493,'M-1507-118.A': 2033,'M-1507-118.B': 17832,'M-1507-119.A': 69373,'M-1507-119.B': 10119,'M-1507-120.A': 10813,'M-1507-120.B': 1515,'M-1507-124.A': 5464,'M-1507-125.B': 3609,'M-1507-126.A': 1456,'M-1507-133.B': 6704,'M-1507-134.B': 22506,'M-1507-136.A': 2103,'M-1507-147.A': 2314,'M-1507-147.B': 40975,'M-1507-154.A': 10102,'M-1507-154.B': 9181,'M-1507-155.A': 8766,'M-1507-155.B': 3702,'S-1409-41.B': 2009,'S-1409-52.B': 4647,'S-1409-57.A': 12433,'S-1410-61.B': 10995,'S-1410-62.A': 24021,'S-1410-62.B': 15828,'S-1410-67.A': 1468,'S-1410-70.A': 28608,'S-1410-70.B': 24070,'S-1410-71.A': 9840,'S-1410-71.B': 1263,'S-1410-72.A': 10484,'S-1410-72.B': 13375,'S-1410-75.A': 3502,'S-1410-75.B': 207,'S-1410-76.A': 14776,'S-1410-76.B': 659,'S-1410-84.A': 9886,'S-1410-84.B': 8042,'S-1504-86.A': 14472},'BC104': {'1001.2': 0,'1010.1': 35,'1010.2': 313,'1018.2': 35,'1021.2': 9,'1026.1': 288,'1026.2': 1,'1029.1': 0,'1029.2': 0,'1048.2': 12,'1049.2': 415,'1057.2': 37,'1065.1': 162,'1065.2': 241,'1067.1': 47,'1067.2': 22,'1071.1': 6,'1071.2': 0,'1072.1': 1,'1072.2': 7,'1073.1': 1,'1073.2': 0,'1080.2': 0,'1086.2': 0,'2018.1': 10,'2018.2': 129,'2028.2': 127,'2031.2': 57,'2034.1': 12,'2034.2': 0,'2036.1': 64,'2050.2': 64,'3038.1': 0,'3038.2': 449,'3041.1': 0,'3041.2': 20,'3042.1': 3,'3042.2': 7620,'A-1501-03.A': 21,'A-1501-03.B': 49,'A-1505-99.A': 17,'A-1505-99.B': 0,'A-1507-168.B': 0,'M-1506-106.B': 0,'M-1506-108.A': 767,'M-1506-108.B': 407,'M-1506-110.A': 1,'M-1506-110.B': 3,'M-1507-113.B': 2069,'M-1507-118.A': 2,'M-1507-118.B': 110,'M-1507-119.A': 3081,'M-1507-119.B': 1222,'M-1507-120.A': 1340,'M-1507-120.B': 151,'M-1507-124.A': 0,'M-1507-125.B': 1,'M-1507-126.A': 0,'M-1507-133.B': 1012,'M-1507-134.B': 8,'M-1507-136.A': 8,'M-1507-147.A': 385,'M-1507-147.B': 4,'M-1507-154.A': 205,'M-1507-154.B': 24,'M-1507-155.A': 0,'M-1507-155.B': 349,'S-1409-41.B': 3,'S-1409-52.B': 640,'S-1409-57.A': 3,'S-1410-61.B': 3,'S-1410-62.A': 226,'S-1410-62.B': 12,'S-1410-67.A': 33,'S-1410-70.A': 0,'S-1410-70.B': 2965,'S-1410-71.A': 0,'S-1410-71.B': 0,'S-1410-72.A': 4,'S-1410-72.B': 54,'S-1410-75.A': 218,'S-1410-75.B': 170,'S-1410-76.A': 99,'S-1410-76.B': 481,'S-1410-84.A': 0,'S-1410-84.B': 9,'S-1504-86.A': 0},'BC105': {'1001.2': 0,'1010.1': 3813,'1010.2': 5828,'1018.2': 232,'1021.2': 412,'1026.1': 4,'1026.2': 13,'1029.1': 95,'1029.2': 8,'1048.2': 2127,'1049.2': 9956,'1057.2': 18,'1065.1': 469,'1065.2': 2961,'1067.1': 988,'1067.2': 439,'1071.1': 14668,'1071.2': 3951,'1072.1': 415,'1072.2': 182,'1073.1': 1288,'1073.2': 74,'1080.2': 122,'1086.2': 696,'2018.1': 21,'2018.2': 173,'2028.2': 3865,'2031.2': 59,'2034.1': 1097,'2034.2': 255,'2036.1': 1710,'2050.2': 8870,'3038.1': 307,'3038.2': 445,'3041.1': 984,'3041.2': 5100,'3042.1': 1481,'3042.2': 139,'A-1501-03.A': 351,'A-1501-03.B': 1183,'A-1505-99.A': 11933,'A-1505-99.B': 4359,'A-1507-168.B': 8246,'M-1506-106.B': 980,'M-1506-108.A': 699,'M-1506-108.B': 4191,'M-1506-110.A': 3477,'M-1506-110.B': 36780,'M-1507-113.B': 8960,'M-1507-118.A': 543,'M-1507-118.B': 324,'M-1507-119.A': 7886,'M-1507-119.B': 1448,'M-1507-120.A': 15303,'M-1507-120.B': 1669,'M-1507-124.A': 9443,'M-1507-125.B': 3264,'M-1507-126.A': 430,'M-1507-133.B': 176,'M-1507-134.B': 952,'M-1507-136.A': 163,'M-1507-147.A': 310,'M-1507-147.B': 16023,'M-1507-154.A': 1068,'M-1507-154.B': 2703,'M-1507-155.A': 71589,'M-1507-155.B': 351,'S-1409-41.B': 956,'S-1409-52.B': 85,'S-1409-57.A': 168,'S-1410-61.B': 1492,'S-1410-62.A': 1215,'S-1410-62.B': 562,'S-1410-67.A': 218,'S-1410-70.A': 925,'S-1410-70.B': 643,'S-1410-71.A': 87,'S-1410-71.B': 428,'S-1410-72.A': 232,'S-1410-72.B': 973,'S-1410-75.A': 11737,'S-1410-75.B': 21,'S-1410-76.A': 1169,'S-1410-76.B': 1833,'S-1410-84.A': 108,'S-1410-84.B': 6469,'S-1504-86.A': 13818}} X = pd.DataFrame(data) + 1 # Shape of X where rows are samples and columns are features n,m = X.shape index_samples = X.index index_features = X.columns X_values = X.values # A = np.empty((n, m**2 - m)) # A[:] = np.nan # Not the most efficient way but the best way to show what I'm trying to do A = defaultdict(dict) for row_pos, a in enumerate(X_values): id_sample = index_samples[row_pos] # col_pos = 0 for i in range(m): a_i = a[i] i_feature = index_features[i] for j in range( m): if i != j: a_j = a[j] j_feature = index_features[j] # A[row_pos, col_pos] = a_i - a_j A[id_sample]["{}/{}".format(i_feature, j_feature)] = np.log(a_i/a_j) # col_pos += 1 df = pd.DataFrame(A).T df.iloc[:5,:5] BC0a/BC0b BC0a/BC1 BC0a/BC10 BC0a/BC100 BC0a/BC101 1001.2 -0.364454 -4.250907 -2.102548 0.226606 -1.133489 1010.1 -1.621745 -3.172429 -0.889908 -0.481099 -0.866140 1010.2 0.381012 -1.785419 0.272488 0.422915 -0.874474 1018.2 -0.263715 -2.106878 1.558403 0.741490 -0.269520 1021.2 -0.975581 -2.575287 -2.139590 -1.405648 -1.129143
You can use np.triu_indices to get all possible combinations by indexing thusly: i_index, j_index = np.triu_indices(m, 1) Using the fact that logarithms were invented to be able to do log(a / b) = log(a) - log(b), you can now do: df = pd.DataFrame(data=np.log(X_values[:, i_index]) - np.log(X_values[:, j_index]), index=X.index.copy(), columns=[f'{a}/{b}' for a, b in zip(X.columns[i_index], X.columns[j_index])]) While it's more elegant in my opinion, you can just as easily keep the original formulation of the ratio: np.log(X_values[:, i_index] / X_values[:, j_index]) Notice that this is half of the data that your answer contains. The other half is just the inverse ratios, which appear as negatives in the log table. If you absolutely insist on computing those (even though it's quite wasteful), you can adjust i_index and j_index accordingly: i_index = np.repeat(np.arange(m), m - 1) j_index = np.delete(np.tile(np.arange(m), m), slice(None, None, m + 1)) Either way, your entire code can be expressed in about three lines.
Trasform a Graph from a dictionary form to a Matrix in python
{'X050412_1_0000_1_4': {'0-4': 64.9516586968339, '2-3': 47.573389284767174, '1-2': 29.414889381739993, '0-1': 20.137499912849165, '0-2': 48.36841808990657, '0-3': 65.07665932728878, '1-3': 58.326301639483376, '3-4': 42.477331382632784, '1-4': 47.380533137671634, '2-4': 18.995337464493748}, 'X050412_1_0000_1_5': {'0-4': 74.45548880223673, '2-3': 42.921786085506746, '1-2': 22.297921437434468, '0-1': 16.755486281812296, '0-2': 31.640265835324445, '0-3': 49.53140665618129, '1-3': 55.29908155494086, '3-4': 25.4227462686469, '1-4': 78.22578893907047, '2-4': 61.73419510554909}, 'X050412_1_0000_1_6': {'0-4': 53.36246109354403, '2-3': 49.125438624810265, '1-2': 26.33647295558006, '0-1': 27.165619713343553, '0-2': 53.21604990987211, '0-3': 31.972624680029007, '1-3': 34.38869991232585, '3-4': 35.126226807472506, '1-4': 33.294628738732015, '2-4': 23.60048014617499}} Suppose that I've a graph represented like this and i Want to create to convert it into matrix form.
for k,v in dataset.distances.items(): if k not in diz2: diz2[k] = {} for k2,v2 in v.items(): res = tuple(map(int, k2.split('-'))) if res not in diz2: diz2[k][res] = v2 def convert(diz5): temp_x = max([cord[0] for cord in diz5.keys()]) temp_y = max([cord[1] for cord in diz5.keys()]) res2 = [[0] * (temp_y + 1) for ele in range(temp_x + 1)] for (i, j), val in diz5.items(): res2[i][j] = val #print("The dictionary after creation of Matrix : " + str(res2)) return res2
How to solve warning message in Gekko due to m.connection?
I am using m.connection to estimate variables initial conditions but I am getting 12 warning messages like: Moreover, the APM file shows: I am not sure how to solve these messages. I am following this explanation "If pos1 or pos2 is not None, the associated var must be a GEKKO variable and the position is the (0-indexed) time-discretized index of the variable" to write m.Connection(var1,var2,pos1=None,pos2=None,node1='end',node2='end'). https://gekko.readthedocs.io/en/latest/quick_start.html#connections Thanks in advance. from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt import math as math import pandas as pd tm1 = [0, 0.0667,0.5,1,4, 22.61667] mca1 = [5.68, 3.48, 3.24, 3.36, 2.96, 1.96] tm2 = [0, 0.08333,0.5,1,4.25 , 22.8167] mca2 = [5.68, 4.20, 4.04, 4.00, 3.76, 2.88] tm3 = [0,0.08333,0.5,1,4.33 , 22.9500] mca3 = [5.68, 4.64, 4.52, 4.56, 4.24, 3.72] tm4 = [0,0.08333,0.5,1,4.0833 , 23.0833] mca4 =[18.90,15.4,14.3,15.10,13.50, 10.90] tm5 = [0,0.08333,0.5,1,4.5, 23.2167] mca5 =[18.90, 15.5, 16.30, 16, 14.70, 13.00] tm6 = [0,0.08333,0.5,1,4.6667, 23.3333 ] mca6 = [18.90, 15.8, 11.70,15.5,12, 9.5 ] df1=pd.DataFrame({'time':tm1,'x1':mca1}) df2=pd.DataFrame({'time':tm2,'x2':mca2}) df3=pd.DataFrame({'time':tm3,'x3':mca3}) df4=pd.DataFrame({'time':tm4,'x4':mca4}) df5=pd.DataFrame({'time':tm5,'x5':mca5}) df6=pd.DataFrame({'time':tm6,'x6':mca6}) df1.set_index('time',inplace=True) df2.set_index('time',inplace=True) df3.set_index('time',inplace=True) df4.set_index('time',inplace=True) df5.set_index('time',inplace=True) df6.set_index('time',inplace=True) #simulation time points dfx = pd.DataFrame({'time':np.linspace(0,25,101)}) dfx.set_index('time',inplace=True) #merge dataframes dfxx=dfx.join(df1,how='outer') dfxxx=dfxx.join(df2,how='outer') dfxxxx=dfxxx.join(df3,how='outer') dfxxxxx=dfxxxx.join(df4,how='outer') dfxxxxxx=dfxxxxx.join(df5,how='outer') df=dfxxxxxx.join(df6,how='outer') # get True (1) or False (0) for measurement df['meas1']=(df['x1'].values==df['x1'].values).astype(int) df['meas2']=(df['x2'].values==df['x2'].values).astype(int) df['meas3']=(df['x3'].values==df['x3'].values).astype(int) df['meas4']=(df['x4'].values==df['x4'].values).astype(int) df['meas5']=(df['x5'].values==df['x5'].values).astype(int) df['meas6']=(df['x6'].values==df['x6'].values).astype(int) #replace NaN with zeros df0=df.fillna(value=0) m = GEKKO() m.time = df0.index.values meas1 = m.Param(df0['meas1'].values) meas2 = m.Param(df0['meas2'].values) meas3 = m.Param(df0['meas3'].values) meas4 = m.Param(df0['meas4'].values) meas5 = m.Param(df0['meas5'].values) meas6 = m.Param(df0['meas6'].values) #adjustable Parameters kf=m.FV(1.3,lb=0.01,ub=10) ks=m.FV(1.3,lb=0.01,ub=10) cnf01=m.FV(1.3,lb=0.01,ub=10) cns01=m.FV(1.3,lb=0.01,ub=10) #constrains cnf02=m.FV(value=cnf01*0.5,lb=cnf01*0.5, ub=cnf01*0.5) cns02=m.FV(value=cns01*0.5,lb=cns01*0.5, ub=cns01*0.5) cnf03=m.FV(value=cnf01*0.25,lb=cnf01*0.25, ub=cnf01*0.25) cns03=m.FV(value=cns01*0.25,lb=cns01*0.25, ub=cns01*0.25) cnf04=m.FV(value=cnf01,lb=cnf01, ub=cnf01) cns04=m.FV(value=cns01,lb=cns01, ub=cns01) cnf05=m.FV(value=cnf01*0.5,lb=cnf01*0.5, ub=cnf01*0.5) cns05=m.FV(value=cns01*0.5,lb=cns01*0.5, ub=cns01*0.5) cnf06=m.FV(value=cnf01*0.25,lb=cnf01*0.25, ub=cnf01*0.25) cns06=m.FV(value=cns01*0.25,lb=cns01*0.25, ub=cns01*0.25) #Variables c1 = m.Var(value=mca1[0]) c2 = m.Var(value=mca2[0]) c3 = m.Var(value=mca3[0]) c4 = m.Var(value=mca4[0]) c5 = m.Var(value=mca5[0]) c6 = m.Var(value=mca6[0]) cm1 = m.Param(df0['x1'].values) cm2 = m.Param(df0['x2'].values) cm3 = m.Param(df0['x3'].values) cm4 = m.Param(df0['x4'].values) cm5 = m.Param(df0['x5'].values) cm6 = m.Param(df0['x6'].values) m.Minimize((meas1*(c1-cm1)**2)+(meas2*(c2-cm2)**2)\ +(meas3*(c3-cm3)**2)+(meas4*(c4-cm4)**2)\ +(meas5*(c5-cm5)**2)+(meas6*(c6-cm6)**2)) cnf1=m.Var(value=cnf01,fixed_initial=False) cns1=m.Var(value=cns01,fixed_initial=False) cnf2=m.Var(value=cnf02,fixed_initial=False) cns2=m.Var(value=cns02,fixed_initial=False) cnf3=m.Var(value=cnf03,fixed_initial=False) cns3=m.Var(value=cns03,fixed_initial=False) cnf4=m.Var(value=cnf04,fixed_initial=False) cns4=m.Var(value=cns04,fixed_initial=False) cnf5=m.Var(value=cnf05,fixed_initial=False) cns5=m.Var(value=cns05,fixed_initial=False) cnf6=m.Var(value=cnf06,fixed_initial=False) cns6=m.Var(value=cns06,fixed_initial=False) #Equations t = m.Param(value=m.time) m.Connection(cnf1,cnf01,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cnf2,cnf02,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cnf3,cnf03,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cnf4,cnf04,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cnf5,cnf05,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cnf6,cnf06,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cns1,cns01,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cns2,cns02,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cns3,cns03,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cns4,cns04,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cns5,cns05,pos1=0,pos2=0,node1=1,node2=1) m.Connection(cns6,cns06,pos1=0,pos2=0,node1=1,node2=1) m.Equation(cnf1.dt()==-kf*c1*cnf1) m.Equation(cns1.dt()==-ks*c1*cns1) m.Equation(c1.dt()==cnf1.dt()+cns1.dt()) m.Equation(cnf2.dt()==-kf*c2*cnf2) m.Equation(cns2.dt()==-ks*c2*cns2) m.Equation(c2.dt()==cnf2.dt()+cns2.dt()) m.Equation(cnf3.dt()==-kf*c3*cnf3) m.Equation(cns3.dt()==-ks*c3*cns3) m.Equation(c3.dt()==cnf3.dt()+cns3.dt()) m.Equation(cnf4.dt()==-kf*c4*cnf4) m.Equation(cns4.dt()==-ks*c4*cns4) m.Equation(c4.dt()==cnf4.dt()+cns4.dt()) m.Equation(cnf5.dt()==-kf*c5*cnf5) m.Equation(cns5.dt()==-ks*c5*cns5) m.Equation(c5.dt()==cnf5.dt()+cns5.dt()) m.Equation(cnf6.dt()==-kf*c6*cnf6) m.Equation(cns6.dt()==-ks*c6*cns6) m.Equation(c6.dt()==cnf6.dt()+cns6.dt()) if True: kf.STATUS=1 ks.STATUS=1 cnf01.STATUS=1 cns01.STATUS=1 cnf02.STATUS=1 cns02.STATUS=1 cnf03.STATUS=1 cns03.STATUS=1 cnf04.STATUS=1 cns04.STATUS=1 cnf05.STATUS=1 cns05.STATUS=1 cnf06.STATUS=1 cns06.STATUS=1 #Options m.options.SOLVER = 1 #IPOPT solver m.options.IMODE = 5 #Dynamic Simultaneous - estimation = MHE m.options.EV_TYPE = 2 #absolute error m.options.NODES = 3 #collocation nodes (2,5) m.solve(disp=True) m.open_folder() print('Final SSE Objective: ' + str(m.options.objfcnval)) print('Solution') print('cnf01 = ' + str(cnf01.value[0])) print('cns01 = ' + str(cns01.value[0])) print('kf = ' + str(kf.value[0])) print('ks = ' + str(ks.value[0])) print('cns02 = '+ str(cns02.value[0])) print('cnf02 = '+ str(cnf02.value[0])) print('cns03 = '+ str(cns03.value[0])) print('cnf03 = '+ str(cnf03.value[0])) print('cns04 = '+ str(cns04.value[0])) print('cnf04 = '+ str(cnf04.value[0])) print('cns05 = '+ str(cns05.value[0])) print('cnf05 = '+ str(cnf05.value[0])) print('cns06 = '+ str(cns06.value[0])) print('cnf06 = '+ str(cnf06.value[0])) plt.figure(1,figsize=(8,5)) plt.plot(m.time,c1.value,'r',label='Predicted c1') plt.plot(m.time,c2.value,'y',label='Predicted c2') plt.plot(m.time,c3.value,'c',label='Predicted c3') plt.plot(m.time,c4.value,'g',label='Predicted c4') plt.plot(m.time,c5.value,'b',label='Predicted c5') plt.plot(m.time,c6.value,'m',label='Predicted c6') plt.plot(tm1,mca1,'rx',label='Meas c1') plt.plot(tm2,mca2,'yx',label='Meas c2') plt.plot(tm3,mca3,'cx',label='Meas c3') plt.plot(tm4,mca4,'go',label='Meas c4') plt.plot(tm5,mca5,'bo',label='Meas c5') plt.plot(tm6,mca6,'mo',label='Meas c6') plt.xlabel('time (h)') plt.ylabel('Concentration (mgCl2/L)') plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=2)
The underlying node structure has a 1-index instead of a 0-index that is common in Python. Using pos1=1 and pos2=1 resolves the warnings. m.Connection(cnf1,cnf01,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf2,cnf02,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf3,cnf03,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf4,cnf04,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf5,cnf05,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf6,cnf06,pos1=1,pos2=1,node1=1,node2=1) Another issue is that Gekko variables shouldn't generally be used to initialize other values. I recommend setting x0=1.3 and using that float to initialize the variables. Change m.Var() to m.SV() to avoid reclassification of m.Var() as an m.FV() during the connection. The m.SV() is a promoted type of variable that is at the same level of precedence as the m.FV(). Here is a complete script although the results don't look optimal. from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt import math as math import pandas as pd tm1 = [0, 0.0667,0.5,1,4, 22.61667] mca1 = [5.68, 3.48, 3.24, 3.36, 2.96, 1.96] tm2 = [0, 0.08333,0.5,1,4.25 , 22.8167] mca2 = [5.68, 4.20, 4.04, 4.00, 3.76, 2.88] tm3 = [0,0.08333,0.5,1,4.33 , 22.9500] mca3 = [5.68, 4.64, 4.52, 4.56, 4.24, 3.72] tm4 = [0,0.08333,0.5,1,4.0833 , 23.0833] mca4 =[18.90,15.4,14.3,15.10,13.50, 10.90] tm5 = [0,0.08333,0.5,1,4.5, 23.2167] mca5 =[18.90, 15.5, 16.30, 16, 14.70, 13.00] tm6 = [0,0.08333,0.5,1,4.6667, 23.3333 ] mca6 = [18.90, 15.8, 11.70,15.5,12, 9.5 ] df1=pd.DataFrame({'time':tm1,'x1':mca1}) df2=pd.DataFrame({'time':tm2,'x2':mca2}) df3=pd.DataFrame({'time':tm3,'x3':mca3}) df4=pd.DataFrame({'time':tm4,'x4':mca4}) df5=pd.DataFrame({'time':tm5,'x5':mca5}) df6=pd.DataFrame({'time':tm6,'x6':mca6}) df1.set_index('time',inplace=True) df2.set_index('time',inplace=True) df3.set_index('time',inplace=True) df4.set_index('time',inplace=True) df5.set_index('time',inplace=True) df6.set_index('time',inplace=True) #simulation time points dfx = pd.DataFrame({'time':np.linspace(0,25,101)}) dfx.set_index('time',inplace=True) #merge dataframes dfxx=dfx.join(df1,how='outer') dfxxx=dfxx.join(df2,how='outer') dfxxxx=dfxxx.join(df3,how='outer') dfxxxxx=dfxxxx.join(df4,how='outer') dfxxxxxx=dfxxxxx.join(df5,how='outer') df=dfxxxxxx.join(df6,how='outer') # get True (1) or False (0) for measurement df['meas1']=(df['x1'].values==df['x1'].values).astype(int) df['meas2']=(df['x2'].values==df['x2'].values).astype(int) df['meas3']=(df['x3'].values==df['x3'].values).astype(int) df['meas4']=(df['x4'].values==df['x4'].values).astype(int) df['meas5']=(df['x5'].values==df['x5'].values).astype(int) df['meas6']=(df['x6'].values==df['x6'].values).astype(int) #replace NaN with zeros df0=df.fillna(value=0) m = GEKKO() m.time = df0.index.values meas1 = m.Param(df0['meas1'].values) meas2 = m.Param(df0['meas2'].values) meas3 = m.Param(df0['meas3'].values) meas4 = m.Param(df0['meas4'].values) meas5 = m.Param(df0['meas5'].values) meas6 = m.Param(df0['meas6'].values) #adjustable Parameters kf=m.FV(1.3,lb=0.01,ub=10) ks=m.FV(1.3,lb=0.01,ub=10) x0 = 1.3 cnf01=m.FV(x0,lb=0.01,ub=10) cns01=m.FV(x0,lb=0.01,ub=10) #constrains cnf02=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5) cns02=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5) cnf03=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25) cns03=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25) cnf04=m.FV(value=x0,lb=x0, ub=x0) cns04=m.FV(value=x0,lb=x0, ub=x0) cnf05=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5) cns05=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5) cnf06=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25) cns06=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25) #Variables c1 = m.SV(value=mca1[0]) c2 = m.SV(value=mca2[0]) c3 = m.SV(value=mca3[0]) c4 = m.SV(value=mca4[0]) c5 = m.SV(value=mca5[0]) c6 = m.SV(value=mca6[0]) cm1 = m.Param(df0['x1'].values) cm2 = m.Param(df0['x2'].values) cm3 = m.Param(df0['x3'].values) cm4 = m.Param(df0['x4'].values) cm5 = m.Param(df0['x5'].values) cm6 = m.Param(df0['x6'].values) m.Minimize((meas1*(c1-cm1)**2)+(meas2*(c2-cm2)**2)\ +(meas3*(c3-cm3)**2)+(meas4*(c4-cm4)**2)\ +(meas5*(c5-cm5)**2)+(meas6*(c6-cm6)**2)) cnf1=m.SV(value=x0,fixed_initial=False) cns1=m.SV(value=x0,fixed_initial=False) cnf2=m.SV(value=x0,fixed_initial=False) cns2=m.SV(value=x0,fixed_initial=False) cnf3=m.SV(value=x0,fixed_initial=False) cns3=m.SV(value=x0,fixed_initial=False) cnf4=m.SV(value=x0,fixed_initial=False) cns4=m.SV(value=x0,fixed_initial=False) cnf5=m.SV(value=x0,fixed_initial=False) cns5=m.SV(value=x0,fixed_initial=False) cnf6=m.SV(value=x0,fixed_initial=False) cns6=m.SV(value=x0,fixed_initial=False) #Equations t = m.Param(value=m.time) m.Connection(cnf1,cnf01,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf2,cnf02,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf3,cnf03,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf4,cnf04,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf5,cnf05,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cnf6,cnf06,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cns1,cns01,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cns2,cns02,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cns3,cns03,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cns4,cns04,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cns5,cns05,pos1=1,pos2=1,node1=1,node2=1) m.Connection(cns6,cns06,pos1=1,pos2=1,node1=1,node2=1) m.Equation(cnf1.dt()==-kf*c1*cnf1) m.Equation(cns1.dt()==-ks*c1*cns1) m.Equation(c1.dt()==cnf1.dt()+cns1.dt()) m.Equation(cnf2.dt()==-kf*c2*cnf2) m.Equation(cns2.dt()==-ks*c2*cns2) m.Equation(c2.dt()==cnf2.dt()+cns2.dt()) m.Equation(cnf3.dt()==-kf*c3*cnf3) m.Equation(cns3.dt()==-ks*c3*cns3) m.Equation(c3.dt()==cnf3.dt()+cns3.dt()) m.Equation(cnf4.dt()==-kf*c4*cnf4) m.Equation(cns4.dt()==-ks*c4*cns4) m.Equation(c4.dt()==cnf4.dt()+cns4.dt()) m.Equation(cnf5.dt()==-kf*c5*cnf5) m.Equation(cns5.dt()==-ks*c5*cns5) m.Equation(c5.dt()==cnf5.dt()+cns5.dt()) m.Equation(cnf6.dt()==-kf*c6*cnf6) m.Equation(cns6.dt()==-ks*c6*cns6) m.Equation(c6.dt()==cnf6.dt()+cns6.dt()) #Options m.options.SOLVER = 1 # APOPT solver m.options.IMODE = 5 # Dynamic Simultaneous - estimation = MHE m.options.EV_TYPE = 2 # Squared error m.options.NODES = 3 # Collocation nodes (2,5) if True: kf.STATUS=1 ks.STATUS=1 cnf01.STATUS=1 cns01.STATUS=1 cnf02.STATUS=1 cns02.STATUS=1 cnf03.STATUS=1 cns03.STATUS=1 cnf04.STATUS=1 cns04.STATUS=1 cnf05.STATUS=1 cns05.STATUS=1 cnf06.STATUS=1 cns06.STATUS=1 m.options.TIME_SHIFT = 0 try: m.solve(disp=True) except: print("don't stop when not finding cnf01...cnf06") #m.open_folder() print('Final SSE Objective: ' + str(m.options.objfcnval)) print('Solution') print('cnf01 = ' + str(cnf1.value[0])) print('cns01 = ' + str(cns1.value[0])) print('kf = ' + str(kf.value[0])) print('ks = ' + str(ks.value[0])) print('cns02 = '+ str(cns2.value[0])) print('cnf02 = '+ str(cnf2.value[0])) print('cns03 = '+ str(cns3.value[0])) print('cnf03 = '+ str(cnf3.value[0])) print('cns04 = '+ str(cns4.value[0])) print('cnf04 = '+ str(cnf4.value[0])) print('cns05 = '+ str(cns5.value[0])) print('cnf05 = '+ str(cnf5.value[0])) print('cns06 = '+ str(cns6.value[0])) print('cnf06 = '+ str(cnf6.value[0])) plt.figure(1,figsize=(8,5)) plt.plot(m.time,c1.value,'r',label='Predicted c1') plt.plot(m.time,c2.value,'y',label='Predicted c2') plt.plot(m.time,c3.value,'c',label='Predicted c3') plt.plot(m.time,c4.value,'g',label='Predicted c4') plt.plot(m.time,c5.value,'b',label='Predicted c5') plt.plot(m.time,c6.value,'m',label='Predicted c6') plt.plot(tm1,mca1,'rx',label='Meas c1') plt.plot(tm2,mca2,'yx',label='Meas c2') plt.plot(tm3,mca3,'cx',label='Meas c3') plt.plot(tm4,mca4,'go',label='Meas c4') plt.plot(tm5,mca5,'bo',label='Meas c5') plt.plot(tm6,mca6,'mo',label='Meas c6') plt.xlabel('time (h)') plt.ylabel('Concentration (mgCl2/L)') plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=2) plt.show()