1. 程式人生 > >時間序列--差分程式碼

時間序列--差分程式碼

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

手動差分--預設是剪去上一個

Shampoo Sales Dataset Plot差分之前

Manually Differenced Shampoo Sales Dataset 差分之後

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/