matlibplot polyfit到x值的子集

2024-04-25 17:14:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在dictionary对象中有x和y数据,格式为{'item1':(x,y),'item2':(x,y)….}其中每个x和y值都是100个数字的列表。 对于每个键,我的x值从0到50。我只想根据x>;=10和x<;=20的数据拟合一条直线。 这就是我要做的。。。在

for key,value in Dict.iteritems():
   #Get x,y values from each key in turn.
   [x,y] =  Dict.get(key) 
   # Extract just the x values in range. 
   xFit = [i for i in x if (i>=10 and i<=20)]

   << yFit = Get the corresponding y values for xFit >>   

   p = polyfit(xFit, yFit, 1)

有没有一种很好的方法可以在所需的范围内为[x,y]数据拟合一条线? 提前谢谢你的帮助。在


Tags: the数据对象keyinforgetdictionary
1条回答
网友
1楼 · 发布于 2024-04-25 17:14:56

您可以在np.array中同时转换x和{},这将使使用花式索引的条件切片更容易:

import numpy as np
x = np.array(x)
y = np.array(y)
cond = (x>=10) & (x<=20)
xFit = x[ cond ]
yFit = y[ cond ]

相关问题 更多 >