# 时间序列--去趋势

https://machinelearningmastery.com/time-series-trends-in-python/

# 差分法去趋势

value(t) = observation(t) - observation(t-1)


1. from pandas import read_csv

2. from pandas import datetime

3. from matplotlib import pyplot

4.
5. def parser(x):

6. return datetime.strptime('190'+x, '%Y-%m')

7.
8. series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)

9. X = series.values

10. diff = list()

11. for i in range(1, len(X)):

12. value = X[i] - X[i - 1]

13. diff.append(value)

14. pyplot.plot(diff)

15. pyplot.show()

# 拟合模型去趋势

X,y

1,obs1

2,obs2

3,obs3

4,obs4

5,obs5

value(t) = observation(t) - prediction(t)


1. from pandas import read_csv

2. from pandas import datetime

3. from sklearn.linear_model import LinearRegression

4. from matplotlib import pyplot

5. import numpy

6.
7. def parser(x):

8. return datetime.strptime('190'+x, '%Y-%m')

9.
10. series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)

11. # fit linear model

12. X = [i for i in range(0, len(series))]

13. X = numpy.reshape(X, (len(X), 1))

14. y = series.values

15. model = LinearRegression()

16. model.fit(X, y)

17. # calculate trend

18. trend = model.predict(X)

19. # plot trend

20. pyplot.plot(y)

21. pyplot.plot(trend)

22. pyplot.show()

23. # detrend

24. detrended = [y[i]-trend[i] for i in range(0, len(series))]

25. # plot detrended

26. pyplot.plot(detrended)

27. pyplot.show()

12-23 1万+
02-12 2404
09-17 13万+
10-18 4万+
02-15 3万+
07-04 443
12-27 1万+
03-22 8万+