我想提取 NDVI 时间序列的线性趋势。当我使用以下代码在 Python 中使用线性回归时:
from sklearn.linear_model import LinearRegression
import pandas as pd
data_orig = pd.read_csv('NDVI.csv')
N_values = data_orig['N'].values.reshape(-1, 1)
NDVI_values = data_orig['NDVI'].values
model = LinearRegression()
model.fit(N_values, NDVI_values)
slope = model.coef_[0]
intercept = model.intercept_
print("Linear trend equation: NDVI = {:.4f} * N + {:.4f}".format(slope, intercept))
这段代码的输出是
Linear trend equation: NDVI = 0.0000 * N + 0.0485
。
我认为这个方程是不正确的,因为时间序列中的植被明显随着时间的推移而增加。
我也用这段代码尝试过LOESS:
import matplotlib.pyplot as plt
import pandas
from pandas.plotting import register_matplotlib_converters
data_orig = pandas.read_csv('NDVI.csv')
from statsmodels.tsa.seasonal import STL
res = STL(data_orig['NDVI'], period=46, trend_jump=460).fit()
由此我得到了良好且显着的上升趋势。但是,我想知道是否可以将
trend_jump
设置为 460(这是我的时间序列值的数量)。
print("Linear trend equation: NDVI = {:.4f} * N + {:.4f}".format(slope, intercept))
此代码在打印前将斜率四舍五入到小数点后四位。目测,在 460 步的过程中,它似乎上升了 0.015。
这意味着斜率大约为 0.015 / 460 = 0.000032。四舍五入到小数点后四位,即 0.0000。
因此,我认为线性回归可能是检测斜率 - 但在四舍五入后为零。
我认为这可能会起作用:
print("Linear trend equation: NDVI = {:.8f} * N + {:.4f}".format(slope, intercept))