python - Apparent creation of array from another array? -
i have following code snippet scipy:
resdat = data[scipy.random.randint(0,n,(n,))] what try understand how , why line works. randint function seems return list of n integer values in range of data indizes, interpret line of code resdat become array n random values data. tried replicate in python shell:
a=[1,2,3,4,5,6] b=[1,2] c=a[b] however if try - on line 3 - error
typeerror: list indices must integers, not list which knowledge means, need give number instead of list. why line @ top working then? have feeling missing important distinction, can't figure out one.
coming .net background first line looks bit linq statement, comparable?
i believe data of type -
numpy.ndarray you can type(data) should comes out numpy.ndarray .
also , scipy.random.randint() returns value of type numpy.ndarray .
you may not able lst[[1,2]] , can use numpy.ndarray subscript numpy.ndarray .
a simple example -
import numpy np data = np.array([10,15,20,25,30]) print(data[np.array([1,2,3])]) >> array([15,20,25])
Comments
Post a Comment