Saturday 15 February 2014

python - ValueError: too many boolean indices for a n=600 array (float) -



python - ValueError: too many boolean indices for a n=600 array (float) -

i getting issue trying run (on python):

#loading in text file in need of analysis x,y=loadtxt('2.8k 293k 15102014_rerun 47_0k.txt',skiprows=1,unpack=true,dtype=float,delimiter=",") c=-1.0 #need flip voltage axis yone=c*y #actually flipping array plot(x,yone)#test origin=600.0#where origin? i.e v=0, taking 0 1v elements of array xorg=x[origin:1201]# array origin final point (n) xfit=xorg[(x>0.85)==true] # taking array origin , shortening farther relevant area

it returns valueerror. have tried doing process much smaller array of 10 elements , xfit=xorg[(x>0.85)==true] command works fine. programme trying narrow field of vision, of data, relevant point can fit line of best fit linear element of data.

i apologise formatting beingness messy first question have asked on website cannot search can understand going wrong.

this reply people don't know numpy arrays (like me), mre pointers numpy docs.

numpy arrays have nice feature of boolean masks.

for numpy arrays, operators homecoming array of operation applied every element - instead of single result in plain python lists:

>>> alist = range(10) >>> alist [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> alist > 5 true >>> anarray = np.array(alist) >>> anarray array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> anarray > 5 array([false, false, false, false, false, false, true, true, true, true], dtype=bool)

you can utilize array of bool index numpy array, in case filtered array positions corresponding bool array element true.

>>> mask = anarray > 5 >>> anarray[mask] array([6, 7, 8, 9])

the mask must not bigger array:

>>> anotherarray = anarray[mask] >>> anotherarray array([6, 7, 8, 9]) >>> anotherarray[mask] valueerror: many boolean indices

so cant utilize mask bigger array masking:

>>> anotherarray[anarray > 7] valueerror: many boolean indices >>> anotherarray[anotherarray > 7] array([8, 9])

since xorg smaller x, mask based on x longer xorg , valueerror exception.

python arrays numpy boolean

No comments:

Post a Comment