Flatten Array from 2d to 1D with same length - python

I need to flat (2,300) array to (,300) array. Maybe flat is not right term but, I want to add each element in order then get mean of these 2 element and create (,300).I think manually change could be work , but since its loop i couldnt find solution yet.
Tried to read documentation but couldnt find it so far and search for numpy options but couldnt find solution yet.
[[ 1.56250000e-01 -2.18750000e-01 -2.81250000e-01 -2.26562500e-01
-8.78906250e-02 4.08203125e-01 1.66992188e-01 1.68457031e-02
3.20312500e-01 -5.22460938e-02 -3.19824219e-02 5.90820312e-02
1.32812500e-01 -1.55273438e-01 2.87109375e-01 1.30859375e-01
8.88671875e-02 4.56542969e-02 -4.37011719e-02 -4.96093750e-01
5.85937500e-02 1.35742188e-01 -1.81884766e-02 7.32421875e-02
-2.16796875e-01 5.66406250e-02 -1.66015625e-01 4.66308594e-02
-1.04492188e-01 1.65039062e-01 -2.61718750e-01 3.27148438e-02
-2.12890625e-01 3.63281250e-01 -2.80761719e-02 -2.40234375e-01
1.21093750e-01 2.75390625e-01 -4.49218750e-02 -1.44531250e-01
-1.61132812e-01 1.75781250e-01 1.69921875e-01 2.94921875e-01
3.41796875e-01 3.02124023e-03 -4.90722656e-02 9.88769531e-03
2.21679688e-01 -3.53515625e-01 -2.83203125e-01 -1.35742188e-01
-2.53906250e-02 -2.14843750e-01 1.51367188e-01 3.73046875e-01
4.68750000e-02 2.40478516e-02 -1.77734375e-01 2.49023438e-01
-5.85937500e-02 9.94873047e-03 -1.93359375e-01 1.38671875e-01
-1.30859375e-01 -4.34570312e-02 -5.54199219e-02 2.50000000e-01
-2.36328125e-01 3.12500000e-01 4.66796875e-01 -6.05468750e-02
2.25585938e-01 -2.26562500e-01 -4.21875000e-01 -2.11914062e-01
-1.37695312e-01 -1.56250000e-01 2.39257812e-02 -3.55468750e-01
-2.28515625e-01 3.82812500e-01 8.49609375e-02 4.00390625e-01
-4.19921875e-02 9.91210938e-02 6.74438477e-03 3.59375000e-01
9.42382812e-02 6.93359375e-02 -2.16796875e-01 -2.34375000e-01
-2.02148438e-01 9.94873047e-03 1.38671875e-01 1.24511719e-01
2.89062500e-01 1.10839844e-01 -6.49414062e-02 -4.08203125e-01
-4.37500000e-01 2.33154297e-02 1.02539062e-01 2.19726562e-01
-2.10937500e-01 9.47265625e-02 2.94921875e-01 3.14941406e-02
2.92968750e-01 -1.28906250e-01 -1.65039062e-01 -1.66992188e-01
1.49414062e-01 -1.44531250e-01 -1.22558594e-01 6.39648438e-02
-2.48046875e-01 -3.30078125e-01 3.93066406e-02 -1.69921875e-01
4.80957031e-02 -2.01171875e-01 -4.10156250e-01 1.65039062e-01
2.29492188e-01 -1.45507812e-01 -2.53906250e-01 -2.27539062e-01
1.33789062e-01 6.83593750e-02 -8.69140625e-02 4.15039062e-02
-1.96289062e-01 1.69921875e-01 6.54296875e-02 6.34765625e-02
1.22070312e-01 1.28906250e-01 -4.29687500e-02 -1.12792969e-01
1.22070312e-01 -1.85546875e-01 -2.89062500e-01 -2.91015625e-01
-1.98242188e-01 2.50000000e-01 2.46093750e-01 2.14843750e-01
-1.81640625e-01 6.98852539e-03 3.35937500e-01 -1.90429688e-01
-1.95312500e-02 4.23828125e-01 -1.04980469e-01 -7.81250000e-02
-1.31835938e-01 -1.21582031e-01 -1.50756836e-02 1.92382812e-01
9.03320312e-03 1.70898438e-01 2.83203125e-01 -1.08398438e-01
-5.07812500e-02 -4.58984375e-02 -1.44531250e-01 -1.38671875e-01
-3.55468750e-01 -6.44531250e-02 -1.21582031e-01 2.12402344e-02
-4.68750000e-01 2.34375000e-02 -1.48437500e-01 -1.45507812e-01
6.98242188e-02 -4.23828125e-01 -3.73535156e-02 1.45507812e-01
-3.10546875e-01 1.82617188e-01 1.31835938e-01 6.49414062e-02
3.04687500e-01 2.11914062e-01 -1.84570312e-01 -7.22656250e-02
1.39648438e-01 5.22460938e-02 -1.01562500e-01 -2.07031250e-01
-3.98437500e-01 -2.91015625e-01 -1.37695312e-01 3.76953125e-01
-1.37695312e-01 -1.91406250e-01 -4.05273438e-02 -5.61523438e-02
2.20947266e-02 3.93066406e-02 -3.39843750e-01 9.32617188e-02
3.75000000e-01 3.66210938e-02 -2.14843750e-02 -1.67968750e-01
-3.12500000e-01 1.81884766e-02 1.75781250e-01 -2.79541016e-02
-4.02343750e-01 -5.20019531e-02 8.88671875e-02 -5.00000000e-01
-1.25976562e-01 -9.37500000e-02 -2.85156250e-01 -1.66015625e-01
-1.92382812e-01 2.38281250e-01 -7.78198242e-03 2.00195312e-01
-1.07421875e-01 2.71484375e-01 -1.25000000e-01 -1.30462646e-03
-3.88183594e-02 -2.34375000e-01 -1.07910156e-01 4.68750000e-02
8.78906250e-03 -2.53906250e-01 6.78710938e-02 -6.15234375e-02
-5.20019531e-02 -9.96093750e-02 2.59765625e-01 -4.07714844e-02
1.03515625e-01 1.22680664e-02 -1.94335938e-01 -3.65234375e-01
2.57812500e-01 -4.19921875e-02 6.73828125e-02 1.73828125e-01
3.29589844e-02 -2.13867188e-01 4.83989716e-05 7.86132812e-02
7.86132812e-02 1.38671875e-01 1.07910156e-01 -4.10156250e-01
1.72851562e-01 -1.78710938e-01 6.56127930e-03 1.19628906e-01
2.55859375e-01 -4.76074219e-02 5.32226562e-02 -3.20312500e-01
-2.57812500e-01 1.11816406e-01 -1.67968750e-01 -2.73437500e-01
-4.19921875e-01 2.07031250e-01 8.10546875e-02 -1.47460938e-01
6.28662109e-03 -5.61523438e-02 -6.25610352e-03 1.25000000e-01
3.10058594e-02 2.83203125e-02 -3.90625000e-01 -1.04003906e-01
1.82617188e-01 4.61425781e-02 -1.11389160e-03 2.50000000e-01
1.77734375e-01 -1.07910156e-01 -3.97949219e-02 -1.62109375e-01
1.44653320e-02 4.49218750e-01 -1.12792969e-01 1.76757812e-01
-1.74804688e-01 -3.04687500e-01 3.94531250e-01 -5.93261719e-02
1.15234375e-01 4.05883789e-03 -2.12890625e-01 2.98828125e-01]
[ 2.01171875e-01 -2.75878906e-02 -1.13281250e-01 1.90429688e-01
-2.98828125e-01 -1.91406250e-01 1.83593750e-01 -2.43164062e-01
3.61328125e-02 -9.32617188e-02 -1.60156250e-01 2.42919922e-02
4.71191406e-02 -1.25976562e-01 -2.24609375e-02 1.20117188e-01
-3.57055664e-03 -8.44726562e-02 1.56250000e-01 -4.39453125e-01
3.53515625e-01 2.23632812e-01 -3.44238281e-02 1.06445312e-01
-3.82812500e-01 3.02734375e-01 -1.39648438e-01 3.98437500e-01
1.69921875e-01 2.72216797e-02 -1.04492188e-01 1.77734375e-01
-1.93359375e-01 -1.87500000e-01 -6.05468750e-02 -2.29492188e-01
3.61328125e-01 3.02734375e-01 -1.21093750e-01 -2.23632812e-01
3.81469727e-05 8.88671875e-02 1.31835938e-01 -5.90820312e-02
2.43164062e-01 3.00292969e-02 1.88476562e-01 2.36816406e-02
2.20703125e-01 -2.96875000e-01 -3.63281250e-01 -1.71875000e-01
1.56250000e-01 -3.45703125e-01 -3.02734375e-01 2.34375000e-01
-1.01074219e-01 -1.25000000e-01 -6.34765625e-02 4.16015625e-01
-3.02734375e-01 -3.84765625e-01 -1.16699219e-01 1.08886719e-01
9.57031250e-02 1.26953125e-01 -6.00585938e-02 -2.59765625e-01
2.61718750e-01 1.72851562e-01 1.63085938e-01 2.91015625e-01
-5.85937500e-02 -3.30078125e-01 -2.15148926e-03 -5.46875000e-01
3.08593750e-01 1.92382812e-01 1.02539062e-01 4.12597656e-02
7.27539062e-02 2.37304688e-01 5.05371094e-02 4.15039062e-03
-4.19921875e-02 -9.71679688e-02 -3.57055664e-03 7.03125000e-02
2.96875000e-01 -1.36718750e-01 -2.68554688e-02 1.39648438e-01
-2.27539062e-01 -1.55273438e-01 -2.66113281e-02 2.16796875e-01
4.41406250e-01 -1.62109375e-01 5.51757812e-02 1.35498047e-02
-8.49609375e-02 -1.75781250e-01 -4.39453125e-01 6.73828125e-02
-2.31445312e-01 1.45507812e-01 3.61328125e-02 5.44433594e-02
2.53906250e-01 -2.91015625e-01 6.25000000e-02 -3.20312500e-01
1.57226562e-01 -6.10351562e-02 1.37695312e-01 2.35351562e-01
-2.13623047e-02 1.37695312e-01 -7.14111328e-03 -2.36328125e-01
9.88769531e-03 -1.04492188e-01 -2.43164062e-01 2.32421875e-01
4.27246094e-02 -3.81469727e-04 7.86132812e-02 1.96289062e-01
1.05957031e-01 7.32421875e-02 3.73535156e-02 -1.70898438e-01
-1.80664062e-01 3.88183594e-02 2.43164062e-01 -7.32421875e-02
1.74804688e-01 -1.21459961e-02 2.04101562e-01 -1.35742188e-01
9.13085938e-02 -5.50781250e-01 -7.81250000e-02 -4.22363281e-02
2.14843750e-01 -1.63085938e-01 1.20117188e-01 -5.95092773e-03
3.34472656e-02 -2.53906250e-01 3.02734375e-01 5.93261719e-02
2.18750000e-01 -5.00488281e-03 1.46484375e-01 -9.57031250e-02
-1.68945312e-01 2.52685547e-02 9.09423828e-03 1.01562500e-01
1.07910156e-01 -6.88476562e-02 2.91015625e-01 7.56835938e-03
-1.95312500e-01 -3.00781250e-01 -1.73828125e-01 2.89062500e-01
-1.13769531e-01 -1.27929688e-01 -2.59765625e-01 2.14843750e-01
2.61230469e-02 4.56542969e-02 -1.78710938e-01 -2.39257812e-01
2.19726562e-02 -2.26562500e-01 -3.35937500e-01 -6.15234375e-02
-4.14062500e-01 1.22070312e-01 -1.56250000e-01 6.64062500e-02
6.32812500e-01 9.42382812e-02 -2.77343750e-01 1.30859375e-01
-7.76367188e-02 1.77734375e-01 4.24804688e-02 -1.41601562e-01
-1.72851562e-01 -1.39648438e-01 1.77734375e-01 9.03320312e-02
-4.72656250e-01 -7.17773438e-02 -1.12792969e-01 1.42578125e-01
1.06445312e-01 1.31835938e-01 -2.32421875e-01 -3.78417969e-02
4.37500000e-01 -1.16699219e-01 -4.73632812e-02 2.10937500e-01
6.34765625e-02 -9.88769531e-03 -1.25976562e-01 -4.76074219e-03
-1.14257812e-01 1.13281250e-01 4.17480469e-02 -3.57421875e-01
2.27539062e-01 6.10351562e-02 -1.64062500e-01 4.46777344e-02
-4.53125000e-01 2.92968750e-01 -1.26953125e-01 2.04101562e-01
-3.41796875e-01 1.62109375e-01 1.68945312e-01 -1.95312500e-01
-1.32812500e-01 -3.69140625e-01 -1.73828125e-01 2.22656250e-01
-5.41992188e-02 9.03320312e-02 -8.00781250e-02 -9.27734375e-02
-9.81445312e-02 -1.60156250e-01 5.23437500e-01 8.05664062e-02
1.38671875e-01 1.97265625e-01 -4.04296875e-01 -4.04296875e-01
-9.08203125e-02 -2.62451172e-02 5.10253906e-02 3.90625000e-01
3.65234375e-01 -2.50000000e-01 -4.02343750e-01 5.20019531e-02
-1.11694336e-02 -3.47656250e-01 2.08007812e-01 -2.55859375e-01
4.90722656e-02 -2.65625000e-01 -1.24023438e-01 -3.26171875e-01
1.44531250e-01 -1.88476562e-01 -8.05664062e-02 -2.85156250e-01
1.33789062e-01 1.56250000e-01 3.27148438e-02 -2.81982422e-02
-1.05468750e-01 -4.56542969e-02 2.21679688e-01 3.22265625e-01
-2.06054688e-01 -1.20117188e-01 -9.37500000e-02 1.00585938e-01
1.90429688e-01 -3.63281250e-01 -2.75390625e-01 1.87500000e-01
1.63085938e-01 -7.51953125e-02 3.59375000e-01 1.75781250e-01
1.45507812e-01 2.69531250e-01 -3.43750000e-01 -2.92968750e-01
1.06445312e-01 3.29589844e-02 2.23632812e-01 2.59765625e-01
-2.71484375e-01 2.84423828e-02 2.11914062e-01 -4.68750000e-01
7.71484375e-02 -3.10546875e-01 -1.62109375e-01 4.17968750e-01]]
To change this
[ 1.56250000e-01 -2.18750000e-01 -2.81250000e-01 -2.26562500e-01
-8.78906250e-02 4.08203125e-01 1.66992188e-01 1.68457031e-02
3.20312500e-01 -5.22460938e-02 -3.19824219e-02 5.90820312e-02
1.32812500e-01 -1.55273438e-01 2.87109375e-01 1.30859375e-01
8.88671875e-02 4.56542969e-02 -4.37011719e-02 -4.96093750e-01
5.85937500e-02 1.35742188e-01 -1.81884766e-02 7.32421875e-02
-2.16796875e-01 5.66406250e-02 -1.66015625e-01 4.66308594e-02
-1.04492188e-01 1.65039062e-01 -2.61718750e-01 3.27148438e-02
-2.12890625e-01 3.63281250e-01 -2.80761719e-02 -2.40234375e-01
1.21093750e-01 2.75390625e-01 -4.49218750e-02 -1.44531250e-01
-1.61132812e-01 1.75781250e-01 1.69921875e-01 2.94921875e-01
3.41796875e-01 3.02124023e-03 -4.90722656e-02 9.88769531e-03
2.21679688e-01 -3.53515625e-01 -2.83203125e-01 -1.35742188e-01
-2.53906250e-02 -2.14843750e-01 1.51367188e-01 3.73046875e-01
4.68750000e-02 2.40478516e-02 -1.77734375e-01 2.49023438e-01
-5.85937500e-02 9.94873047e-03 -1.93359375e-01 1.38671875e-01
-1.30859375e-01 -4.34570312e-02 -5.54199219e-02 2.50000000e-01
-2.36328125e-01 3.12500000e-01 4.66796875e-01 -6.05468750e-02
2.25585938e-01 -2.26562500e-01 -4.21875000e-01 -2.11914062e-01
-1.37695312e-01 -1.56250000e-01 2.39257812e-02 -3.55468750e-01
-2.28515625e-01 3.82812500e-01 8.49609375e-02 4.00390625e-01
-4.19921875e-02 9.91210938e-02 6.74438477e-03 3.59375000e-01
9.42382812e-02 6.93359375e-02 -2.16796875e-01 -2.34375000e-01
-2.02148438e-01 9.94873047e-03 1.38671875e-01 1.24511719e-01
2.89062500e-01 1.10839844e-01 -6.49414062e-02 -4.08203125e-01
-4.37500000e-01 2.33154297e-02 1.02539062e-01 2.19726562e-01
-2.10937500e-01 9.47265625e-02 2.94921875e-01 3.14941406e-02
2.92968750e-01 -1.28906250e-01 -1.65039062e-01 -1.66992188e-01
1.49414062e-01 -1.44531250e-01 -1.22558594e-01 6.39648438e-02
-2.48046875e-01 -3.30078125e-01 3.93066406e-02 -1.69921875e-01
4.80957031e-02 -2.01171875e-01 -4.10156250e-01 1.65039062e-01
2.29492188e-01 -1.45507812e-01 -2.53906250e-01 -2.27539062e-01
1.33789062e-01 6.83593750e-02 -8.69140625e-02 4.15039062e-02
-1.96289062e-01 1.69921875e-01 6.54296875e-02 6.34765625e-02
1.22070312e-01 1.28906250e-01 -4.29687500e-02 -1.12792969e-01
1.22070312e-01 -1.85546875e-01 -2.89062500e-01 -2.91015625e-01
-1.98242188e-01 2.50000000e-01 2.46093750e-01 2.14843750e-01
-1.81640625e-01 6.98852539e-03 3.35937500e-01 -1.90429688e-01
-1.95312500e-02 4.23828125e-01 -1.04980469e-01 -7.81250000e-02
-1.31835938e-01 -1.21582031e-01 -1.50756836e-02 1.92382812e-01
9.03320312e-03 1.70898438e-01 2.83203125e-01 -1.08398438e-01
-5.07812500e-02 -4.58984375e-02 -1.44531250e-01 -1.38671875e-01
-3.55468750e-01 -6.44531250e-02 -1.21582031e-01 2.12402344e-02
-4.68750000e-01 2.34375000e-02 -1.48437500e-01 -1.45507812e-01
6.98242188e-02 -4.23828125e-01 -3.73535156e-02 1.45507812e-01
-3.10546875e-01 1.82617188e-01 1.31835938e-01 6.49414062e-02
3.04687500e-01 2.11914062e-01 -1.84570312e-01 -7.22656250e-02
1.39648438e-01 5.22460938e-02 -1.01562500e-01 -2.07031250e-01
-3.98437500e-01 -2.91015625e-01 -1.37695312e-01 3.76953125e-01
-1.37695312e-01 -1.91406250e-01 -4.05273438e-02 -5.61523438e-02
2.20947266e-02 3.93066406e-02 -3.39843750e-01 9.32617188e-02
3.75000000e-01 3.66210938e-02 -2.14843750e-02 -1.67968750e-01
-3.12500000e-01 1.81884766e-02 1.75781250e-01 -2.79541016e-02
-4.02343750e-01 -5.20019531e-02 8.88671875e-02 -5.00000000e-01
-1.25976562e-01 -9.37500000e-02 -2.85156250e-01 -1.66015625e-01
-1.92382812e-01 2.38281250e-01 -7.78198242e-03 2.00195312e-01
-1.07421875e-01 2.71484375e-01 -1.25000000e-01 -1.30462646e-03
-3.88183594e-02 -2.34375000e-01 -1.07910156e-01 4.68750000e-02
8.78906250e-03 -2.53906250e-01 6.78710938e-02 -6.15234375e-02
-5.20019531e-02 -9.96093750e-02 2.59765625e-01 -4.07714844e-02
1.03515625e-01 1.22680664e-02 -1.94335938e-01 -3.65234375e-01
2.57812500e-01 -4.19921875e-02 6.73828125e-02 1.73828125e-01
3.29589844e-02 -2.13867188e-01 4.83989716e-05 7.86132812e-02
7.86132812e-02 1.38671875e-01 1.07910156e-01 -4.10156250e-01
1.72851562e-01 -1.78710938e-01 6.56127930e-03 1.19628906e-01
2.55859375e-01 -4.76074219e-02 5.32226562e-02 -3.20312500e-01
-2.57812500e-01 1.11816406e-01 -1.67968750e-01 -2.73437500e-01
-4.19921875e-01 2.07031250e-01 8.10546875e-02 -1.47460938e-01
6.28662109e-03 -5.61523438e-02 -6.25610352e-03 1.25000000e-01
3.10058594e-02 2.83203125e-02 -3.90625000e-01 -1.04003906e-01
1.82617188e-01 4.61425781e-02 -1.11389160e-03 2.50000000e-01
1.77734375e-01 -1.07910156e-01 -3.97949219e-02 -1.62109375e-01
1.44653320e-02 4.49218750e-01 -1.12792969e-01 1.76757812e-01
-1.74804688e-01 -3.04687500e-01 3.94531250e-01 -5.93261719e-02
1.15234375e-01 4.05883789e-03 -2.12890625e-01 2.98828125e-01]
Need to do this in loop which is
`
def preprocess_and_vectorize(text):
# remove stop words and lemmatize the text
doc = nlp(text)
filtered_tokens = []
for token in doc:
if token.is_stop or token.is_punct:
continue
filtered_tokens.append(token.lemma_)
return wv[filtered_tokens]
This is suppose to be easy while using gensim attribute after return.
return wv.get_mean_vector(filtered_tokens)

Related

Gekko seems to ignore the single equation

I've been using Gekko for the last three years for optimization purposes. As a matter of fact, I'm using Gekko version 1.0.4. Recently, I've been trying to solve an ESG-MVP problem developed by (Vo, He et al. 2019):
Considering sum(wesg) should be equal to 1 and 0 ≤ wesg_i ≤ 1 for iε[0, 1, 2,..., 23]. In this model, I'm trying to
find the best values of wesg_i for the model above. Hence, according to the information provided, I have to find the value of 24 variables through an optimization model. My code is available for you to test on your computer with all the vital data needed.
Libraries
from gekko import GEKKO
import numpy as np
import pandas as pd
Data Section
Make sure copy it all.
esg_scores_df = pd.DataFrame({'Score 2019': {'MSFT': 7,'PEP': 7,'TSLA': 4,'AMZN': 6,'LKQ': 3,'ABMD': 3,'MSI': 10,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 4,'GS': 6,'EQIX': 9,'EA': 7,'AAP': 6,'TEL': 9,'DG': 6,'EXR': 5,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 9},
'Score 2020': {'MSFT': 6,'PEP': 11,'TSLA': 4,'AMZN': 6,'LKQ': 4,'ABMD': 4,'MSI': 8,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 3,'GS': 6,'EQIX': 9,'EA': 6,'AAP': 7,'TEL': 9,'DG': 6,'EXR': 4,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 7}})
predicted_return_dataframe = pd.DataFrame({'MSFT': 1.0982472257593677e-15,'PEP': 4.567069595849647e-09,'TSLA': 7.439258841202596e-10,'AMZN': 3.176883309676764e-07,'LKQ': 4.825709334830293e-05,'ABMD': 2.0608058642685837e-05,'MSI': -3.1789250961959136e-12,'PH': -2.257237871892785e-07,'NKE': -4.737530217609426e-07,'TM': 9.951932427172896e-07,'EOG': 1.184074639824261e-08,'GOOGL': -1.7027206923083418e-10,'NFLX': 2.948344729885545e-05,'GS': 1.458862302453713e-07,'EQIX': 4.620207503091301e-07,'EA': -1.297137640100873e-06,'AAP': -3.493153382990658e-07,'TEL': 8.202463568291799e-08,'DG': 2.743802312024829e-05,'EXR': 1.971994818465128e-11,'MDLZ': 1.578772827915881e-08,'FIS': 1.3314663987166631e-08,'CRL': 4.112083587242872e-05,'RCL': -1.470829206425942e-09} , index = [0])
r = pd.DataFrame({'MSFT': -0.029838842874655123,'PEP': -0.012265555866403165,'TSLA': -0.053599428210690636,'AMZN': -0.02614876342440773,'LKQ': -0.026658034482507052,'ABMD': -0.017274321994365294,'MSI': -0.018624198080929168,'PH': -0.02871952268633362,'NKE': -0.028820168231794074,'TM': -0.011208781085200492,'EOG': -0.04606883798249617,'GOOGL': -0.027983366801701926,'NFLX': -0.0070609860042608165,'GS': -0.02429261616692462,'EQIX': -0.015181566244607758,'EA': -0.012009295653284258,'AAP': -0.011979871239295382,'TEL': -0.021782414925879602,'DG': -0.009820085124020328,'EXR': -0.012233190959318829,'MDLZ': -0.015917631061436933,'FIS': -0.023877815976094417,'CRL': -0.020169902356185498,'RCL': -0.06831607178882856} , index = [0])
sigma = pd.DataFrame(np.array([[ 1.02188994e-04, 3.16399586e-05, 8.54243667e-05,
7.14148287e-05, 3.55313650e-05, 5.44796564e-05,
3.39128448e-05, 6.65003271e-05, 5.42932808e-05,
2.29333906e-05, 4.26407605e-05, 7.92791756e-05,
9.28910574e-05, 6.36770764e-05, 3.75024646e-05,
5.13720672e-05, 1.33646720e-05, 6.21865710e-05,
4.72888751e-05, 2.35546749e-06, 2.49700413e-05,
6.74503236e-05, 7.08948785e-05, 5.55096135e-05],
[ 3.16399586e-05, 6.47824823e-05, -2.57461009e-05,
1.81931187e-05, 2.11795049e-05, 1.01708683e-05,
3.33739243e-05, 1.23893822e-05, 2.74825418e-05,
1.35954411e-05, 2.37108310e-06, 2.34208728e-05,
1.96972793e-05, 1.09335123e-05, 4.26042775e-05,
2.76849308e-05, 6.01821651e-06, 8.04387109e-06,
3.10892437e-05, 2.79970364e-05, 3.70572613e-05,
3.08215527e-05, 2.57244480e-05, 8.72002781e-06],
[ 8.54243667e-05, -2.57461009e-05, 8.84138653e-04,
6.16478906e-05, -1.78063009e-05, 3.62712388e-05,
-6.15200573e-05, 8.63597848e-05, 2.12571109e-05,
3.21749681e-05, 6.54693912e-05, 7.95298035e-05,
1.53917874e-04, 8.06306277e-05, -5.27893372e-06,
2.60899474e-05, 4.29017379e-05, 6.13689353e-05,
2.30097086e-06, -4.45963115e-05, -2.83669072e-06,
2.95395217e-05, 4.53889239e-05, 3.11615533e-05],
[ 7.14148287e-05, 1.81931187e-05, 6.16478906e-05,
1.19357840e-04, 5.48763301e-05, 5.55161292e-05,
2.77288469e-05, 6.25231790e-05, 5.19494452e-05,
1.93193913e-05, 4.28946368e-05, 7.00210371e-05,
9.92756795e-05, 6.54630027e-05, 1.73935405e-05,
4.46819063e-05, 2.94199398e-05, 5.76749375e-05,
3.75634244e-05, -5.53847280e-06, 9.72193169e-06,
4.42756733e-05, 4.21469376e-05, 4.35344231e-05],
[ 3.55313650e-05, 2.11795049e-05, -1.78063009e-05,
5.48763301e-05, 3.49607174e-04, 1.58156275e-04,
-1.64565378e-05, 5.95602984e-05, 3.36138721e-05,
2.25095973e-05, 4.90144491e-05, 5.60207136e-05,
3.73119885e-05, 8.65010497e-05, -1.68774498e-05,
1.06928038e-05, 3.53732120e-05, 8.46071106e-05,
3.89200328e-05, -1.10740797e-05, 4.20238479e-06,
2.07371361e-05, 2.27640364e-05, 7.39017715e-05],
[ 5.44796564e-05, 1.01708683e-05, 3.62712388e-05,
5.55161292e-05, 1.58156275e-04, 1.12266763e-03,
2.49786917e-05, 5.60053839e-05, -2.57686022e-05,
3.00855419e-05, 2.09079416e-04, 1.21797841e-04,
3.80689080e-06, 1.17305259e-04, -4.95192897e-05,
5.56059321e-05, 2.87765760e-06, 1.13640096e-04,
-2.73690072e-05, 2.17458024e-05, -1.24009254e-05,
6.11287657e-05, 1.12849096e-04, 1.17234141e-04],
[ 3.39128448e-05, 3.33739243e-05, -6.15200573e-05,
2.77288469e-05, -1.64565378e-05, 2.49786917e-05,
1.84271573e-04, -2.99455473e-06, 3.56885701e-05,
1.53451236e-05, 3.20222217e-06, 3.00619247e-05,
5.17902613e-05, 1.69851832e-05, 7.75007583e-05,
7.49839347e-06, -3.78485373e-05, -1.28942631e-05,
4.36657573e-05, 3.97590833e-05, 4.04704969e-05,
4.05201790e-05, 4.01384618e-05, 4.82818631e-06],
[ 6.65003271e-05, 1.23893822e-05, 8.63597848e-05,
6.25231790e-05, 5.95602984e-05, 5.60053839e-05,
-2.99455473e-06, 2.86568704e-04, 6.55278686e-05,
4.08938883e-05, 1.18822482e-04, 9.32577766e-05,
7.92498191e-05, 1.38967662e-04, -1.64128182e-05,
4.16393158e-05, 1.06300676e-04, 1.31672888e-04,
6.64839109e-05, -5.14108669e-05, -1.03044572e-05,
1.59107041e-05, 7.65457600e-05, 1.31611693e-04],
[ 5.42932808e-05, 2.74825418e-05, 2.12571109e-05,
5.19494452e-05, 3.36138721e-05, -2.57686022e-05,
3.56885701e-05, 6.55278686e-05, 1.36374334e-04,
1.36781102e-05, 2.38544176e-05, 5.90104054e-05,
6.20754315e-05, 5.84082445e-05, 3.96422393e-05,
2.96064418e-05, 5.54456912e-05, 5.69396332e-05,
7.28867485e-05, 7.92870807e-06, 3.79528145e-05,
3.38036564e-05, 3.47015652e-05, 3.86545249e-05],
[ 2.29333906e-05, 1.35954411e-05, 3.21749681e-05,
1.93193913e-05, 2.25095973e-05, 3.00855419e-05,
1.53451236e-05, 4.08938883e-05, 1.36781102e-05,
5.30619573e-05, 4.42693706e-05, 2.92937393e-05,
1.15184525e-05, 3.04141154e-05, 1.11981894e-06,
1.29388215e-05, 1.41913521e-05, 2.37704063e-05,
1.27983719e-05, -3.53963791e-06, 5.41505031e-06,
1.21874870e-05, 2.04099948e-05, 2.89151466e-05],
[ 4.26407605e-05, 2.37108310e-06, 6.54693912e-05,
4.28946368e-05, 4.90144491e-05, 2.09079416e-04,
3.20222217e-06, 1.18822482e-04, 2.38544176e-05,
4.42693706e-05, 5.01946804e-04, 7.79572195e-05,
7.61563242e-05, 1.31597522e-04, -2.43789107e-05,
6.77962026e-05, 5.39745318e-05, 9.05223133e-05,
-1.88433584e-06, -1.63784490e-05, -1.04001201e-05,
1.79450881e-05, 5.33951974e-05, 1.07102393e-04],
[ 7.92791756e-05, 2.34208728e-05, 7.95298035e-05,
7.00210371e-05, 5.60207136e-05, 1.21797841e-04,
3.00619247e-05, 9.32577766e-05, 5.90104054e-05,
2.92937393e-05, 7.79572195e-05, 1.10688236e-04,
8.13294795e-05, 8.72981821e-05, 2.13737337e-05,
4.33439061e-05, 3.06090710e-05, 7.60853329e-05,
4.44363057e-05, 3.33458671e-06, 1.60908360e-05,
4.09067616e-05, 5.72051871e-05, 6.05239478e-05],
[ 9.28910574e-05, 1.96972793e-05, 1.53917874e-04,
9.92756795e-05, 3.73119885e-05, 3.80689080e-06,
5.17902613e-05, 7.92498191e-05, 6.20754315e-05,
1.15184525e-05, 7.61563242e-05, 8.13294795e-05,
3.77348891e-04, 8.99709029e-05, 2.58732673e-05,
8.24015922e-05, 2.77333081e-05, 4.74146019e-05,
3.76467363e-05, -5.60031564e-06, 2.25653918e-05,
6.22679966e-05, 3.92394407e-05, 6.47067147e-05],
[ 6.36770764e-05, 1.09335123e-05, 8.06306277e-05,
6.54630027e-05, 8.65010497e-05, 1.17305259e-04,
1.69851832e-05, 1.38967662e-04, 5.84082445e-05,
3.04141154e-05, 1.31597522e-04, 8.72981821e-05,
8.99709029e-05, 1.54802097e-04, -1.57072336e-05,
3.58129141e-05, 5.52667399e-05, 1.02065825e-04,
4.96942712e-05, -2.44193897e-05, -5.66188177e-06,
2.35884764e-05, 5.15584350e-05, 9.98376706e-05],
[ 3.75024646e-05, 4.26042775e-05, -5.27893372e-06,
1.73935405e-05, -1.68774498e-05, -4.95192897e-05,
7.75007583e-05, -1.64128182e-05, 3.96422393e-05,
1.11981894e-06, -2.43789107e-05, 2.13737337e-05,
2.58732673e-05, -1.57072336e-05, 1.54605175e-04,
1.07085192e-05, -1.14274871e-05, -1.26204468e-05,
2.50810750e-05, 5.60340845e-05, 4.60520336e-05,
4.29696767e-05, 5.33004299e-05, -1.56063893e-05],
[ 5.13720672e-05, 2.76849308e-05, 2.60899474e-05,
4.46819063e-05, 1.06928038e-05, 5.56059321e-05,
7.49839347e-06, 4.16393158e-05, 2.96064418e-05,
1.29388215e-05, 6.77962026e-05, 4.33439061e-05,
8.24015922e-05, 3.58129141e-05, 1.07085192e-05,
1.32544808e-04, 3.15561503e-05, 3.62681876e-05,
1.76503394e-05, 1.90982407e-07, 1.02992114e-05,
2.81341342e-05, 2.81970597e-05, 3.94413355e-05],
[ 1.33646720e-05, 6.01821651e-06, 4.29017379e-05,
2.94199398e-05, 3.53732120e-05, 2.87765760e-06,
-3.78485373e-05, 1.06300676e-04, 5.54456912e-05,
1.41913521e-05, 5.39745318e-05, 3.06090710e-05,
2.77333081e-05, 5.52667399e-05, -1.14274871e-05,
3.15561503e-05, 2.71861487e-04, 6.04272650e-05,
3.55256428e-05, -2.37123843e-05, -1.99844011e-05,
-2.02943319e-07, -4.12746174e-07, 6.80701129e-05],
[ 6.21865710e-05, 8.04387109e-06, 6.13689353e-05,
5.76749375e-05, 8.46071106e-05, 1.13640096e-04,
-1.28942631e-05, 1.31672888e-04, 5.69396332e-05,
2.37704063e-05, 9.05223133e-05, 7.60853329e-05,
4.74146019e-05, 1.02065825e-04, -1.26204468e-05,
3.62681876e-05, 6.04272650e-05, 1.67171047e-04,
5.06392219e-05, -1.02673299e-05, 1.64715372e-05,
2.50897861e-05, 5.81836032e-05, 1.00294572e-04],
[ 4.72888751e-05, 3.10892437e-05, 2.30097086e-06,
3.75634244e-05, 3.89200328e-05, -2.73690072e-05,
4.36657573e-05, 6.64839109e-05, 7.28867485e-05,
1.27983719e-05, -1.88433584e-06, 4.44363057e-05,
3.76467363e-05, 4.96942712e-05, 2.50810750e-05,
1.76503394e-05, 3.55256428e-05, 5.06392219e-05,
1.99771178e-04, 4.70710752e-06, 3.76160380e-05,
2.30014580e-05, 3.12554634e-05, 5.94948441e-06],
[ 2.35546749e-06, 2.79970364e-05, -4.45963115e-05,
-5.53847280e-06, -1.10740797e-05, 2.17458024e-05,
3.97590833e-05, -5.14108669e-05, 7.92870807e-06,
-3.53963791e-06, -1.63784490e-05, 3.33458671e-06,
-5.60031564e-06, -2.44193897e-05, 5.60340845e-05,
1.90982407e-07, -2.37123843e-05, -1.02673299e-05,
4.70710752e-06, 8.86508330e-05, 4.02039134e-05,
2.39025489e-05, -5.94660474e-06, -1.92301274e-05],
[ 2.49700413e-05, 3.70572613e-05, -2.83669072e-06,
9.72193169e-06, 4.20238479e-06, -1.24009254e-05,
4.04704969e-05, -1.03044572e-05, 3.79528145e-05,
5.41505031e-06, -1.04001201e-05, 1.60908360e-05,
2.25653918e-05, -5.66188177e-06, 4.60520336e-05,
1.02992114e-05, -1.99844011e-05, 1.64715372e-05,
3.76160380e-05, 4.02039134e-05, 8.23927778e-05,
3.21948208e-05, 3.50482953e-05, -1.97971783e-07],
[ 6.74503236e-05, 3.08215527e-05, 2.95395217e-05,
4.42756733e-05, 2.07371361e-05, 6.11287657e-05,
4.05201790e-05, 1.59107041e-05, 3.38036564e-05,
1.21874870e-05, 1.79450881e-05, 4.09067616e-05,
6.22679966e-05, 2.35884764e-05, 4.29696767e-05,
2.81341342e-05, -2.02943319e-07, 2.50897861e-05,
2.30014580e-05, 2.39025489e-05, 3.21948208e-05,
1.20680211e-04, 5.96710279e-05, 3.14411495e-05],
[ 7.08948785e-05, 2.57244480e-05, 4.53889239e-05,
4.21469376e-05, 2.27640364e-05, 1.12849096e-04,
4.01384618e-05, 7.65457600e-05, 3.47015652e-05,
2.04099948e-05, 5.33951974e-05, 5.72051871e-05,
3.92394407e-05, 5.15584350e-05, 5.33004299e-05,
2.81970597e-05, -4.12746174e-07, 5.81836032e-05,
3.12554634e-05, -5.94660474e-06, 3.50482953e-05,
5.96710279e-05, 1.65974972e-04, 5.42453625e-05],
[ 5.55096135e-05, 8.72002781e-06, 3.11615533e-05,
4.35344231e-05, 7.39017715e-05, 1.17234141e-04,
4.82818631e-06, 1.31611693e-04, 3.86545249e-05,
2.89151466e-05, 1.07102393e-04, 6.05239478e-05,
6.47067147e-05, 9.98376706e-05, -1.56063893e-05,
3.94413355e-05, 6.80701129e-05, 1.00294572e-04,
5.94948441e-06, -1.92301274e-05, -1.97971783e-07,
3.14411495e-05, 5.42453625e-05, 1.92061080e-04]]))
sigma_bar = pd.DataFrame(np.array([[ 1.26820873e-09, 9.99357360e-01, 9.96834344e-01,
9.99572167e-01, 9.42281243e-01, 9.97900189e-01,
9.99540337e-01, 9.98601652e-01, 9.99273813e-01,
9.97018616e-01, -9.73801139e-01, 9.99572102e-01,
9.99493833e-01, 9.98516049e-01, 9.99016459e-01,
9.99032878e-01, -9.44718270e-01, 9.99102498e-01,
9.99273586e-01, 9.97964072e-01, 9.98809893e-01,
9.99306302e-01, 9.98670142e-01, 9.89075401e-01],
[ 9.99357360e-01, 8.85756152e-04, 9.96279483e-01,
9.99197947e-01, 9.41407791e-01, 9.97899466e-01,
9.99417552e-01, 9.98836192e-01, 9.99109838e-01,
9.97212584e-01, -9.74070678e-01, 9.99506469e-01,
9.99304970e-01, 9.98532839e-01, 9.99435799e-01,
9.99191149e-01, -9.45834544e-01, 9.99087732e-01,
9.99237110e-01, 9.98261304e-01, 9.98920402e-01,
9.99254574e-01, 9.98828941e-01, 9.88935866e-01],
[ 9.96834344e-01, 9.96279483e-01, 4.28636430e-07,
9.96600181e-01, 9.47870932e-01, 9.95399154e-01,
9.96381458e-01, 9.96592822e-01, 9.96914826e-01,
9.93862938e-01, -9.68327765e-01, 9.96760950e-01,
9.96379221e-01, 9.96672585e-01, 9.96674621e-01,
9.95069722e-01, -9.40676110e-01, 9.96340223e-01,
9.96354345e-01, 9.94003174e-01, 9.96112855e-01,
9.96745892e-01, 9.95997518e-01, 9.89905446e-01],
[ 9.99572167e-01, 9.99197947e-01, 9.96600181e-01,
1.21966822e-09, 9.43053169e-01, 9.97880532e-01,
9.99500369e-01, 9.98410401e-01, 9.99173244e-01,
9.96775797e-01, -9.74367127e-01, 9.99362203e-01,
9.99367414e-01, 9.98297331e-01, 9.98880383e-01,
9.98663753e-01, -9.44650667e-01, 9.98960337e-01,
9.99311633e-01, 9.97876202e-01, 9.98462419e-01,
9.99263944e-01, 9.98810150e-01, 9.89492504e-01],
[ 9.42281243e-01, 9.41407791e-01, 9.47870932e-01,
9.43053169e-01, 5.87433525e-09, 9.47101615e-01,
9.43035090e-01, 9.51819965e-01, 9.46546373e-01,
9.35729856e-01, -8.83958006e-01, 9.43231419e-01,
9.39614529e-01, 9.50658526e-01, 9.44337124e-01,
9.41768971e-01, -8.57348147e-01, 9.46361648e-01,
9.43788045e-01, 9.35138095e-01, 9.43924294e-01,
9.46696871e-01, 9.46932752e-01, 9.74895278e-01],
[ 9.97900189e-01, 9.97899466e-01, 9.95399154e-01,
9.97880532e-01, 9.47101615e-01, 8.52516849e-08,
9.98162029e-01, 9.97693085e-01, 9.97494890e-01,
9.94870487e-01, -9.71281207e-01, 9.97825507e-01,
9.97609029e-01, 9.97078699e-01, 9.97937996e-01,
9.97886353e-01, -9.39807793e-01, 9.97323488e-01,
9.98086971e-01, 9.96890683e-01, 9.97306126e-01,
9.98328683e-01, 9.98019225e-01, 9.90401184e-01],
[ 9.99540337e-01, 9.99417552e-01, 9.96381458e-01,
9.99500369e-01, 9.43035090e-01, 9.98162029e-01,
1.97026096e-09, 9.98697236e-01, 9.99167466e-01,
9.96534267e-01, -9.74797106e-01, 9.99405117e-01,
9.99312797e-01, 9.98338095e-01, 9.99222108e-01,
9.99012700e-01, -9.45433385e-01, 9.99002384e-01,
9.99391574e-01, 9.98227196e-01, 9.98655866e-01,
9.99450287e-01, 9.98871183e-01, 9.89348747e-01],
[ 9.98601652e-01, 9.98836192e-01, 9.96592822e-01,
9.98410401e-01, 9.51819965e-01, 9.97693085e-01,
9.98697236e-01, 3.81563526e-10, 9.98687080e-01,
9.97121814e-01, -9.69119242e-01, 9.98787443e-01,
9.98289054e-01, 9.98866436e-01, 9.98833399e-01,
9.98412389e-01, -9.40737501e-01, 9.99081172e-01,
9.98609560e-01, 9.96732620e-01, 9.98290946e-01,
9.98808945e-01, 9.98467379e-01, 9.92893798e-01],
[ 9.99273813e-01, 9.99109838e-01, 9.96914826e-01,
9.99173244e-01, 9.46546373e-01, 9.97494890e-01,
9.99167466e-01, 9.98687080e-01, 1.63952743e-09,
9.97076275e-01, -9.71833154e-01, 9.99346179e-01,
9.99129781e-01, 9.98683650e-01, 9.99143749e-01,
9.98628992e-01, -9.42716179e-01, 9.99193054e-01,
9.99051762e-01, 9.97592295e-01, 9.98279757e-01,
9.99206541e-01, 9.98709951e-01, 9.91294961e-01],
[ 9.97018616e-01, 9.97212584e-01, 9.93862938e-01,
9.96775797e-01, 9.35729856e-01, 9.94870487e-01,
9.96534267e-01, 9.97121814e-01, 9.97076275e-01,
4.14508406e-09, -9.69602110e-01, 9.97209084e-01,
9.97269710e-01, 9.96130871e-01, 9.96598901e-01,
9.96945711e-01, -9.41737281e-01, 9.97547921e-01,
9.96623630e-01, 9.95604803e-01, 9.96053414e-01,
9.96252977e-01, 9.96293594e-01, 9.87014134e-01],
[-9.73801139e-01, -9.74070678e-01, -9.68327765e-01,
-9.74367127e-01, -8.83958006e-01, -9.71281207e-01,
-9.74797106e-01, -9.69119242e-01, -9.71833154e-01,
-9.69602110e-01, 7.91579754e-08, -9.72627284e-01,
-9.74397877e-01, -9.66816215e-01, -9.74055921e-01,
-9.72152413e-01, 9.49931280e-01, -9.70349967e-01,
-9.74543996e-01, -9.77042256e-01, -9.72510826e-01,
-9.72753685e-01, -9.70977647e-01, -9.49332577e-01],
[ 9.99572102e-01, 9.99506469e-01, 9.96760950e-01,
9.99362203e-01, 9.43231419e-01, 9.97825507e-01,
9.99405117e-01, 9.98787443e-01, 9.99346179e-01,
9.97209084e-01, -9.72627284e-01, 9.49759898e-10,
9.99396168e-01, 9.98616747e-01, 9.99231469e-01,
9.99092707e-01, -9.43690714e-01, 9.99236560e-01,
9.99125265e-01, 9.97720253e-01, 9.98657360e-01,
9.99322811e-01, 9.98924819e-01, 9.89982094e-01],
[ 9.99493833e-01, 9.99304970e-01, 9.96379221e-01,
9.99367414e-01, 9.39614529e-01, 9.97609029e-01,
9.99312797e-01, 9.98289054e-01, 9.99129781e-01,
9.97269710e-01, -9.74397877e-01, 9.99396168e-01,
6.10314496e-08, 9.98282510e-01, 9.98908676e-01,
9.99022688e-01, -9.44584256e-01, 9.99076968e-01,
9.99242844e-01, 9.98014936e-01, 9.98393894e-01,
9.99093343e-01, 9.98535127e-01, 9.88011557e-01],
[ 9.98516049e-01, 9.98532839e-01, 9.96672585e-01,
9.98297331e-01, 9.50658526e-01, 9.97078699e-01,
9.98338095e-01, 9.98866436e-01, 9.98683650e-01,
9.96130871e-01, -9.66816215e-01, 9.98616747e-01,
9.98282510e-01, 1.39566686e-09, 9.98517176e-01,
9.98377797e-01, -9.41181012e-01, 9.99085880e-01,
9.98417332e-01, 9.95895305e-01, 9.98089538e-01,
9.98786233e-01, 9.98273278e-01, 9.92133050e-01],
[ 9.99016459e-01, 9.99435799e-01, 9.96674621e-01,
9.98880383e-01, 9.44337124e-01, 9.97937996e-01,
9.99222108e-01, 9.98833399e-01, 9.99143749e-01,
9.96598901e-01, -9.74055921e-01, 9.99231469e-01,
9.98908676e-01, 9.98517176e-01, 3.91145914e-09,
9.98762801e-01, -9.44712685e-01, 9.98882295e-01,
9.99167722e-01, 9.98365735e-01, 9.98539526e-01,
9.99349674e-01, 9.98682774e-01, 9.90282263e-01],
[ 9.99032878e-01, 9.99191149e-01, 9.95069722e-01,
9.98663753e-01, 9.41768971e-01, 9.97886353e-01,
9.99012700e-01, 9.98412389e-01, 9.98628992e-01,
9.96945711e-01, -9.72152413e-01, 9.99092707e-01,
9.99022688e-01, 9.98377797e-01, 9.98762801e-01,
6.12438062e-09, -9.43278583e-01, 9.98714615e-01,
9.98765094e-01, 9.97518255e-01, 9.98234814e-01,
9.98833239e-01, 9.98556185e-01, 9.88600916e-01],
[-9.44718270e-01, -9.45834544e-01, -9.40676110e-01,
-9.44650667e-01, -8.57348147e-01, -9.39807793e-01,
-9.45433385e-01, -9.40737501e-01, -9.42716179e-01,
-9.41737281e-01, 9.49931280e-01, -9.43690714e-01,
-9.44584256e-01, -9.41181012e-01, -9.44712685e-01,
-9.43278583e-01, 1.85097555e-08, -9.42274581e-01,
-9.43930225e-01, -9.44416759e-01, -9.44123084e-01,
-9.43611879e-01, -9.43058469e-01, -9.20277725e-01],
[ 9.99102498e-01, 9.99087732e-01, 9.96340223e-01,
9.98960337e-01, 9.46361648e-01, 9.97323488e-01,
9.99002384e-01, 9.99081172e-01, 9.99193054e-01,
9.97547921e-01, -9.70349967e-01, 9.99236560e-01,
9.99076968e-01, 9.99085880e-01, 9.98882295e-01,
9.98714615e-01, -9.42274581e-01, 4.63172335e-10,
9.99055447e-01, 9.96678367e-01, 9.97947641e-01,
9.99023756e-01, 9.98655609e-01, 9.90680163e-01],
[ 9.99273586e-01, 9.99237110e-01, 9.96354345e-01,
9.99311633e-01, 9.43788045e-01, 9.98086971e-01,
9.99391574e-01, 9.98609560e-01, 9.99051762e-01,
9.96623630e-01, -9.74543996e-01, 9.99125265e-01,
9.99242844e-01, 9.98417332e-01, 9.99167722e-01,
9.98765094e-01, -9.43930225e-01, 9.99055447e-01,
1.49968419e-08, 9.98019738e-01, 9.98407433e-01,
9.99222857e-01, 9.98652394e-01, 9.89226980e-01],
[ 9.97964072e-01, 9.98261304e-01, 9.94003174e-01,
9.97876202e-01, 9.35138095e-01, 9.96890683e-01,
9.98227196e-01, 9.96732620e-01, 9.97592295e-01,
9.95604803e-01, -9.77042256e-01, 9.97720253e-01,
9.98014936e-01, 9.95895305e-01, 9.98365735e-01,
9.97518255e-01, -9.44416759e-01, 9.96678367e-01,
9.98019738e-01, 7.47650147e-10, 9.97617693e-01,
9.97870740e-01, 9.97367170e-01, 9.86577350e-01],
[ 9.98809893e-01, 9.98920402e-01, 9.96112855e-01,
9.98462419e-01, 9.43924294e-01, 9.97306126e-01,
9.98655866e-01, 9.98290946e-01, 9.98279757e-01,
9.96053414e-01, -9.72510826e-01, 9.98657360e-01,
9.98393894e-01, 9.98089538e-01, 9.98539526e-01,
9.98234814e-01, -9.44123084e-01, 9.97947641e-01,
9.98407433e-01, 9.97617693e-01, 9.94482237e-10,
9.98596112e-01, 9.98000736e-01, 9.89489744e-01],
[ 9.99306302e-01, 9.99254574e-01, 9.96745892e-01,
9.99263944e-01, 9.46696871e-01, 9.98328683e-01,
9.99450287e-01, 9.98808945e-01, 9.99206541e-01,
9.96252977e-01, -9.72753685e-01, 9.99322811e-01,
9.99093343e-01, 9.98786233e-01, 9.99349674e-01,
9.98833239e-01, -9.43611879e-01, 9.99023756e-01,
9.99222857e-01, 9.97870740e-01, 9.98596112e-01,
7.44695142e-09, 9.99016834e-01, 9.90734007e-01],
[ 9.98670142e-01, 9.98828941e-01, 9.95997518e-01,
9.98810150e-01, 9.46932752e-01, 9.98019225e-01,
9.98871183e-01, 9.98467379e-01, 9.98709951e-01,
9.96293594e-01, -9.70977647e-01, 9.98924819e-01,
9.98535127e-01, 9.98273278e-01, 9.98682774e-01,
9.98556185e-01, -9.43058469e-01, 9.98655609e-01,
9.98652394e-01, 9.97367170e-01, 9.98000736e-01,
9.99016834e-01, 1.79765233e-09, 9.91199840e-01],
[ 9.89075401e-01, 9.88935866e-01, 9.89905446e-01,
9.89492504e-01, 9.74895278e-01, 9.90401184e-01,
9.89348747e-01, 9.92893798e-01, 9.91294961e-01,
9.87014134e-01, -9.49332577e-01, 9.89982094e-01,
9.88011557e-01, 9.92133050e-01, 9.90282263e-01,
9.88600916e-01, -9.20277725e-01, 9.90680163e-01,
9.89226980e-01, 9.86577350e-01, 9.89489744e-01,
9.90734007e-01, 9.91199840e-01, 9.41720628e-08]]))
Algorithm Section
N = 24
model = GEKKO(remote=False)
wesg = model.Array(model.Var , N , value = 1/N , lb=0 , ub=1)
model.Equation = (sum(wesg) == 1)
def obj(rf = 0.02):
esg_portfo = 0
for i in range(N):
esg_portfo += model.Intermediate(wesg[i] * ((esg_scores_df["Score 2019"][i] + esg_scores_df["Score 2020"][i])/2))
return_portfo = 0
for i in range(N):
return_portfo += model.Intermediate(wesg[i]*((predicted_return_dataframe.iloc[0 , i] + r.iloc[0 ,i])/2))
sigma_portfo = 0
for i in range(N):
for j in range(N):
sigma_portfo += model.Intermediate(wesg[i] * ((sigma.iloc[i,j] + sigma_bar.iloc[i,j])/2) * wesg[j])
return -esg_portfo * ((return_portfo - rf)/sigma_portfo)
model.Minimize(obj())
model.solve(disp=False)
wesg = np.array( [item[0] for item in wesg], dtype=float)
print(wesg)
Result
The code results in:
[0.00000000e+00 1.31172076e-07 0.00000000e+00 0.00000000e+00
1.88847768e-01 1.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00
1.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00
0.00000000e+00 0.00000000e+00 1.00000000e+00 1.00000000e+00
9.07168980e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
But, there are many ones in the result!! I have a constraint of sum(wesg) == 1! But the result indicates that somehow, Gekko doesn't consider the constraint. If you try print(wesg.sum()), then it will result:
7.096016879722077
Which I expected to be exact 1!
How can I fix this issue, and where is the problem? Any help would be appreciated.
The error is with model.Equation = (sum(wesg) == 1) that redefines the model.Equation function as False. Use this instead:
model.Equation(sum(wesg) == 1)
The new result enforces the constraint to add to one with sum(wesg)=1.000000000001.
[0. 0. 0. 0. 0.1425506 0.24959448
0. 0. 0. 0. 0. 0.0281217
0.32053308 0. 0. 0. 0. 0.
0.05860444 0.18554967 0.01504603 0. 0. 0. ]
Here are a couple additional suggestions and options:
For larger portfolios use model.sum() instead for better performance.
To select the top 3 stocks as integers (purchase or don't purchase) then use integer=True with the APOPT solver.
wesg = model.Array(model.Var, N, value = 1/N, lb=0, ub=1, integer=True)
model.Equation(model.sum(wesg) == 3)
model.options.SOLVER=1
Here is the complete code.
from gekko import GEKKO
import numpy as np
import pandas as pd
esg_scores_df = pd.DataFrame({'Score 2019': {'MSFT': 7,'PEP': 7,'TSLA': 4,'AMZN': 6,'LKQ': 3,'ABMD': 3,'MSI': 10,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 4,'GS': 6,'EQIX': 9,'EA': 7,'AAP': 6,'TEL': 9,'DG': 6,'EXR': 5,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 9},
'Score 2020': {'MSFT': 6,'PEP': 11,'TSLA': 4,'AMZN': 6,'LKQ': 4,'ABMD': 4,'MSI': 8,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 3,'GS': 6,'EQIX': 9,'EA': 6,'AAP': 7,'TEL': 9,'DG': 6,'EXR': 4,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 7}})
predicted_return_dataframe = pd.DataFrame({'MSFT': 1.0982472257593677e-15,'PEP': 4.567069595849647e-09,'TSLA': 7.439258841202596e-10,'AMZN': 3.176883309676764e-07,'LKQ': 4.825709334830293e-05,'ABMD': 2.0608058642685837e-05,'MSI': -3.1789250961959136e-12,'PH': -2.257237871892785e-07,'NKE': -4.737530217609426e-07,'TM': 9.951932427172896e-07,'EOG': 1.184074639824261e-08,'GOOGL': -1.7027206923083418e-10,'NFLX': 2.948344729885545e-05,'GS': 1.458862302453713e-07,'EQIX': 4.620207503091301e-07,'EA': -1.297137640100873e-06,'AAP': -3.493153382990658e-07,'TEL': 8.202463568291799e-08,'DG': 2.743802312024829e-05,'EXR': 1.971994818465128e-11,'MDLZ': 1.578772827915881e-08,'FIS': 1.3314663987166631e-08,'CRL': 4.112083587242872e-05,'RCL': -1.470829206425942e-09} , index = [0])
r = pd.DataFrame({'MSFT': -0.029838842874655123,'PEP': -0.012265555866403165,'TSLA': -0.053599428210690636,'AMZN': -0.02614876342440773,'LKQ': -0.026658034482507052,'ABMD': -0.017274321994365294,'MSI': -0.018624198080929168,'PH': -0.02871952268633362,'NKE': -0.028820168231794074,'TM': -0.011208781085200492,'EOG': -0.04606883798249617,'GOOGL': -0.027983366801701926,'NFLX': -0.0070609860042608165,'GS': -0.02429261616692462,'EQIX': -0.015181566244607758,'EA': -0.012009295653284258,'AAP': -0.011979871239295382,'TEL': -0.021782414925879602,'DG': -0.009820085124020328,'EXR': -0.012233190959318829,'MDLZ': -0.015917631061436933,'FIS': -0.023877815976094417,'CRL': -0.020169902356185498,'RCL': -0.06831607178882856} , index = [0])
sigma = pd.DataFrame(np.array([[ 1.02188994e-04, 3.16399586e-05, 8.54243667e-05,
7.14148287e-05, 3.55313650e-05, 5.44796564e-05,
3.39128448e-05, 6.65003271e-05, 5.42932808e-05,
2.29333906e-05, 4.26407605e-05, 7.92791756e-05,
9.28910574e-05, 6.36770764e-05, 3.75024646e-05,
5.13720672e-05, 1.33646720e-05, 6.21865710e-05,
4.72888751e-05, 2.35546749e-06, 2.49700413e-05,
6.74503236e-05, 7.08948785e-05, 5.55096135e-05],
[ 3.16399586e-05, 6.47824823e-05, -2.57461009e-05,
1.81931187e-05, 2.11795049e-05, 1.01708683e-05,
3.33739243e-05, 1.23893822e-05, 2.74825418e-05,
1.35954411e-05, 2.37108310e-06, 2.34208728e-05,
1.96972793e-05, 1.09335123e-05, 4.26042775e-05,
2.76849308e-05, 6.01821651e-06, 8.04387109e-06,
3.10892437e-05, 2.79970364e-05, 3.70572613e-05,
3.08215527e-05, 2.57244480e-05, 8.72002781e-06],
[ 8.54243667e-05, -2.57461009e-05, 8.84138653e-04,
6.16478906e-05, -1.78063009e-05, 3.62712388e-05,
-6.15200573e-05, 8.63597848e-05, 2.12571109e-05,
3.21749681e-05, 6.54693912e-05, 7.95298035e-05,
1.53917874e-04, 8.06306277e-05, -5.27893372e-06,
2.60899474e-05, 4.29017379e-05, 6.13689353e-05,
2.30097086e-06, -4.45963115e-05, -2.83669072e-06,
2.95395217e-05, 4.53889239e-05, 3.11615533e-05],
[ 7.14148287e-05, 1.81931187e-05, 6.16478906e-05,
1.19357840e-04, 5.48763301e-05, 5.55161292e-05,
2.77288469e-05, 6.25231790e-05, 5.19494452e-05,
1.93193913e-05, 4.28946368e-05, 7.00210371e-05,
9.92756795e-05, 6.54630027e-05, 1.73935405e-05,
4.46819063e-05, 2.94199398e-05, 5.76749375e-05,
3.75634244e-05, -5.53847280e-06, 9.72193169e-06,
4.42756733e-05, 4.21469376e-05, 4.35344231e-05],
[ 3.55313650e-05, 2.11795049e-05, -1.78063009e-05,
5.48763301e-05, 3.49607174e-04, 1.58156275e-04,
-1.64565378e-05, 5.95602984e-05, 3.36138721e-05,
2.25095973e-05, 4.90144491e-05, 5.60207136e-05,
3.73119885e-05, 8.65010497e-05, -1.68774498e-05,
1.06928038e-05, 3.53732120e-05, 8.46071106e-05,
3.89200328e-05, -1.10740797e-05, 4.20238479e-06,
2.07371361e-05, 2.27640364e-05, 7.39017715e-05],
[ 5.44796564e-05, 1.01708683e-05, 3.62712388e-05,
5.55161292e-05, 1.58156275e-04, 1.12266763e-03,
2.49786917e-05, 5.60053839e-05, -2.57686022e-05,
3.00855419e-05, 2.09079416e-04, 1.21797841e-04,
3.80689080e-06, 1.17305259e-04, -4.95192897e-05,
5.56059321e-05, 2.87765760e-06, 1.13640096e-04,
-2.73690072e-05, 2.17458024e-05, -1.24009254e-05,
6.11287657e-05, 1.12849096e-04, 1.17234141e-04],
[ 3.39128448e-05, 3.33739243e-05, -6.15200573e-05,
2.77288469e-05, -1.64565378e-05, 2.49786917e-05,
1.84271573e-04, -2.99455473e-06, 3.56885701e-05,
1.53451236e-05, 3.20222217e-06, 3.00619247e-05,
5.17902613e-05, 1.69851832e-05, 7.75007583e-05,
7.49839347e-06, -3.78485373e-05, -1.28942631e-05,
4.36657573e-05, 3.97590833e-05, 4.04704969e-05,
4.05201790e-05, 4.01384618e-05, 4.82818631e-06],
[ 6.65003271e-05, 1.23893822e-05, 8.63597848e-05,
6.25231790e-05, 5.95602984e-05, 5.60053839e-05,
-2.99455473e-06, 2.86568704e-04, 6.55278686e-05,
4.08938883e-05, 1.18822482e-04, 9.32577766e-05,
7.92498191e-05, 1.38967662e-04, -1.64128182e-05,
4.16393158e-05, 1.06300676e-04, 1.31672888e-04,
6.64839109e-05, -5.14108669e-05, -1.03044572e-05,
1.59107041e-05, 7.65457600e-05, 1.31611693e-04],
[ 5.42932808e-05, 2.74825418e-05, 2.12571109e-05,
5.19494452e-05, 3.36138721e-05, -2.57686022e-05,
3.56885701e-05, 6.55278686e-05, 1.36374334e-04,
1.36781102e-05, 2.38544176e-05, 5.90104054e-05,
6.20754315e-05, 5.84082445e-05, 3.96422393e-05,
2.96064418e-05, 5.54456912e-05, 5.69396332e-05,
7.28867485e-05, 7.92870807e-06, 3.79528145e-05,
3.38036564e-05, 3.47015652e-05, 3.86545249e-05],
[ 2.29333906e-05, 1.35954411e-05, 3.21749681e-05,
1.93193913e-05, 2.25095973e-05, 3.00855419e-05,
1.53451236e-05, 4.08938883e-05, 1.36781102e-05,
5.30619573e-05, 4.42693706e-05, 2.92937393e-05,
1.15184525e-05, 3.04141154e-05, 1.11981894e-06,
1.29388215e-05, 1.41913521e-05, 2.37704063e-05,
1.27983719e-05, -3.53963791e-06, 5.41505031e-06,
1.21874870e-05, 2.04099948e-05, 2.89151466e-05],
[ 4.26407605e-05, 2.37108310e-06, 6.54693912e-05,
4.28946368e-05, 4.90144491e-05, 2.09079416e-04,
3.20222217e-06, 1.18822482e-04, 2.38544176e-05,
4.42693706e-05, 5.01946804e-04, 7.79572195e-05,
7.61563242e-05, 1.31597522e-04, -2.43789107e-05,
6.77962026e-05, 5.39745318e-05, 9.05223133e-05,
-1.88433584e-06, -1.63784490e-05, -1.04001201e-05,
1.79450881e-05, 5.33951974e-05, 1.07102393e-04],
[ 7.92791756e-05, 2.34208728e-05, 7.95298035e-05,
7.00210371e-05, 5.60207136e-05, 1.21797841e-04,
3.00619247e-05, 9.32577766e-05, 5.90104054e-05,
2.92937393e-05, 7.79572195e-05, 1.10688236e-04,
8.13294795e-05, 8.72981821e-05, 2.13737337e-05,
4.33439061e-05, 3.06090710e-05, 7.60853329e-05,
4.44363057e-05, 3.33458671e-06, 1.60908360e-05,
4.09067616e-05, 5.72051871e-05, 6.05239478e-05],
[ 9.28910574e-05, 1.96972793e-05, 1.53917874e-04,
9.92756795e-05, 3.73119885e-05, 3.80689080e-06,
5.17902613e-05, 7.92498191e-05, 6.20754315e-05,
1.15184525e-05, 7.61563242e-05, 8.13294795e-05,
3.77348891e-04, 8.99709029e-05, 2.58732673e-05,
8.24015922e-05, 2.77333081e-05, 4.74146019e-05,
3.76467363e-05, -5.60031564e-06, 2.25653918e-05,
6.22679966e-05, 3.92394407e-05, 6.47067147e-05],
[ 6.36770764e-05, 1.09335123e-05, 8.06306277e-05,
6.54630027e-05, 8.65010497e-05, 1.17305259e-04,
1.69851832e-05, 1.38967662e-04, 5.84082445e-05,
3.04141154e-05, 1.31597522e-04, 8.72981821e-05,
8.99709029e-05, 1.54802097e-04, -1.57072336e-05,
3.58129141e-05, 5.52667399e-05, 1.02065825e-04,
4.96942712e-05, -2.44193897e-05, -5.66188177e-06,
2.35884764e-05, 5.15584350e-05, 9.98376706e-05],
[ 3.75024646e-05, 4.26042775e-05, -5.27893372e-06,
1.73935405e-05, -1.68774498e-05, -4.95192897e-05,
7.75007583e-05, -1.64128182e-05, 3.96422393e-05,
1.11981894e-06, -2.43789107e-05, 2.13737337e-05,
2.58732673e-05, -1.57072336e-05, 1.54605175e-04,
1.07085192e-05, -1.14274871e-05, -1.26204468e-05,
2.50810750e-05, 5.60340845e-05, 4.60520336e-05,
4.29696767e-05, 5.33004299e-05, -1.56063893e-05],
[ 5.13720672e-05, 2.76849308e-05, 2.60899474e-05,
4.46819063e-05, 1.06928038e-05, 5.56059321e-05,
7.49839347e-06, 4.16393158e-05, 2.96064418e-05,
1.29388215e-05, 6.77962026e-05, 4.33439061e-05,
8.24015922e-05, 3.58129141e-05, 1.07085192e-05,
1.32544808e-04, 3.15561503e-05, 3.62681876e-05,
1.76503394e-05, 1.90982407e-07, 1.02992114e-05,
2.81341342e-05, 2.81970597e-05, 3.94413355e-05],
[ 1.33646720e-05, 6.01821651e-06, 4.29017379e-05,
2.94199398e-05, 3.53732120e-05, 2.87765760e-06,
-3.78485373e-05, 1.06300676e-04, 5.54456912e-05,
1.41913521e-05, 5.39745318e-05, 3.06090710e-05,
2.77333081e-05, 5.52667399e-05, -1.14274871e-05,
3.15561503e-05, 2.71861487e-04, 6.04272650e-05,
3.55256428e-05, -2.37123843e-05, -1.99844011e-05,
-2.02943319e-07, -4.12746174e-07, 6.80701129e-05],
[ 6.21865710e-05, 8.04387109e-06, 6.13689353e-05,
5.76749375e-05, 8.46071106e-05, 1.13640096e-04,
-1.28942631e-05, 1.31672888e-04, 5.69396332e-05,
2.37704063e-05, 9.05223133e-05, 7.60853329e-05,
4.74146019e-05, 1.02065825e-04, -1.26204468e-05,
3.62681876e-05, 6.04272650e-05, 1.67171047e-04,
5.06392219e-05, -1.02673299e-05, 1.64715372e-05,
2.50897861e-05, 5.81836032e-05, 1.00294572e-04],
[ 4.72888751e-05, 3.10892437e-05, 2.30097086e-06,
3.75634244e-05, 3.89200328e-05, -2.73690072e-05,
4.36657573e-05, 6.64839109e-05, 7.28867485e-05,
1.27983719e-05, -1.88433584e-06, 4.44363057e-05,
3.76467363e-05, 4.96942712e-05, 2.50810750e-05,
1.76503394e-05, 3.55256428e-05, 5.06392219e-05,
1.99771178e-04, 4.70710752e-06, 3.76160380e-05,
2.30014580e-05, 3.12554634e-05, 5.94948441e-06],
[ 2.35546749e-06, 2.79970364e-05, -4.45963115e-05,
-5.53847280e-06, -1.10740797e-05, 2.17458024e-05,
3.97590833e-05, -5.14108669e-05, 7.92870807e-06,
-3.53963791e-06, -1.63784490e-05, 3.33458671e-06,
-5.60031564e-06, -2.44193897e-05, 5.60340845e-05,
1.90982407e-07, -2.37123843e-05, -1.02673299e-05,
4.70710752e-06, 8.86508330e-05, 4.02039134e-05,
2.39025489e-05, -5.94660474e-06, -1.92301274e-05],
[ 2.49700413e-05, 3.70572613e-05, -2.83669072e-06,
9.72193169e-06, 4.20238479e-06, -1.24009254e-05,
4.04704969e-05, -1.03044572e-05, 3.79528145e-05,
5.41505031e-06, -1.04001201e-05, 1.60908360e-05,
2.25653918e-05, -5.66188177e-06, 4.60520336e-05,
1.02992114e-05, -1.99844011e-05, 1.64715372e-05,
3.76160380e-05, 4.02039134e-05, 8.23927778e-05,
3.21948208e-05, 3.50482953e-05, -1.97971783e-07],
[ 6.74503236e-05, 3.08215527e-05, 2.95395217e-05,
4.42756733e-05, 2.07371361e-05, 6.11287657e-05,
4.05201790e-05, 1.59107041e-05, 3.38036564e-05,
1.21874870e-05, 1.79450881e-05, 4.09067616e-05,
6.22679966e-05, 2.35884764e-05, 4.29696767e-05,
2.81341342e-05, -2.02943319e-07, 2.50897861e-05,
2.30014580e-05, 2.39025489e-05, 3.21948208e-05,
1.20680211e-04, 5.96710279e-05, 3.14411495e-05],
[ 7.08948785e-05, 2.57244480e-05, 4.53889239e-05,
4.21469376e-05, 2.27640364e-05, 1.12849096e-04,
4.01384618e-05, 7.65457600e-05, 3.47015652e-05,
2.04099948e-05, 5.33951974e-05, 5.72051871e-05,
3.92394407e-05, 5.15584350e-05, 5.33004299e-05,
2.81970597e-05, -4.12746174e-07, 5.81836032e-05,
3.12554634e-05, -5.94660474e-06, 3.50482953e-05,
5.96710279e-05, 1.65974972e-04, 5.42453625e-05],
[ 5.55096135e-05, 8.72002781e-06, 3.11615533e-05,
4.35344231e-05, 7.39017715e-05, 1.17234141e-04,
4.82818631e-06, 1.31611693e-04, 3.86545249e-05,
2.89151466e-05, 1.07102393e-04, 6.05239478e-05,
6.47067147e-05, 9.98376706e-05, -1.56063893e-05,
3.94413355e-05, 6.80701129e-05, 1.00294572e-04,
5.94948441e-06, -1.92301274e-05, -1.97971783e-07,
3.14411495e-05, 5.42453625e-05, 1.92061080e-04]]))
sigma_bar = pd.DataFrame(np.array([[ 1.26820873e-09, 9.99357360e-01, 9.96834344e-01,
9.99572167e-01, 9.42281243e-01, 9.97900189e-01,
9.99540337e-01, 9.98601652e-01, 9.99273813e-01,
9.97018616e-01, -9.73801139e-01, 9.99572102e-01,
9.99493833e-01, 9.98516049e-01, 9.99016459e-01,
9.99032878e-01, -9.44718270e-01, 9.99102498e-01,
9.99273586e-01, 9.97964072e-01, 9.98809893e-01,
9.99306302e-01, 9.98670142e-01, 9.89075401e-01],
[ 9.99357360e-01, 8.85756152e-04, 9.96279483e-01,
9.99197947e-01, 9.41407791e-01, 9.97899466e-01,
9.99417552e-01, 9.98836192e-01, 9.99109838e-01,
9.97212584e-01, -9.74070678e-01, 9.99506469e-01,
9.99304970e-01, 9.98532839e-01, 9.99435799e-01,
9.99191149e-01, -9.45834544e-01, 9.99087732e-01,
9.99237110e-01, 9.98261304e-01, 9.98920402e-01,
9.99254574e-01, 9.98828941e-01, 9.88935866e-01],
[ 9.96834344e-01, 9.96279483e-01, 4.28636430e-07,
9.96600181e-01, 9.47870932e-01, 9.95399154e-01,
9.96381458e-01, 9.96592822e-01, 9.96914826e-01,
9.93862938e-01, -9.68327765e-01, 9.96760950e-01,
9.96379221e-01, 9.96672585e-01, 9.96674621e-01,
9.95069722e-01, -9.40676110e-01, 9.96340223e-01,
9.96354345e-01, 9.94003174e-01, 9.96112855e-01,
9.96745892e-01, 9.95997518e-01, 9.89905446e-01],
[ 9.99572167e-01, 9.99197947e-01, 9.96600181e-01,
1.21966822e-09, 9.43053169e-01, 9.97880532e-01,
9.99500369e-01, 9.98410401e-01, 9.99173244e-01,
9.96775797e-01, -9.74367127e-01, 9.99362203e-01,
9.99367414e-01, 9.98297331e-01, 9.98880383e-01,
9.98663753e-01, -9.44650667e-01, 9.98960337e-01,
9.99311633e-01, 9.97876202e-01, 9.98462419e-01,
9.99263944e-01, 9.98810150e-01, 9.89492504e-01],
[ 9.42281243e-01, 9.41407791e-01, 9.47870932e-01,
9.43053169e-01, 5.87433525e-09, 9.47101615e-01,
9.43035090e-01, 9.51819965e-01, 9.46546373e-01,
9.35729856e-01, -8.83958006e-01, 9.43231419e-01,
9.39614529e-01, 9.50658526e-01, 9.44337124e-01,
9.41768971e-01, -8.57348147e-01, 9.46361648e-01,
9.43788045e-01, 9.35138095e-01, 9.43924294e-01,
9.46696871e-01, 9.46932752e-01, 9.74895278e-01],
[ 9.97900189e-01, 9.97899466e-01, 9.95399154e-01,
9.97880532e-01, 9.47101615e-01, 8.52516849e-08,
9.98162029e-01, 9.97693085e-01, 9.97494890e-01,
9.94870487e-01, -9.71281207e-01, 9.97825507e-01,
9.97609029e-01, 9.97078699e-01, 9.97937996e-01,
9.97886353e-01, -9.39807793e-01, 9.97323488e-01,
9.98086971e-01, 9.96890683e-01, 9.97306126e-01,
9.98328683e-01, 9.98019225e-01, 9.90401184e-01],
[ 9.99540337e-01, 9.99417552e-01, 9.96381458e-01,
9.99500369e-01, 9.43035090e-01, 9.98162029e-01,
1.97026096e-09, 9.98697236e-01, 9.99167466e-01,
9.96534267e-01, -9.74797106e-01, 9.99405117e-01,
9.99312797e-01, 9.98338095e-01, 9.99222108e-01,
9.99012700e-01, -9.45433385e-01, 9.99002384e-01,
9.99391574e-01, 9.98227196e-01, 9.98655866e-01,
9.99450287e-01, 9.98871183e-01, 9.89348747e-01],
[ 9.98601652e-01, 9.98836192e-01, 9.96592822e-01,
9.98410401e-01, 9.51819965e-01, 9.97693085e-01,
9.98697236e-01, 3.81563526e-10, 9.98687080e-01,
9.97121814e-01, -9.69119242e-01, 9.98787443e-01,
9.98289054e-01, 9.98866436e-01, 9.98833399e-01,
9.98412389e-01, -9.40737501e-01, 9.99081172e-01,
9.98609560e-01, 9.96732620e-01, 9.98290946e-01,
9.98808945e-01, 9.98467379e-01, 9.92893798e-01],
[ 9.99273813e-01, 9.99109838e-01, 9.96914826e-01,
9.99173244e-01, 9.46546373e-01, 9.97494890e-01,
9.99167466e-01, 9.98687080e-01, 1.63952743e-09,
9.97076275e-01, -9.71833154e-01, 9.99346179e-01,
9.99129781e-01, 9.98683650e-01, 9.99143749e-01,
9.98628992e-01, -9.42716179e-01, 9.99193054e-01,
9.99051762e-01, 9.97592295e-01, 9.98279757e-01,
9.99206541e-01, 9.98709951e-01, 9.91294961e-01],
[ 9.97018616e-01, 9.97212584e-01, 9.93862938e-01,
9.96775797e-01, 9.35729856e-01, 9.94870487e-01,
9.96534267e-01, 9.97121814e-01, 9.97076275e-01,
4.14508406e-09, -9.69602110e-01, 9.97209084e-01,
9.97269710e-01, 9.96130871e-01, 9.96598901e-01,
9.96945711e-01, -9.41737281e-01, 9.97547921e-01,
9.96623630e-01, 9.95604803e-01, 9.96053414e-01,
9.96252977e-01, 9.96293594e-01, 9.87014134e-01],
[-9.73801139e-01, -9.74070678e-01, -9.68327765e-01,
-9.74367127e-01, -8.83958006e-01, -9.71281207e-01,
-9.74797106e-01, -9.69119242e-01, -9.71833154e-01,
-9.69602110e-01, 7.91579754e-08, -9.72627284e-01,
-9.74397877e-01, -9.66816215e-01, -9.74055921e-01,
-9.72152413e-01, 9.49931280e-01, -9.70349967e-01,
-9.74543996e-01, -9.77042256e-01, -9.72510826e-01,
-9.72753685e-01, -9.70977647e-01, -9.49332577e-01],
[ 9.99572102e-01, 9.99506469e-01, 9.96760950e-01,
9.99362203e-01, 9.43231419e-01, 9.97825507e-01,
9.99405117e-01, 9.98787443e-01, 9.99346179e-01,
9.97209084e-01, -9.72627284e-01, 9.49759898e-10,
9.99396168e-01, 9.98616747e-01, 9.99231469e-01,
9.99092707e-01, -9.43690714e-01, 9.99236560e-01,
9.99125265e-01, 9.97720253e-01, 9.98657360e-01,
9.99322811e-01, 9.98924819e-01, 9.89982094e-01],
[ 9.99493833e-01, 9.99304970e-01, 9.96379221e-01,
9.99367414e-01, 9.39614529e-01, 9.97609029e-01,
9.99312797e-01, 9.98289054e-01, 9.99129781e-01,
9.97269710e-01, -9.74397877e-01, 9.99396168e-01,
6.10314496e-08, 9.98282510e-01, 9.98908676e-01,
9.99022688e-01, -9.44584256e-01, 9.99076968e-01,
9.99242844e-01, 9.98014936e-01, 9.98393894e-01,
9.99093343e-01, 9.98535127e-01, 9.88011557e-01],
[ 9.98516049e-01, 9.98532839e-01, 9.96672585e-01,
9.98297331e-01, 9.50658526e-01, 9.97078699e-01,
9.98338095e-01, 9.98866436e-01, 9.98683650e-01,
9.96130871e-01, -9.66816215e-01, 9.98616747e-01,
9.98282510e-01, 1.39566686e-09, 9.98517176e-01,
9.98377797e-01, -9.41181012e-01, 9.99085880e-01,
9.98417332e-01, 9.95895305e-01, 9.98089538e-01,
9.98786233e-01, 9.98273278e-01, 9.92133050e-01],
[ 9.99016459e-01, 9.99435799e-01, 9.96674621e-01,
9.98880383e-01, 9.44337124e-01, 9.97937996e-01,
9.99222108e-01, 9.98833399e-01, 9.99143749e-01,
9.96598901e-01, -9.74055921e-01, 9.99231469e-01,
9.98908676e-01, 9.98517176e-01, 3.91145914e-09,
9.98762801e-01, -9.44712685e-01, 9.98882295e-01,
9.99167722e-01, 9.98365735e-01, 9.98539526e-01,
9.99349674e-01, 9.98682774e-01, 9.90282263e-01],
[ 9.99032878e-01, 9.99191149e-01, 9.95069722e-01,
9.98663753e-01, 9.41768971e-01, 9.97886353e-01,
9.99012700e-01, 9.98412389e-01, 9.98628992e-01,
9.96945711e-01, -9.72152413e-01, 9.99092707e-01,
9.99022688e-01, 9.98377797e-01, 9.98762801e-01,
6.12438062e-09, -9.43278583e-01, 9.98714615e-01,
9.98765094e-01, 9.97518255e-01, 9.98234814e-01,
9.98833239e-01, 9.98556185e-01, 9.88600916e-01],
[-9.44718270e-01, -9.45834544e-01, -9.40676110e-01,
-9.44650667e-01, -8.57348147e-01, -9.39807793e-01,
-9.45433385e-01, -9.40737501e-01, -9.42716179e-01,
-9.41737281e-01, 9.49931280e-01, -9.43690714e-01,
-9.44584256e-01, -9.41181012e-01, -9.44712685e-01,
-9.43278583e-01, 1.85097555e-08, -9.42274581e-01,
-9.43930225e-01, -9.44416759e-01, -9.44123084e-01,
-9.43611879e-01, -9.43058469e-01, -9.20277725e-01],
[ 9.99102498e-01, 9.99087732e-01, 9.96340223e-01,
9.98960337e-01, 9.46361648e-01, 9.97323488e-01,
9.99002384e-01, 9.99081172e-01, 9.99193054e-01,
9.97547921e-01, -9.70349967e-01, 9.99236560e-01,
9.99076968e-01, 9.99085880e-01, 9.98882295e-01,
9.98714615e-01, -9.42274581e-01, 4.63172335e-10,
9.99055447e-01, 9.96678367e-01, 9.97947641e-01,
9.99023756e-01, 9.98655609e-01, 9.90680163e-01],
[ 9.99273586e-01, 9.99237110e-01, 9.96354345e-01,
9.99311633e-01, 9.43788045e-01, 9.98086971e-01,
9.99391574e-01, 9.98609560e-01, 9.99051762e-01,
9.96623630e-01, -9.74543996e-01, 9.99125265e-01,
9.99242844e-01, 9.98417332e-01, 9.99167722e-01,
9.98765094e-01, -9.43930225e-01, 9.99055447e-01,
1.49968419e-08, 9.98019738e-01, 9.98407433e-01,
9.99222857e-01, 9.98652394e-01, 9.89226980e-01],
[ 9.97964072e-01, 9.98261304e-01, 9.94003174e-01,
9.97876202e-01, 9.35138095e-01, 9.96890683e-01,
9.98227196e-01, 9.96732620e-01, 9.97592295e-01,
9.95604803e-01, -9.77042256e-01, 9.97720253e-01,
9.98014936e-01, 9.95895305e-01, 9.98365735e-01,
9.97518255e-01, -9.44416759e-01, 9.96678367e-01,
9.98019738e-01, 7.47650147e-10, 9.97617693e-01,
9.97870740e-01, 9.97367170e-01, 9.86577350e-01],
[ 9.98809893e-01, 9.98920402e-01, 9.96112855e-01,
9.98462419e-01, 9.43924294e-01, 9.97306126e-01,
9.98655866e-01, 9.98290946e-01, 9.98279757e-01,
9.96053414e-01, -9.72510826e-01, 9.98657360e-01,
9.98393894e-01, 9.98089538e-01, 9.98539526e-01,
9.98234814e-01, -9.44123084e-01, 9.97947641e-01,
9.98407433e-01, 9.97617693e-01, 9.94482237e-10,
9.98596112e-01, 9.98000736e-01, 9.89489744e-01],
[ 9.99306302e-01, 9.99254574e-01, 9.96745892e-01,
9.99263944e-01, 9.46696871e-01, 9.98328683e-01,
9.99450287e-01, 9.98808945e-01, 9.99206541e-01,
9.96252977e-01, -9.72753685e-01, 9.99322811e-01,
9.99093343e-01, 9.98786233e-01, 9.99349674e-01,
9.98833239e-01, -9.43611879e-01, 9.99023756e-01,
9.99222857e-01, 9.97870740e-01, 9.98596112e-01,
7.44695142e-09, 9.99016834e-01, 9.90734007e-01],
[ 9.98670142e-01, 9.98828941e-01, 9.95997518e-01,
9.98810150e-01, 9.46932752e-01, 9.98019225e-01,
9.98871183e-01, 9.98467379e-01, 9.98709951e-01,
9.96293594e-01, -9.70977647e-01, 9.98924819e-01,
9.98535127e-01, 9.98273278e-01, 9.98682774e-01,
9.98556185e-01, -9.43058469e-01, 9.98655609e-01,
9.98652394e-01, 9.97367170e-01, 9.98000736e-01,
9.99016834e-01, 1.79765233e-09, 9.91199840e-01],
[ 9.89075401e-01, 9.88935866e-01, 9.89905446e-01,
9.89492504e-01, 9.74895278e-01, 9.90401184e-01,
9.89348747e-01, 9.92893798e-01, 9.91294961e-01,
9.87014134e-01, -9.49332577e-01, 9.89982094e-01,
9.88011557e-01, 9.92133050e-01, 9.90282263e-01,
9.88600916e-01, -9.20277725e-01, 9.90680163e-01,
9.89226980e-01, 9.86577350e-01, 9.89489744e-01,
9.90734007e-01, 9.91199840e-01, 9.41720628e-08]]))
N = 24
model = GEKKO(remote=False)
wesg = model.Array(model.Var, N, value = 1/N, lb=0, ub=1)
model.Equation(m.sum(wesg) == 1)
def obj(rf = 0.02):
esg_portfo = 0
for i in range(N):
esg_portfo += model.Intermediate(wesg[i] * ((esg_scores_df["Score 2019"][i] + esg_scores_df["Score 2020"][i])/2))
return_portfo = 0
for i in range(N):
return_portfo += model.Intermediate(wesg[i]*((predicted_return_dataframe.iloc[0 , i] + r.iloc[0 ,i])/2))
sigma_portfo = 0
for i in range(N):
for j in range(N):
sigma_portfo += model.Intermediate(wesg[i] * ((sigma.iloc[i,j] + sigma_bar.iloc[i,j])/2) * wesg[j])
return -esg_portfo * ((return_portfo - rf)/sigma_portfo)
model.Minimize(obj())
model.solve(disp=True)
wesg = np.array( [item[0] for item in wesg], dtype=float)
print(wesg)
print(sum(wesg))
Thanks for the well-written question and for sharing the application.

How to Add a list to a dataframe?

I know this question has been asked before, but I've tried to implement all the solutions I've found and still haven't solved the problem.
Here is my code
import csv
import pandas as pd
import helperFunctions import pandas tickers = []
f = open("watchlist.txt", "r") for i in f:
tickers.append((helperFunctions.getTicker(i)))
head = ["Name", "Growth", "Recommendation", "Earnings Growth", "Current Ratio",
"Total Cash", "Debt", " Revenue", "Percentage Shares Out", "Percentage Institutions", "Percentage Insiders",
"Price to Book", "Short Ratio", "Regular Market Price"] df = pd.DataFrame() df = pd.DataFrame(columns=head) try:
for i in tickers:
currentTicker = []
currentTicker.append(i.info['longName'])
currentTicker.append(i.info['revenueGrowth'])
currentTicker.append(i.info['recommendationKey'])
currentTicker.append(i.info['earningsGrowth'])
currentTicker.append(i.info['currentRatio'])
currentTicker.append(i.info['totalCash'])
currentTicker.append(i.info['totalDebt'])
currentTicker.append(i.info['totalRevenue'])
currentTicker.append(i.info['sharesPercentSharesOut'])
currentTicker.append(i.info['heldPercentInstitutions'])
currentTicker.append(i.info['heldPercentInsiders'])
currentTicker.append(i.info['priceToBook'])
currentTicker.append(i.info['shortRatio'])
currentTicker.append(i.info['beta'])
currentTicker.append(i.info['regularMarketPrice'])
print(str(currentTicker + "\n"))
'''
# Why Don't these work??
1.
df.append(currentTicker)
2.
df_length = len(df)
df.loc[df_length] = currentTicker
3.
a_series = pd.Series(currentTicker, index=df.columns)
df = df.append(a_series, ignore_index=True)
'''
except Exception as e:
print(str(i) + repr(e))
print(df)
In the section where you see comments in the list, those are all things things I've attempted to add each iteration to the dataframe. Basically the watchlist is a txtfile with some tickers, and I would like to get that data and put it into a dataframe and I'm running into some trouble. Thanks for your time.
This was how I ended up solving it
import csv
import pandas as pd
import helperFunctions
import pandas
tickers = []
f = open("watchlist.txt", "r")
for i in f:
tickers.append((helperFunctions.getTicker(i)))
head = ["Name", "Growth", "Recommendation", "Earnings Growth", "Current Ratio",
"Total Cash", "Debt", "Revenue", "Percentage Shares Out", "Percentage Institutions", "Percentage Insiders",
"Price to Book", "Short Ratio", "Regular Market Price"]
df = pd.DataFrame(columns=head)
try:
for i in tickers:
currentTicker = []
currentTicker.append(i.info['longName'])
currentTicker.append(i.info['revenueGrowth'])
currentTicker.append(i.info['recommendationKey'])
currentTicker.append(i.info['earningsGrowth'])
currentTicker.append(i.info['currentRatio'])
currentTicker.append(i.info['totalCash'])
currentTicker.append(i.info['totalDebt'])
currentTicker.append(i.info['totalRevenue'])
currentTicker.append(i.info['sharesPercentSharesOut'])
currentTicker.append(i.info['heldPercentInstitutions'])
currentTicker.append(i.info['heldPercentInsiders'])
currentTicker.append(i.info['priceToBook'])
currentTicker.append(i.info['shortRatio'])
currentTicker.append(i.info['beta'])
currentTicker.append(i.info['regularMarketPrice'])
df = df.append({'Name': currentTicker[0],
'Growth': currentTicker[1],
"Recommendation":currentTicker[2],
"Earnings Growth":currentTicker[3],
"Current Ratio": currentTicker[4],
"Total Cash":currentTicker[5],
"Debt": currentTicker[6],
"Revenue": currentTicker[7],
"Percentage Shares Out": currentTicker[8],
"Percentage Institutions": currentTicker[9],
"Percentage Insiders": currentTicker[10],
"Price to Book": currentTicker[11],
"Short Ratio": currentTicker[12],
"Regular Market Price": currentTicker[13],
},
ignore_index=True)
#print(currentTicker)
except Exception as e:
print(str(i) + repr(e))
print(df)
The issue in your code seems to be related to the fact that the currentTicker list has an extra column, namely beta, which is not present in the columns of the df.
Once you either add that column to head or remove it from currentTicker, method 2 and 3 will work.
import csv
import pandas as pd
import helperFunctions
with open("watchlist.txt", "r") as f:
tickers = [helperFunctions.getTicker(i) for i in f]
head = [
"Name",
"Growth",
"Recommendation",
"Earnings Growth",
"Current Ratio",
"Total Cash",
"Debt",
"Revenue",
"Percentage Shares Out",
"Percentage Institutions",
"Percentage Insiders",
"Price to Book",
"Short Ratio",
"Regular Market Price"
]
df = pd.DataFrame(columns=head)
columns_to_extract = [
'longName',
'revenueGrowth',
'recommendationKey',
'earningsGrowth',
'currentRatio',
'totalCash',
'totalDebt',
'totalRevenue',
'sharesPercentSharesOut',
'heldPercentInstitutions',
'heldPercentInsiders',
'priceToBook',
'shortRatio',
# 'beta', # <- The column for this value is missing from `head`
'regularMarketPrice'
]
currentTicker = [i.info[key] for key in columns_to_extract]
# 1. Method - THIS DOES NOT WORK
# df.append(currentTicker)
# 2. Method
df_length = len(df)
df.loc[df_length] = currentTicker
# 3. Method
a_series = pd.Series(currentTicker, index=df.columns)
df = df.append(a_series, ignore_index=True)
print(df)

Print new line each array index in a loop reach certain number

I have this array (as an example of my problem) :
[0.8067, 0.7152, 0.4551, 0.7519, 0.3419, 0.7161, 0.3793, 0.6859, 0.4205, 0.5129, 0.5534, 0.5995, 0.4999, 0.5136, 0.8194, 0.4855, 0.6822, 0.4924, 0.5359, 0.4083, 0.5078, 0.7260, 0.6876, 0.7033, 0.5777, 0.4515, 0.5460,0.5842, 0.7296, 0.7570, 0.4579, 0.3252, 0.4683, 0.3646, 0.3220, 0.4150,0.3263, 0.6402, 0.3184, 0.7087, 0.2958, 0.5384, 0.5462, 0.3933, 0.2963,0.6883, 0.4766, 0.5430, 0.4943, 0.2810, 0.4785, 0.5618, 0.6941, 0.4943,0.3793, 0.7629, 0.6058, 0.6419, 0.4902, 0.3158, 0.7923, 0.7335, 0.5624,0.5390, 0.5337, 0.8333, 0.7519, 0.6591, 0.5301, 0.3020, 0.8187, 0.8084,0.3412, 0.7912, 0.6240, 0.4296, 0.4908, 0.6560, 0.7366, 0.5219, 0.8128,0.3683, 0.6037, 0.4570, 0.3640, 0.4717, 0.5948, 0.6294, 0.8222, 0.7323,0.4344, 0.4371, 0.5013, 0.2913, 0.4335, 0.4046, 0.6788, 0.5917, 0.8369,0.8983, 0.3981, 0.8857, 0.4309, 0.6197, 0.7020, 0.3666, 0.5837, 0.3259,0.7193, 0.3719, 0.7098, 0.4088, 0.5421, 0.5039, 0.3664, 0.5499, 0.8648, 0.3217, 0.7696, 0.5970, 0.4611, 0.3465, 0.6396, 0.6688, 0.5773, 0.7444,0.7232, 0.5695, 0.5801, 0.5218, 0.8099, 0.6983, 0.5733, 0.3286, 0.6736, 0.6470, 0.9196, 0.7589, 0.7610, 0.8454, 0.6261, 0.6229, 0.7600, 0.5022,0.3035, 0.5229, 0.5353, 0.4962, 0.8466, 0.1817, 0.5271, 0.6928, 0.7898,0.4182, 0.5234, 0.4112, 0.4812, 0.7522, 0.4209, 0.7217, 0.6545, 0.6954,0.3139, 0.5253, 0.5467, 0.3606, 0.6640, 0.7399, 0.7965, 0.5742, 0.5729,0.6213, 0.7981, 0.5613, 0.4904, 0.7292, 0.5686, 0.8421, 0.7316, 0.6408,0.6550, 0.3902, 0.5353, 0.5459, 0.4035, 0.3390, 0.4407, 0.7370, 0.4466,0.4029, 0.8216, 0.4862, 0.7136, 0.3544, 0.7967, 0.2909, 0.4384, 0.5505,0.6768, 0.5122, 0.6042, 0.5240, 0.4299, 0.3714, 0.6224, 0.6549, 0.7901,0.7289, 0.7580, 0.5656, 0.7841, 0.7520, 0.8379, 0.4449, 0.4860, 0.6904,0.7279, 0.6378, 0.4493, 0.5407, 0.6737, 0.5260, 0.8009, 0.6307, 0.6026,0.5197, 0.7532, 0.4754, 0.6674, 0.6768, 0.7643, 0.6705, 0.7871, 0.6135, 0.7762, 0.6081, 0.4060, 0.3688, 0.5848, 0.4235, 0.6011, 0.6949, 0.4410,0.8054, 0.7706, 0.3644, 0.6820, 0.6351, 0.4282, 0.4613, 0.7392, 0.5208,0.4409, 0.5589, 0.3401, 0.5811, 0.7109, 0.3245, 0.5017, 0.6650, 0.5949,0.5680, 0.4445, 0.7482, 0.3044, 0.7760, 0.4396, 0.4067, 0.3840, 0.7426,0.5989, 0.5169, 0.7056, 0.4329, 0.5555]
What i want to do is each time the index value of the array reach 9, it will print a new line before it continues printing the next array value.
So what i expect it will print the array like this :
[0.8067, 0.7152, 0.4551, 0.7519, 0.3419, 0.7161, 0.3793, 0.6859, 0.4205,
0.5129, 0.5534, 0.5995, 0.4999, 0.5136, 0.8194, 0.4855, 0.6822, 0.4924,
0.5359, 0.4083, 0.5078, 0.7260, 0.6876, 0.7033, 0.5777, 0.4515, 0.5460,
0.5842, 0.7296, 0.7570, 0.4579, 0.3252, 0.4683, 0.3646, 0.3220, 0.4150,
0.3263, 0.6402, 0.3184, 0.7087, 0.2958, 0.5384, 0.5462, 0.3933, 0.2963,
0.6883, 0.4766, 0.5430, 0.4943, 0.2810, 0.4785, 0.5618, 0.6941, 0.4943,
0.3793, 0.7629, 0.6058, 0.6419, 0.4902, 0.3158, 0.7923, 0.7335, 0.5624,
0.5390, 0.5337, 0.8333, 0.7519, 0.6591, 0.5301, 0.3020, 0.8187, 0.8084,
0.3412, 0.7912, 0.6240, 0.4296, 0.4908, 0.6560, 0.7366, 0.5219, 0.8128,
0.3683, 0.6037, 0.4570, 0.3640, 0.4717, 0.5948, 0.6294, 0.8222, 0.7323,
0.4344, 0.4371, 0.5013, 0.2913, 0.4335, 0.4046, 0.6788, 0.5917, 0.8369,
0.8983, 0.3981, 0.8857, 0.4309, 0.6197, 0.7020, 0.3666, 0.5837, 0.3259,
0.7193, 0.3719, 0.7098, 0.4088, 0.5421, 0.5039, 0.3664, 0.5499, 0.8648,
0.3217, 0.7696, 0.5970, 0.4611, 0.3465, 0.6396, 0.6688, 0.5773, 0.7444,
0.7232, 0.5695, 0.5801, 0.5218, 0.8099, 0.6983, 0.5733, 0.3286, 0.6736,
0.6470, 0.9196, 0.7589, 0.7610, 0.8454, 0.6261, 0.6229, 0.7600, 0.5022,
0.3035, 0.5229, 0.5353, 0.4962, 0.8466, 0.1817, 0.5271, 0.6928, 0.7898,
0.4182, 0.5234, 0.4112, 0.4812, 0.7522, 0.4209, 0.7217, 0.6545, 0.6954,
0.3139, 0.5253, 0.5467, 0.3606, 0.6640, 0.7399, 0.7965, 0.5742, 0.5729,
0.6213, 0.7981, 0.5613, 0.4904, 0.7292, 0.5686, 0.8421, 0.7316, 0.6408,
0.6550, 0.3902, 0.5353, 0.5459, 0.4035, 0.3390, 0.4407, 0.7370, 0.4466,
0.4029, 0.8216, 0.4862, 0.7136 ]
How can i achieve this? Thank you in advance.
Try this
for i in range(len(arr)):
if i%9 == 0:
print()
print(arr[i], end = " ")

How to combine two python matrices numpy

I have this CSV "TEMP2" full of data showed below.
1376460059,4,33.29,33.23,33.23,33.29,33.23,33.29,33.29,33.29,33.33,33.29,33.33,33.29,33.33,33.33,33.37,33.33,33.33,33.33,33.33,33.37,33.37,33.37,33.37
My work so far is this:
import csv
import numpy as np
import datetime
data = np.genfromtxt('TEMP2.csv', delimiter=',')[1:]
limite=data[0]
COLUMN_NUM = int(limite)
data = np.genfromtxt('TEMP2.csv', delimiter=',')[2:]
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
tiempo = np.array(tiempo)
print tiempo
results = []
for i in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
results.append(tiempo)
print np.hstack(results)
if data.shape[0] % 4 == 0:
print data.reshape((-1, 4))
else:
data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')
print (data)
print data.reshape((-1, COLUMN_NUM))
This part of the code will give me the time and miliseconds when each data was generated.
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
tiempo = np.array(tiempo)
print tiempo
My question is, how can I set this results into a matrix looking something like this:
[[1376460059.0 1376460059.25 1376460059.5 1376460059.75]
[1376460060.0 1376460060.25 1376460060.5 1376460060.75]
[1376460061.0 1376460061.25 1376460061.5 1376460061.75]
[. . . . . . . . . . . . . . . . . . . and so on . .]
[1376460063.75 1376460064.0 1376460064.25 1376460064.5]]
Also, once I have it, is it possible to combine it with my other matrix to get a result like this:
[[33.29 33.23 33.23 33.29]
[1376460059.0 1376460059.25 1376460059.5 1376460059.75]
[33.23 33.29 33.29 33.29]
[1376460060.0 1376460060.25 1376460060.5 1376460060.75]
[33.33 33.29 33.33 33.29]
[1376460061.0 1376460061.25 1376460061.5 1376460061.75]
[ and so on. . . . .. . . . . .. . . . . . . .]]
I am really asking this because I have other source of help. But desperately to know. I have searched but Im not getting no where now. Thanks.
for the first part of your question, you just have to create a tab before your first iteration.
res = []
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
tiempo = np.array(tiempo)
print tiempo
res.append(tiempo)
for the second part of your question, to combine the two matrix, you can use numpy.concatenate:
import numpy as np
np.concatenate((A, B))
matrix([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
I have not test my code, so I hope it will help you
I have work a little on the code, and now I have that :
import csv
import numpy as np
import datetime
data = np.genfromtxt('TEMP2.csv', delimiter=',')[1:]
limite=data[0]
COLUMN_NUM = int(limite)
data = np.genfromtxt('TEMP2.csv', delimiter=',')[2:]
results = np.zeros(shape=(len(data)))
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
results[x] = tiempo
if data.shape[0] % 4 == 0:
print(data.reshape((-1, 4)))
else:
data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')
if results.shape[0] % 4 == 0:
print(results.reshape((-1, 4)))
else:
results = np.pad(results, (0, COLUMN_NUM - len(results) % COLUMN_NUM),
'constant')
data = data.reshape((-1, COLUMN_NUM))
results = results.reshape((-1, COLUMN_NUM))
final_matrix = np.concatenate((data, results))
final_matrix2 = []
for i in range(len(data)):
final_matrix2.append(list(data[i]))
final_matrix2.append(list(results[i]))
print(final_matrix)
print(final_matrix2)
and I have this result
[ 3.32900000e+01 3.32300000e+01 3.32300000e+01 3.32900000e+01]
[ 3.32300000e+01 3.32900000e+01 3.32900000e+01 3.32900000e+01]
[ 3.33300000e+01 3.32900000e+01 3.33300000e+01 3.32900000e+01]
[ 3.33300000e+01 3.33300000e+01 3.33700000e+01 3.33300000e+01]
[ 3.33300000e+01 3.33300000e+01 3.33300000e+01 3.33700000e+01]
[ 3.33700000e+01 3.33700000e+01 3.33700000e+01 0.00000000e+00]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 0.00000000e+00]]
[[33.289999999999999, 33.229999999999997, 33.229999999999997, 33.289999999999999], [1376460059.0, 1376460059.25, 1376460059.5, 1376460059.75], [33.229999999999997, 33.289999999999999, 33.289999999999999, 33.289999999999999], [1376460060.0, 1376460060.25, 1376460060.5, 1376460060.75], [33.329999999999998, 33.289999999999999, 33.329999999999998, 33.289999999999999], [1376460061.0, 1376460061.25, 1376460061.5, 1376460061.75], [33.329999999999998, 33.329999999999998, 33.369999999999997, 33.329999999999998], [1376460062.0, 1376460062.25, 1376460062.5, 1376460062.75], [33.329999999999998, 33.329999999999998, 33.329999999999998, 33.369999999999997], [1376460063.0, 1376460063.25, 1376460063.5, 1376460063.75], [33.369999999999997, 33.369999999999997, 33.369999999999997, 0.0], [1376460064.0, 1376460064.25, 1376460064.5, 0.0]]

Array only filling last value

I'm having a problem with my array only filling the last space with the calculated value. My code is below:
c_volume = 4.45e-5
c_flow_rate = 1.67e-6
acr = c_flow_rate/c_volume
t1 = [3600.0,18000.0, 36000.0]
air_conc_t1 = [6.42404968e+02, 2.74977722e+02, 1.45282562e+02]
t2 = [7200.0, 21600.0, 39600.0]
air_conc_t2 = [4.53346985e+02, 2.41359268e+02, 1.28038071e+02]
===============================================================
n_calc = np.zeros((len(t1),1), dtype='f')
def n(t1, air_conc_t1, t2, air_conc_t2):
return (1/(t2-t1))*(np.log(air_conc_t1/air_conc_t2))
for i in range(len(t1)):
n_calc[i] = n(t1[i], air_conc_t1[i], t2[i], air_conc_t2[i])
===============================================================
calc_f1 = np.zeros((len(t1),1), dtype='f')
calc_f2 = np.zeros((len(t1),1), dtype='f')
calc_N = np.zeros((len(t1),1), dtype='f')
def f1(acr, n_calc):
return (acr+n_calc)/n_calc
calc_f1[i] = f1(acr, n_calc[i])
def f2(acr, n_calc):
return (acr-n_calc)/n_calc
calc_f2[i] = f2(acr, n_calc[i])
def N(R, calc_root, m_thickness, calc_f1, calc_f2):
return (2*R*np.tan(calc_root*m_thickness))/(calc_root*m_thickness*
(calc_f1+calc_f2*calc_root*m_thickness*((1/np.tan(calc_root\
*m_thickness))+np.tan(calc_root*m_thickness))))
for i in xrange(len(t1)):
calc_N[i] = N(R, calc_root[i], m_thickness, calc_f1[i], calc_f2[i])
print calc_f1
print calc_f2
print calc_N
I'm getting the following printed:
[[ 0. ]
[ 0. ]
[ 1070.23657227]]
[[ 0. ]
[ 0. ]
[ 1068.2364502]]
[[ inf]
[ inf]
[ 3.55326119e-06]]
I'm not sure why the first two values of the array are not being filled, but the last one is. I've calculated them by hand and get values.
I'm very new to programming so any help would be appreciated.
Thanks,
Shane
your calls to f1 and f2 are not in a loop. Note that everything at the same level of indentation is included in the loop.
try this instead:
for i in xrange(len(t1)):
calc_f1[i] = f1(acr, n_calc[i])
calc_f2[i] = f2(acr, n_calc[i])
calc_N[i] = N(R, calc_root[i], m_thickness, calc_f1[i], calc_f2[i])
print calc_f1
print calc_f2
print calc_N
Lines
calc_f1[i] = f1(acr, n_calc[i])
calc_f2[i] = f2(acr, n_calc[i])
are outside any for loop (maybe it is only problem with indentions).

Categories

Resources