时间序列--差分代码

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()

手动差分--默认是剪去上一个

时间序列--差分代码_第1张图片差分之前

时间序列--差分代码_第2张图片 差分之后

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/

你可能感兴趣的:(时间序列)