'Filling an empty matrix with a for loop

I am trying to create a 3195x71 matrix in Python using iterations and equations. The code is referenced below:-

##Creating an empty matrix
import numpy as np
CC_FINAL = np.empty((3195,71))
for i in range(3195):
    eB1_s = eB1[i]
    Ys = Y.iloc[i]
    Y_s = Ys.values.reshape(1,71)
    CC_s = np.dot(eB1_s,Y_s)
    CClist=CC_s
    CCdf=pd.DataFrame(CClist) 
    CC_final_demand_s=np.add(CC_s,HC,size=(1,71))
    print(CC_final_demand_s)
    Final_DD = np.concatenate((CC_FINAL,CC_final_demand_s))
print(Final_DD)

The equation CC_final_demand_s=np.add(CC_s,HC,size=(1,71)) checks out and I get my desired output. The output is to look like this (upto 71 columns, 3195 times)

 0           1             2            3           4   \
1  5913403.203026  214.118854  15892.865306  5379.204602  333.111413   

          5          6            7          8          9          10  \
1  499.54388  27.436068  8302.752527  293.54779  30.622154  87.851813   

             11            12          13         14         15          16  \
1  10121.788307  38874.323567  120.792053  46.108922  20.592629  727.064092   

           17            18            19            20        21        22  \
1  7838.86962  30496.167176  92945.842897  40627.994136  4.506425  8.177975   

But when I'm trying to combine the results from the equation above into the empty matrix, something does not seem to be working correct. I get the correct dimensions for Final_DD but the output is not right. Any suggestions are greatly appreciated!



Solution 1:[1]

You start with CC_FINAL = np.empty((3195,71)) and don't change it. Did you look at its values? I suspect you don't understand what np.empty does.

In the loop you do

Final_DD = np.concatenate((CC_FINAL,CC_final_demand_s))

That is joining the (3195,71) array with a (1,71) array producing a (3196,71), where most of the rows are garbage. You do that again and again, without changing CC_FINAL or preserving Final_DD. You just concatenate on a different CC_final_demand_s. You are not "filling" CC_FINAL at all.

Work through the logic in more detail. It may help for make much smaller arrays so you can actually look at the their values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hpaulj