# Install on Terminal of MacOS #pip3 install -U pandas #pip3 install -U matplotlib #pip3 install -U statsmodels |
1_MacOS_Terminal.txt
########## Run Terminal on MacOS and execute ### TO UPDATE cd "YOUR_WORKING_DIRECTORY" python3 ts.py airline-passengers.csv multiplicative 12 |
Input Data files
airline-passengers.csv
"Month","Passengers" "1949-01",112 "1949-02",118 "1949-03",132 "1949-04",129 "1949-05",121 "1949-06",135 "1949-07",148 "1949-08",148 "1949-09",136 "1949-10",119 "1949-11",104 "1949-12",118 "1950-01",115 "1950-02",126 "1950-03",141 "1950-04",135 "1950-05",125 "1950-06",149 "1950-07",170 "1950-08",170 "1950-09",158 "1950-10",133 "1950-11",114 "1950-12",140 "1951-01",145 "1951-02",150 "1951-03",178 "1951-04",163 "1951-05",172 "1951-06",178 "1951-07",199 "1951-08",199 "1951-09",184 "1951-10",162 "1951-11",146 "1951-12",166 "1952-01",171 "1952-02",180 "1952-03",193 "1952-04",181 "1952-05",183 "1952-06",218 "1952-07",230 "1952-08",242 "1952-09",209 "1952-10",191 "1952-11",172 "1952-12",194 "1953-01",196 "1953-02",196 "1953-03",236 "1953-04",235 "1953-05",229 "1953-06",243 "1953-07",264 "1953-08",272 "1953-09",237 "1953-10",211 "1953-11",180 "1953-12",201 "1954-01",204 "1954-02",188 "1954-03",235 "1954-04",227 "1954-05",234 "1954-06",264 "1954-07",302 "1954-08",293 "1954-09",259 "1954-10",229 "1954-11",203 "1954-12",229 "1955-01",242 "1955-02",233 "1955-03",267 "1955-04",269 "1955-05",270 "1955-06",315 "1955-07",364 "1955-08",347 "1955-09",312 "1955-10",274 "1955-11",237 "1955-12",278 "1956-01",284 "1956-02",277 "1956-03",317 "1956-04",313 "1956-05",318 "1956-06",374 "1956-07",413 "1956-08",405 "1956-09",355 "1956-10",306 "1956-11",271 "1956-12",306 "1957-01",315 "1957-02",301 "1957-03",356 "1957-04",348 "1957-05",355 "1957-06",422 "1957-07",465 "1957-08",467 "1957-09",404 "1957-10",347 "1957-11",305 "1957-12",336 "1958-01",340 "1958-02",318 "1958-03",362 "1958-04",348 "1958-05",363 "1958-06",435 "1958-07",491 "1958-08",505 "1958-09",404 "1958-10",359 "1958-11",310 "1958-12",337 "1959-01",360 "1959-02",342 "1959-03",406 "1959-04",396 "1959-05",420 "1959-06",472 "1959-07",548 "1959-08",559 "1959-09",463 "1959-10",407 "1959-11",362 "1959-12",405 "1960-01",417 "1960-02",391 "1960-03",419 "1960-04",461 "1960-05",472 "1960-06",535 "1960-07",622 "1960-08",606 "1960-09",508 "1960-10",461 "1960-11",390 "1960-12",432 |
Python files
########## Additive Model and Multiplicative Model : Decomposing Time Series Data into Trend, Seasonality, and Residual (plus Monthly Average) ######### # # #Run this code on Terminal of MacOS as follows: #python3 ts.py (raw dataset csv file) (model:additive or multiplicative) (frequency) # #For instance, #python3 ts.py airline-passengers.csv multiplicative 12 # #Additive Model # y(t) = Level + Trend + Seasonality + Noise # #Multiplicative Model #y(t) = Level * Trend * Seasonality * Noise # # #Input dataset: #https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv # # #Reference: #https://machinelearningmastery.com/decompose-time-series-data-trend-seasonality/ #https://stackoverflow.com/questions/45184055/how-to-plot-multiple-seasonal-decompose-plots-in-one-figure ##### import import sys import datetime import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose ##### arguments csvf = str(sys.argv[1]) # e.g., airline-passengers.csv md = str(sys.argv[2]) # e.g., multiplicative frq = int(sys.argv[3]) # e.g., 12 ##### Plot: Raw Dataset dt_now = datetime.datetime.now() #print(dt_now) # 2019-02-04 21:04:15.412854 series = pd.read_csv(csvf, header=0, index_col=0) #print(series) ''' Passengers Month 1949-01 112 1949-02 118 1949-03 132 1949-04 129 1949-05 121 ... ... 1960-08 606 1960-09 508 1960-10 461 1960-11 390 1960-12 432 ''' #print(type(series)) #<class 'pandas.core.frame.DataFrame'> series.plot(figsize=(12,9)) plt.title("Raw Data") plt.savefig("Figure_1_raw_data_" + dt_now.strftime('%Y-%m-%d_%H%M%S') + ".png") plt.show() ##### Plot: Decomposing Raw (Observed) Data into Trend, Seasonality, and Residual dta = series #print(dta) ''' Passengers Month 1949-01 112 1949-02 118 1949-03 132 1949-04 129 1949-05 121 ... ... 1960-08 606 1960-09 508 1960-10 461 1960-11 390 1960-12 432 [144 rows x 1 columns] ''' #print(type(dta)) #<class 'pandas.core.frame.DataFrame'> yname = dta.columns[0] dta.eval(yname).interpolate(inplace=True) ##statsmodels.api.tsa.seasonal_decompose(dta.eval(yname)) #res = seasonal_decompose(dta.eval(yname), model=md, freq=frq) res = seasonal_decompose(dta.eval(yname), model=md, period=frq) #print(res) rescsv = pd.concat([res.observed, res.trend, res.seasonal, res.resid], axis=1, join='outer') #print(rescsv) pd.DataFrame(data=rescsv).to_csv("rescsv.csv", header=True, index=True) def plotseasonal(res, axes ): res.observed.plot(ax=axes[0], legend=False) axes[0].set_ylabel('Observed') res.trend.plot(ax=axes[1], legend=False) axes[1].set_ylabel('Trend') res.seasonal.plot(ax=axes[2], legend=False) axes[2].set_ylabel('Seasonal') res.resid.plot(ax=axes[3], legend=False) axes[3].set_ylabel('Residual') ##### plot # # [Case A] When plotting one raw dataset and its trend, seasonality, and residual fig, axes = plt.subplots(ncols=1, nrows=4, sharex=True, figsize=(12,9)) #print(type(fig)) #<class 'matplotlib.figure.Figure'> # #print(type(axes)) #<class 'numpy.ndarray'> # # #print(axes) ''' [<matplotlib.axes._subplots.AxesSubplot object at 0x128497130> <matplotlib.axes._subplots.AxesSubplot object at 0x129abe2b0> <matplotlib.axes._subplots.AxesSubplot object at 0x129aea430> <matplotlib.axes._subplots.AxesSubplot object at 0x129b17580>] ''' plotseasonal(res, axes[:]) # # # [Case B] When plotting three raw datasets and its trend, seasonality, and residual #fig, axes = plt.subplots(ncols=3, nrows=4, sharex=True, figsize=(12,5)) #plotseasonal(res, axes[:,0]) #plotseasonal(res, axes[:,1]) #plotseasonal(res, axes[:,2]) plt.tight_layout() plt.savefig("Figure_2_raw_data_trend_seasonality_residual_" + md + "_" + dt_now.strftime('%Y-%m-%d_%H%M%S') + ".png") plt.show() ##### Plot: Raw (Observed) Data Monthly Average # Convert to Pandas.Series (x and index: Month, y: Passengers) dta2 = pd.Series(dta[yname], dtype='float') # #print(dta2) ''' Month 1949-01 112.0 1949-02 118.0 1949-03 132.0 1949-04 129.0 1949-05 121.0 ... 1960-08 606.0 1960-09 508.0 1960-10 461.0 1960-11 390.0 1960-12 432.0 Name: Passengers, Length: 144, dtype: float64 ''' # #print(dta2.index) ''' Index(['1949-01', '1949-02', '1949-03', '1949-04', '1949-05', '1949-06', '1949-07', '1949-08', '1949-09', '1949-10', ... '1960-03', '1960-04', '1960-05', '1960-06', '1960-07', '1960-08', '1960-09', '1960-10', '1960-11', '1960-12'], dtype='object', name='Month', length=144) ''' #print(dta2.index.values) # #dta2.index = pd.to_datetime(dta2['Month']) dta2.index = pd.to_datetime(dta2.index) # #print(dta2.index) ''' DatetimeIndex(['1949-01-01', '1949-02-01', '1949-03-01', '1949-04-01', '1949-05-01', '1949-06-01', '1949-07-01', '1949-08-01', '1949-09-01', '1949-10-01', ... '1960-03-01', '1960-04-01', '1960-05-01', '1960-06-01', '1960-07-01', '1960-08-01', '1960-09-01', '1960-10-01', '1960-11-01', '1960-12-01'], dtype='datetime64[ns]', name='Month', length=144, freq=None) ''' # monthly_mean = dta2.groupby(dta2.index.month).mean() #print(monthly_mean) ''' Month 1 241.750000 2 235.000000 3 270.166667 4 267.083333 5 271.833333 6 311.666667 7 351.333333 8 351.083333 9 302.416667 10 266.583333 11 232.833333 12 261.833333 Name: Passengers, dtype: float64 ''' #print(type(monthly_mean)) #<class 'pandas.core.series.Series'> pd.DataFrame(data=monthly_mean).to_csv("monthly_mean.csv", header=True, index=True) monthly_mean.plot(kind='bar') plt.title("Raw Data: Monthly Average") plt.savefig("Figure_3_raw_data_monthly average_" + dt_now.strftime('%Y-%m-%d_%H%M%S') + ".png") plt.show() |
Output Data files
rescsv.csv
Month,Passengers,trend,seasonal,resid 1949-01,112.0,,0.9102303673722006, 1949-02,118.0,,0.8836253206943756, 1949-03,132.0,,1.0073662876035454, 1949-04,129.0,,0.9759060123228472, 1949-05,121.0,,0.9813780274951293, 1949-06,135.0,,1.1127758266792729, 1949-07,148.0,126.79166666666666,1.2265555429312012,0.9516643164028834 1949-08,148.0,127.24999999999999,1.2199109694456252,0.9534014056242447 1949-09,136.0,127.95833333333331,1.0604919326468183,1.00221976781656 1949-10,119.0,128.58333333333331,0.9217572404104976,1.0040277671042104 1949-11,104.0,128.99999999999997,0.8011780824134743,1.0062701015971256 1949-12,118.0,129.75,0.8988243899850112,1.011811921520978 1950-01,115.0,131.25,0.9102303673722006,0.9626029932620286 1950-02,126.0,133.08333333333334,0.8836253206943756,1.0714668098946898 1950-03,141.0,134.91666666666669,1.0073662876035454,1.0374474253490114 1950-04,135.0,136.41666666666666,0.9759060123228472,1.0140476000435144 1950-05,125.0,137.41666666666669,0.9813780274951293,0.9269029690018589 1950-06,149.0,138.75000000000003,1.1127758266792729,0.9650406201566315 1950-07,170.0,140.91666666666666,1.2265555429312012,0.9835565624018564 1950-08,170.0,143.16666666666669,1.2199109694456252,0.9733720498615518 1950-09,158.0,145.70833333333331,1.0604919326468183,1.022504733680162 1950-10,133.0,148.41666666666666,0.9217572404104976,0.9721928212238378 1950-11,114.0,151.54166666666666,0.8011780824134743,0.9389527366650869 1950-12,140.0,154.70833333333331,0.8988243899850112,1.006791359050623 1951-01,145.0,157.125,0.9102303673722006,1.0138445970332666 1951-02,150.0,159.54166666666666,0.8836253206943756,1.0640180175116092 1951-03,178.0,161.83333333333331,1.0073662876035454,1.0918541020514378 1951-04,163.0,164.125,0.9759060123228472,1.0176650782477634 1951-05,172.0,166.66666666666666,0.9813780274951293,1.0515825411682371 1951-06,178.0,169.0833333333333,1.1127758266792729,0.9460443984897541 1951-07,199.0,171.24999999999997,1.2265555429312012,0.9474041369895129 1951-08,199.0,173.58333333333331,1.2199109694456252,0.9397599140119374 1951-09,184.0,175.45833333333331,1.0604919326468183,0.9888637442578553 1951-10,162.0,176.83333333333331,0.9217572404104976,0.9938808513926574 1951-11,146.0,178.04166666666669,0.8011780824134743,1.0235336960240646 1951-12,166.0,180.16666666666669,0.8988243899850112,1.0250824443004556 1952-01,171.0,183.125,0.9102303673722006,1.0258813915429423 1952-02,180.0,186.20833333333331,0.8836253206943756,1.0939695651963173 1952-03,193.0,189.04166666666663,1.0073662876035454,1.0134734098250622 1952-04,181.0,191.29166666666663,0.9759060123228472,0.9695596432636905 1952-05,183.0,193.5833333333333,0.9813780274951293,0.9632672518184556 1952-06,218.0,195.83333333333331,1.1127758266792729,1.0003735367649653 1952-07,230.0,198.0416666666666,1.2265555429312012,0.9468562364697322 1952-08,242.0,199.74999999999997,1.2199109694456252,0.9931170579946488 1952-09,209.0,202.20833333333334,1.0604919326468183,0.9746302068393307 1952-10,191.0,206.24999999999997,0.9217572404104976,1.0046686540245588 1952-11,172.0,210.41666666666666,0.8011780824134743,1.0202797112370305 1952-12,194.0,213.375,0.8988243899850112,1.0115406663515256 1953-01,196.0,215.83333333333331,0.9102303673722006,0.9976684371998933 1953-02,196.0,218.5,0.8836253206943756,1.0151646298680175 1953-03,236.0,220.91666666666663,1.0073662876035454,1.0604644361877407 1953-04,235.0,222.91666666666663,0.9759060123228472,1.0802327213533813 1953-05,229.0,224.08333333333331,0.9813780274951293,1.0413329150091515 1953-06,243.0,224.70833333333331,1.1127758266792729,0.971805633482808 1953-07,264.0,225.33333333333334,1.2265555429312012,0.9551932971060005 1953-08,272.0,225.33333333333334,1.2199109694456252,0.9894989240604416 1953-09,237.0,224.95833333333331,1.0604919326468183,0.9934337063380791 1953-10,211.0,224.58333333333334,0.9217572404104976,1.0192679634537005 1953-11,180.0,224.45833333333331,0.8011780824134743,1.0009392308728386 1953-12,201.0,225.54166666666666,0.8988243899850112,0.9915038921474064 1954-01,204.0,228.0,0.9102303673722006,0.9829784570782156 1954-02,188.0,230.45833333333331,0.8836253206943756,0.9232031554773301 1954-03,235.0,232.24999999999997,1.0073662876035454,1.004441682597805 1954-04,227.0,233.91666666666663,0.9759060123228472,0.9943898827760491 1954-05,234.0,235.625,0.9813780274951293,1.0119479145163468 1954-06,264.0,237.74999999999997,1.1127758266792729,0.9978740263893865 1954-07,302.0,240.5,1.2265555429312012,1.0237752892269063 1954-08,293.0,243.95833333333334,1.2199109694456252,0.98451837489729 1954-09,259.0,247.16666666666666,1.0604919326468183,0.9881036290010385 1954-10,229.0,250.24999999999997,0.9217572404104976,0.9927612987096137 1954-11,203.0,253.49999999999997,0.8011780824134743,0.9995143055122108 1954-12,229.0,257.125,0.8988243899850112,0.9908691997123485 1955-01,242.0,261.8333333333333,0.9102303673722006,1.0154045633681739 1955-02,233.0,266.66666666666663,0.8836253206943756,0.9888240858844841 1955-03,267.0,271.125,1.0073662876035454,0.9775844472954781 1955-04,269.0,275.2083333333333,0.9759060123228472,1.0015732252714535 1955-05,270.0,278.5,0.9813780274951293,0.9878755449160926 1955-06,315.0,281.9583333333333,1.1127758266792729,1.0039635286058564 1955-07,364.0,285.75,1.2265555429312012,1.038551231735955 1955-08,347.0,289.33333333333337,1.2199109694456252,0.9831117071644834 1955-09,312.0,293.24999999999994,1.0604919326468183,1.0032500825069086 1955-10,274.0,297.1666666666667,0.9217572404104976,1.0003083921250941 1955-11,237.0,301.0,0.8011780824134743,0.9827720360378518 1955-12,278.0,305.45833333333326,0.8988243899850112,1.0125534772990237 1956-01,284.0,309.9583333333333,0.9102303673722006,1.006615706613454 1956-02,277.0,314.4166666666667,0.8836253206943756,0.9970250216162121 1956-03,317.0,318.625,1.0073662876035454,0.9876248322104635 1956-04,313.0,321.74999999999994,0.9759060123228472,0.9968223994127333 1956-05,318.0,324.5,0.9813780274951293,0.9985644225806528 1956-06,374.0,327.08333333333326,1.1127758266792729,1.027556011760332 1956-07,413.0,329.54166666666663,1.2265555429312012,1.0217684733250962 1956-08,405.0,331.8333333333333,1.2199109694456252,1.0004764655259983 1956-09,355.0,334.45833333333326,1.0604919326468183,1.0008729746411495 1956-10,306.0,337.5416666666667,0.9217572404104976,0.983507052164718 1956-11,271.0,340.5416666666667,0.8011780824134743,0.9932760726708177 1956-12,306.0,344.0833333333333,0.8988243899850112,0.9894251399019294 1957-01,315.0,348.25,0.9102303673722006,0.993729329946053 1957-02,301.0,353.0,0.8836253206943756,0.964991833258292 1957-03,356.0,357.625,1.0073662876035454,0.9881769386852579 1957-04,348.0,361.375,0.9759060123228472,0.9867636566481572 1957-05,355.0,364.5,0.9813780274951293,0.9924176745109158 1957-06,422.0,367.16666666666674,1.1127758266792729,1.0328601494311507 1957-07,465.0,369.45833333333337,1.2265555429312012,1.0261249953429645 1957-08,467.0,371.2083333333333,1.2199109694456252,1.0312667769357124 1957-09,404.0,372.1666666666666,1.0604919326468183,1.0236147216993436 1957-10,347.0,372.41666666666663,0.9217572404104976,1.0108432339509779 1957-11,305.0,372.75,0.8011780824134743,1.0212995188396765 1957-12,336.0,373.625,0.8988243899850112,1.0005262806704482 1958-01,340.0,375.25,0.9102303673722006,0.9954212223576867 1958-02,318.0,377.9166666666667,0.8836253206943756,0.9522761826670493 1958-03,362.0,379.49999999999994,1.0073662876035454,0.9469114707882059 1958-04,348.0,380.0,0.9759060123228472,0.9383992537400733 1958-05,363.0,380.70833333333337,0.9813780274951293,0.9715785356347435 1958-06,435.0,380.95833333333337,1.1127758266792729,1.0261340434482746 1958-07,491.0,381.8333333333333,1.2265555429312012,1.0483841196851822 1958-08,505.0,383.6666666666666,1.2199109694456252,1.0789695108337816 1958-09,404.0,386.49999999999994,1.0604919326468183,0.9856540205065262 1958-10,359.0,390.3333333333333,0.9217572404104976,0.9977971302743561 1958-11,310.0,394.7083333333333,0.8011780824134743,0.9802939860543499 1958-12,337.0,398.625,0.8988243899850112,0.9405686948769 1959-01,360.0,402.54166666666663,0.9102303673722006,0.9825176026958595 1959-02,342.0,407.1666666666667,0.8836253206943756,0.950573575014168 1959-03,406.0,411.875,1.0073662876035454,0.9785278460391813 1959-04,396.0,416.33333333333326,0.9759060123228472,0.9746439890036597 1959-05,420.0,420.49999999999994,0.9813780274951293,1.0177637071285097 1959-06,472.0,425.5,1.1127758266792729,0.9968613350903052 1959-07,548.0,430.7083333333333,1.2265555429312012,1.0373135823544246 1959-08,559.0,435.125,1.2199109694456252,1.053100054130422 1959-09,463.0,437.7083333333333,1.0604919326468183,0.9974446537535596 1959-10,407.0,440.95833333333326,0.9217572404104976,1.0013370766167895 1959-11,362.0,445.8333333333333,0.8011780824134743,1.0134608455294587 1959-12,405.0,450.625,0.8988243899850112,0.9999191652088765 1960-01,417.0,456.3333333333333,0.9102303673722006,1.0039279399429428 1960-02,391.0,461.37499999999994,0.8836253206943756,0.9590793646523456 1960-03,419.0,465.20833333333326,1.0073662876035454,0.8940856500108696 1960-04,461.0,469.3333333333332,0.9759060123228472,1.0064947912800382 1960-05,472.0,472.74999999999994,0.9813780274951293,1.0173587647555493 1960-06,535.0,475.04166666666663,1.1127758266792729,1.0120789574210476 1960-07,622.0,,1.2265555429312012, 1960-08,606.0,,1.2199109694456252, 1960-09,508.0,,1.0604919326468183, 1960-10,461.0,,0.9217572404104976, 1960-11,390.0,,0.8011780824134743, 1960-12,432.0,,0.8988243899850112, |
monthly_mean.csv
Month,Passengers 1,241.75 2,235.0 3,270.1666666666667 4,267.0833333333333 5,271.8333333333333 6,311.6666666666667 7,351.3333333333333 8,351.0833333333333 9,302.4166666666667 10,266.5833333333333 11,232.83333333333334 12,261.8333333333333 |
Figures
Figure_1_raw_data_2020-06-06_085650.png
Figure_2_raw_data_trend_seasonality_residual_multiplicative_2020-06-06_085650.png
Figure_3_raw_data_monthly average_2020-06-06_085650.png
No comments:
Post a Comment