我在尝试运行(在 Python 上)时遇到问题:
#Loading in the text file in need of analysis
x,y=loadtxt('2.8k to 293k 15102014_rerun 47_0K.txt',skiprows=1,unpack=True,dtype=float,delimiter=",")
C=-1.0 #Need to flip my voltage axis
yone=C*y #Actually flipping the array
plot(x,yone)#Test
origin=600.0#Where is the origin? i.e V=0, taking the 0 to 1V elements of array
xorg=x[origin:1201]# Array from the origin to the final point (n)
xfit=xorg[(x>0.85)==True] # Taking the array from the origin and shortening it further to get relevant area
它返回 ValueError。我尝试使用更小的 10 个元素数组来执行此过程,并且 xfit=xorg[(x>0.85)==True]
命令工作正常。该程序试图做的是将某些数据的视野缩小到相关点,以便我可以拟合一条最适合数据线性元素的线。
对于格式困惑,我深表歉意,但这是我在该网站上提出的第一个问题,因为我无法搜索我可以理解哪里出错的内容。
最佳答案
这个答案是为那些不了解 numpy 数组的人(比如我)准备的,感谢 MrE 提供了 numpy 文档的指针。
Numpy 数组具有 boolean 掩码这个很好的功能。
对于 numpy 数组,大多数运算符返回应用于每个元素的操作数组 - 而不是像普通 Python 列表中那样的单个结果:
>>> 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)
您可以使用 bool 数组作为 numpy 数组的索引,在这种情况下,您将获得一个过滤后的数组,其中对应的 bool 数组元素为 True 的位置。
>>> mask = anarray > 5
>>> anarray[mask]
array([6, 7, 8, 9])
掩码不得大于数组:
>>> anotherarray = anarray[mask]
>>> anotherarray
array([6, 7, 8, 9])
>>> anotherarray[mask]
ValueError: too many boolean indices
所以你不能使用比你正在屏蔽的数组更大的掩码:
>>> anotherarray[anarray > 7]
ValueError: too many boolean indices
>>> anotherarray[anotherarray > 7]
array([8, 9])
由于 xorg
小于 x
,因此基于 x
的掩码将比 xorg
长,并且您获取 ValueError
异常。
关于python - 值错误: too many boolean indices for a n=600 array (float),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26778500/