我正在使用Python的一个频谱,我已经为该频谱设置了一条线。我想要一个代码,可以检测光谱上是否有10个数据点在一行中小于拟合线。有人知道这是一种简单快捷的方法吗
我现在有这样的东西:
count = 0
for i in range(lowerbound, upperbound):
if spectrum[i] < fittedline[i]
count += 1
if count > 15:
*do whatever*
如果我将第一个If语句行更改为:
if spectrum[i] < fittedline[i] & spectrum[i+1] < fittedline[i+1] & so on
我相信这个算法会起作用,但是如果我想让用户输入一个数字,表示一行中有多少数据点必须小于拟合线,那么有没有更聪明的方法让我自动完成这个任务
我的建议是,在开发特殊功能之前,研究并使用现有库
在这种情况下,一些超级聪明的人开发了数字python库numpy。该库在科学项目中广泛使用,它有大量有用的功能实现,这些功能实现经过测试和优化
您的需求可以通过以下行来满足:
但让我们一步一步走:
现在让我们考虑一下,在你的光谱中有10个点,你有10米。代码效率是一个需要考虑的关键问题,NUMPY在那里可以节省帮助:你的尝试很接近成功!对于连续的点,如果一个点不满足您的条件,您只需重置计数
让我们测试一下:
使用这些值运行代码可以得到:
相关问题 更多 >
编程相关推荐