如果我有50个元素的数组,我将如何计算3个周期斜率和5个周期斜率?文档不添加太多.....
>>> from scipy import stats
>>> import numpy as np
>>> x = np.random.random(10)
>>> y = np.random.random(10)
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
Run Code Online (Sandbox Code Playgroud)
这会有用吗?
def slope(x, n):
if i<len(x)-n:
slope = stats.linregress(x[i:i+n],y[i:i+n])[0]
return slope
Run Code Online (Sandbox Code Playgroud)
但是数组的长度是相同的
@joe :::
xx = [2.0 ,4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
x = np.asarray(xx, np.float)
s = np.diff(x[::3])/3
window = [1, 0, 0, 0, -1]
window2 = [1, 0, -1]
slope = np.convolve(x, window, mode='same') / (len(window) - 1)
slope2 = np.convolve(x, window2, mode='same') / (len(window2) - 1)
print x
print s
print slope
print slope2
Run Code Online (Sandbox Code Playgroud)
结果.....
[ 2. 4. 6. 8. 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30.]
[ 2. 2. 2. 2.]
[ 1.5 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. -6. -6.5]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. -14.]
Run Code Online (Sandbox Code Playgroud)
斜率和斜率2是Im之后的除了-6,-6.5和-14不是我正在寻找的结果.
这工作.......
window = [1, 0, 0, -1]
slope = np.convolve(xx, window, mode='valid') / float(len(window) - 1)
padlength = len(window) -1
slope = np.hstack([np.ones(padlength), slope])
print slope
Run Code Online (Sandbox Code Playgroud)
我假设你的意思是计算每个第3和第5个元素的斜率,这样你就有了一系列(精确的,非最小二乘)斜率?
如果是这样,你只需要做一些事情:
third_period_slope = np.diff(y[::3]) / np.diff(x[::3])
fifth_period_slope = np.diff(y[::5]) / np.diff(x[::5])
Run Code Online (Sandbox Code Playgroud)
不过,我可能完全误解了你的意思.我以前从未领过"3期斜率"一词......
如果你想要更多的"移动窗口"计算(这样输入元素的数量与输出元素相同),只需将其建模为带有[-1, 0, 1]或的窗口的卷积[-1, 0, 0, 0, 1].
例如
window = [-1, 0, 1]
slope = np.convolve(y, window, mode='same') / np.convolve(x, window, mode='same')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17307 次 |
| 最近记录: |