時間序列--差分程式碼
阿新 • • 發佈:2019-01-01
from pandas import read_csv from pandas import datetime from pandas import Series from matplotlib import pyplot def parser(x): return datetime.strptime('190'+x, '%Y-%m') # create a differenced series def difference(dataset, interval=1): diff = list() for i in range(interval, len(dataset)): value = dataset[i] - dataset[i - interval] diff.append(value) return Series(diff) series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser) X = series.values diff = difference(X) pyplot.plot(diff) pyplot.show()
手動差分--預設是剪去上一個
差分之前
差分之後
2.自動差分
from pandas import read_csv from pandas import datetime from matplotlib import pyplot def parser(x): return datetime.strptime('190'+x, '%Y-%m') series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser) diff = series.diff() pyplot.plot(diff) pyplot.show()
還是預設1.可以手動設定
https://machinelearningmastery.com/difference-time-series-dataset-python/