简体   繁体   中英

Assign numpy ndarray to list

Can someone help me with my question. I have created a loop which in every execution produce a numpy.ndarray of size (5,) but when the loop terminates and I want to print the results of my code it only print the last ndarry of size 5, I tried to assigned the results in a list but I get "too many indices for array"

k=0;
for i in range(M):
    for j in range(N):
        if table[i, j] != 0:
            k=k+1;
            inv=np.linalg.inv(np.dot(X.T,X));
            theta[k,:] = np.dot(inv,X.T).dot(HSI[i,j,:])

I want to assign the results on theta[] so if I want to print the result from the second execution I will write theta[1] and so on.

Most likely my false is on the last line

A quick sketch, you can amend as necessary

import random

a=[] #start with an empty set
k=0
while k< 5:
    b=random.sample(range(1,100), 5) #get a random sample of length 5 within range of 1-100
    a.append(b) # add/'append' b to your currently empty set of 'a'
    print a #print current contents of 'a'
    k=k+1

I've not run this but it seems intuitive.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM