Printing a list of two numpy arrays in Python

Question:

I am trying to print Path which should consist of a list of two numpy arrays but it is only printing the last array. I present the current and expected output.

import numpy as np

Ii01 = [np.array([[0, 2],
        [0, 3],
        [0, 4],
        [1, 5],
        [2, 3],
        [2, 4],
        [3, 1],
        [3, 5],
        [4, 3],
        [4, 6],
        [5, 6],
        [5, 7],
        [5, 8],
        [6, 7],
        [6, 8],
        [7, 8]]),
 np.array([[0, 2],
        [0, 3],
        [0, 4],
        [1, 5],
        [2, 3],
        [2, 4],
        [3, 1],
        [3, 5],
        [4, 3],
        [4, 6],
        [4, 7],
        [5, 8],
        [5, 9],
        [6, 7],
        [7, 5],
        [7, 8],
        [7, 9],
        [8, 9]])]

Iv01 = [np.array([[0.5928417218382795  ],
        [1.0598523225018839  ],
        [1.0038922911359835  ],
        [0.5228083275600917  ],
        [0.45152663276269006 ],
        [0.33163464481335625 ],
        [0.6382821834929433  ],
        [1.1929711967814043  ],
        [0.151430568179323   ],
        [1.2882852997187433  ],
        [0.020634234459323438],
        [0.3371248675176444  ],
        [1.0339712303899318  ],
        [0.3170028087537722  ],
        [1.0107667057670484  ],
        [0.645202745957077   ]]),
 np.array([[0.6413512609273813 ],
        [1.101096390751953  ],
        [1.1271899958869345 ],
        [0.5129288710922218 ],
        [0.44299417932362256],
        [0.39992456692863954],
        [0.6262206291648664 ],
        [1.1704277398685619 ],
        [0.0811025900626848 ],
        [0.4804819688823268 ],
        [1.1198920618079702 ],
        [0.33075425274871   ],
        [1.0794187146885863 ],
        [0.5875522376738217 ],
        [0.05687040105172758],
        [0.3822797557154033 ],
        [1.1388373266868645 ],
        [0.6979967492676955 ]])]

for i in range(0,len(Ii01)):
        uniq = np.unique(Ii01[i][:, 1][None, :])
        y =Iv01[i][:, 0]
        x = Ii01[i][:, 1][:, None] == uniq
        output = np.vstack((np.linalg.lstsq(x, y, rcond=None)[0], uniq)).T
        Path=output[:,0]
        Path = np.insert(Path, 0, 10)   #fixed inlet
print([Path])        

The current output is

[array([10.                 ,  0.6262206291648664 ,  0.6413512609273813 ,
        0.5417310533794202 ,  0.763557281407787  ,  0.580075670670837  ,
        0.48048196888232686,  0.8537221497408958 ,  0.35651700423205657,
        0.9720842635477158 ])]

The expected output is

[array([10.                 ,  0.6382821834929432 ,  0.5928417218382795 ,
        0.5542698411479658 ,  0.6677634679746701 ,  0.8578897621707481 ,
        0.6544597670890333 ,  0.32706383813570833,  0.8966468940380192 ]),
array([10.                 ,  0.6262206291648664 ,  0.6413512609273813 ,
        0.5417310533794202 ,  0.763557281407787  ,  0.580075670670837  ,
        0.48048196888232686,  0.8537221497408958 ,  0.35651700423205657,
        0.9720842635477158 ])]
Asked By: user19657580

||

Answers:

That’s because you are always overwriting Path:

Path = []
for i in range(0, len(Ii01)):
    uniq = np.unique(Ii01[i][:, 1][None, :])
    y = Iv01[i][:, 0]
    x = Ii01[i][:, 1][:, None] == uniq
    output = np.vstack((np.linalg.lstsq(x, y, rcond=None)[0], uniq)).T
    Path.append(np.insert(output[:, 0], 0, 10))  # fixed inlet
print(Path)
Answered By: mauro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.