%3CLINGO-SUB%20id%3D%22lingo-sub-1091781%22%20slang%3D%22en-US%22%3ERe%3A%20Machine%20Learning%20for%20Sales%20Forecasting%3A%20A%20Capstone%20Project%20with%20Columbia%20University%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1091781%22%20slang%3D%22en-US%22%3E%3CP%3ENice%20Blogpost%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fcool_40x40.gif%22%20alt%3D%22%3Acool%3A%22%20title%3D%22%3Acool%3A%22%20%2F%3E%20I%20Shared%20it%20with%20Education%20Netherlands%20in%20Amsterdam.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1091578%22%20slang%3D%22en-US%22%3EMachine%20Learning%20for%20Sales%20Forecasting%3A%20A%20Capstone%20Project%20with%20Columbia%20University%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1091578%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThis%20past%20semester%20we%20have%20been%20collaborating%20on%20a%20machine%20learning%20%3CEM%3ECapstone%20Project%3C%2FEM%3E%20with%20%3CA%20href%3D%22https%3A%2F%2Fsps.columbia.edu%2Facademics%2Fmasters%2Fapplied-analytics%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EColumbia%20University%E2%80%99s%20Master%20of%20Science%20in%20Applied%20Analytics%3C%2FA%3E%3A%20capstone%20projects%20are%20applied%20and%20experimental%20projects%20where%20students%20take%20what%20they%20have%20learned%20throughout%20the%20course%20of%20their%20graduate%20program%20and%20apply%20it%20to%20examine%20a%20specific%20area%20of%20study.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3ECapstone%20projects%20are%20specifically%20designed%20to%20encourage%20students%20to%20think%20critically%2C%20solve%20challenging%20data%20science%20problems%2C%20and%20develop%20analytical%20skills.%20Two%20group%20of%20students%20built%20an%20end-to-end%20data%20science%20solution%20using%20Azure%20Machine%20Learning%20to%20accurately%20forecast%20sales.%20Azure%20Machine%20Learning%20is%20a%20cloud-based%20environment%20that%20you%20can%20use%20to%20train%2C%20deploy%2C%20automate%2C%20manage%2C%20and%20track%20ML%20models.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EAzure%20Machine%20Learning%20can%20be%20used%20for%20any%20kind%20of%20machine%20learning%2C%20from%20classical%20machine%20learning%20to%20deep%20learning%2C%20supervised%2C%20and%20unsupervised%20learning.%20Whether%20you%20prefer%20to%20write%20Python%20or%20R%20code%20or%20zero-code%2Flow-code%20options%20such%20as%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Ftutorial-designer-automobile-price-train-score%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edesigner%3C%2FA%3E%2C%20you%20can%20build%2C%20train%2C%20and%20track%20highly%20accurate%20machine%20learning%20and%20deep-learning%20models%20in%20an%20Azure%20Machine%20Learning%20Workspace.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3ETo%20explore%20the%20solution%20developed%20by%20students%20at%20Columbia%20University%2C%20you%20can%20look%20at%20their%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftl2896%2FTime-Series-Predictions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ETime-Series-Prediction%3C%2FA%3E%20repository%20on%20GitHub.%20In%20this%20article%2C%20we%20use%20an%20approach%20also%20used%20by%20Columbia%20University%20students%2C%20which%20is%20Automated%20Machine%20Learning%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fconcept-automated-ml%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAutomated%20ML%3C%2FA%3E%20or%20AutoML)%20to%20train%2C%20select%2C%20and%20operationalize%20a%20time-series%20forecasting%20model%20for%20multiple%20time-series.%20Make%20sure%20you%20have%20executed%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2FMachineLearningNotebooks%2Fblob%2Fmaster%2Fconfiguration.ipynb%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Econfiguration%20notebook%3C%2FA%3E%26nbsp%3Bbefore%20running%20this%20notebook.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EAutomated%20ML%20(as%20illustrated%20in%20Figure%201%20below)%20is%20the%20process%20of%20automating%20the%20time%20consuming%2C%20iterative%20tasks%20of%20machine%20learning%20model%20development.%20It%20allows%20data%20scientists%2C%20analysts%2C%20and%20developers%20to%20build%20ML%20models%20with%20high%20scale%2C%20efficiency%2C%20and%20productivity%20all%20while%20sustaining%20model%20quality%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20596px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F163852i73044B3E6F96D65F%2Fimage-dimensions%2F596x246%3Fv%3D1.0%22%20width%3D%22596%22%20height%3D%22246%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20left%3B%22%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EFigure%201%20-%20Automated%20ML%20process%20on%20Azure%20-%20Source%3A%20%3CA%20href%3D%22http%3A%2F%2Fwww.aka.ms%2FAutomatedMLDocs%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ewww.aka.ms%2FAutomatedMLDocs%3C%2FA%3E%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%20examples%20below%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.chicagobooth.edu%2Fresearch%2Fkilts%2Fdatasets%2Fdominicks%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDominick's%20Finer%20Foods%3C%2FA%3E%20data%20set%20from%20James%20M.%20Kilts%20Center%2C%20University%20of%20Chicago%20Booth%20School%20of%20Business%2C%20to%20forecast%20orange%20juice%20sales.%20In%20the%20rest%20of%20this%20article%20we%20will%20go%20through%20the%20following%20steps%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20654px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F163851iA7DD63DD2F28ED42%2Fimage-dimensions%2F654x619%3Fv%3D1.0%22%20width%3D%22654%22%20height%3D%22619%22%20alt%3D%22clipboard_image_1.png%22%20title%3D%22clipboard_image_1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1436749000%22%20id%3D%22toc-hId--1436749000%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1050763833%22%20id%3D%22toc-hId-1050763833%22%3E%3CSPAN%3E1.%20Azure%20ML%20set%20up%20and%20create%20a%20machine%20learning%20experiment%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20azureml.core%0Aimport%20pandas%20as%20pd%0Aimport%20numpy%20as%20np%0Aimport%20logging%0A%0Afrom%20azureml.core.workspace%20import%20Workspace%0Afrom%20azureml.core.experiment%20import%20Experiment%0Afrom%20azureml.train.automl%20import%20AutoMLConfig%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EAs%20part%20of%20the%20setup%20you%20have%20already%20created%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fhow-to-manage-workspace%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWorkspace%3C%2FA%3E.%20To%20run%20AutoML%2C%20you%20also%20need%20to%20create%20an%20Experiment.%20An%20Experiment%20corresponds%20to%20a%20prediction%20problem%20you%20are%20trying%20to%20solve%2C%20while%20a%20Run%20corresponds%20to%20a%20specific%20approach%20to%20the%20problem%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Ews%20%3D%20Workspace.from_config()%0A%0A%23%20choose%20a%20name%20for%20the%20run%20history%20container%20in%20the%20workspace%0Aexperiment_name%20%3D%20'automl-ojforecasting'%0Aexperiment%20%3D%20Experiment(ws%2C%20experiment_name)%0Aoutput%20%3D%20%7B%7D%0Aoutput%5B'SDK%20version'%5D%20%3D%20azureml.core.VERSION%0Aoutput%5B'Subscription%20ID'%5D%20%3D%20ws.subscription_id%0Aoutput%5B'Workspace'%5D%20%3D%20ws.name%0Aoutput%5B'SKU'%5D%20%3D%20ws.sku%0Aoutput%5B'Resource%20Group'%5D%20%3D%20ws.resource_group%0Aoutput%5B'Location'%5D%20%3D%20ws.location%0Aoutput%5B'Run%20History%20Name'%5D%20%3D%20experiment_name%0A%0Apd.set_option('display.max_colwidth'%2C%20-1)%0AoutputDf%20%3D%20pd.DataFrame(data%20%3D%20output%2C%20index%20%3D%20%5B''%5D)%0AoutputDf.T%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--756690630%22%20id%3D%22toc-hId--756690630%22%3E2.%20Create%20a%20compute%20target%3C%2FH2%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EYou%20will%20need%20to%20create%20a%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fhow-to-set-up-training-targets%23amlcomputehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fhow-to-manage-workspace%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecompute%20target%3C%2FA%3E%26nbsp%3Bfor%20your%20AutoML%20run.%20In%20this%20tutorial%2C%20you%20create%20AmlCompute%20as%20your%20training%20compute%20resource.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EIf%20the%20AmlCompute%20with%20that%20name%20is%20already%20in%20your%20workspace%20this%20code%20will%20skip%20the%20creation%20process.%20As%20with%20other%20Azure%20services%2C%20there%20are%20limits%20on%20certain%20resources%20(e.g.%20AmlCompute)%20associated%20with%20the%20Azure%20Machine%20Learning%20service%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Efrom%20azureml.core.compute%20import%20AmlCompute%0Afrom%20azureml.core.compute%20import%20ComputeTarget%0A%0A%23%20Choose%20a%20name%20for%20your%20cluster.%0Aamlcompute_cluster_name%20%3D%20%22cpu-cluster-oj%22%0Afound%20%3D%20False%0A%0A%23%20Check%20if%20this%20compute%20target%20already%20exists%20in%20the%20workspace.%0Acts%20%3D%20ws.compute_targets%0Aif%20amlcompute_cluster_name%20in%20cts%20and%20cts%5Bamlcompute_cluster_name%5D.type%20%3D%3D%20'AmlCompute'%3A%0A%20%20%20%20found%20%3D%20True%0A%20%20%20%20print('Found%20existing%20compute%20target.')%0A%20%20%20%20compute_target%20%3D%20cts%5Bamlcompute_cluster_name%5D%0A%0Aif%20not%20found%3A%0A%20%20%20%20print('Creating%20a%20new%20compute%20target...')%0A%20%20%20%20provisioning_config%20%3D%20AmlCompute.provisioning_configuration(vm_size%20%3D%20%22STANDARD_D2_V2%22%2C%20%23%20for%20GPU%2C%20use%20%22STANDARD_NC6%22%0A%23vm_priority%20%3D%20'lowpriority'%2C%20%23%20optional%20max_nodes%20%3D%206)%0A%0A%20%20%20%20%23%20Create%20the%20cluster%0A%20%20%20%20compute_target%20%3D%20ComputeTarget.create(ws%2C%20amlcompute_cluster_name%2C%20provisioning_config)%0A%0Aprint('Checking%20cluster%20status...')%0A%0A%23%20Can%20poll%20for%20a%20minimum%20number%20of%20nodes%20and%20for%20a%20specific%20timeout.%0A%23%20If%20no%20min_node_count%20is%20provided%2C%20it%20will%20use%20the%20scale%20settings%20for%20the%20cluster.%0A%0Acompute_target.wait_for_completion(show_output%20%3D%20True%2C%20min_node_count%20%3D%20None%2C%20timeout_in_minutes%20%3D%2020)%0A%0A%20%20%20%23%20For%20a%20more%20detailed%20view%20of%20current%20AmlCompute%20status%2C%20use%20get_status().%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1730822203%22%20id%3D%22toc-hId-1730822203%22%3E3.%20Loading%20and%20handling%20historical%20data%3C%2FH2%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EYou%20are%20now%20ready%20to%20load%20the%20historical%20orange%20juice%20sales%20data.%20We%20will%20load%20the%20CSV%20file%20into%20a%20plain%20pandas%20DataFrame%3B%20the%20time%20column%20in%20the%20CSV%20is%20called%26nbsp%3B%3CEM%3EWeekStarting%3C%2FEM%3E%2C%20so%20it%20will%20be%20specially%20parsed%20into%20the%20datetime%20type%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Etime_column_name%20%3D%20'WeekStarting'%0Adata%20%3D%20pd.read_csv(%22dominicks_OJ.csv%22%2C%20parse_dates%3D%5Btime_column_name%5D)%0Adata.head()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EEach%20row%20in%20the%20DataFrame%20holds%20a%20quantity%20of%20weekly%20sales%20for%20an%20OJ%20brand%20at%20a%20single%20store.%20The%20data%20also%20includes%20the%20sales%20price%2C%20a%20flag%20indicating%20if%20the%20OJ%20brand%20was%20advertised%20in%20the%20store%20that%20week%2C%20and%20some%20customer%20demographic%20information%20based%20on%20the%20store%20location.%20For%20historical%20reasons%2C%20the%20data%20also%20include%20the%20logarithm%20of%20the%20sales%20quantity.%20The%20Dominick's%20grocery%20data%20is%20commonly%20used%20to%20illustrate%20econometric%20modeling%20techniques%20where%20logarithms%20of%20quantities%20are%20generally%20preferred.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%20task%20is%20now%20to%20build%20a%20time-series%20model%20for%20the%26nbsp%3B%3CEM%3EQuantity%3C%2FEM%3E%26nbsp%3Bcolumn.%20It%20is%20important%20to%20note%20that%20this%20dataset%20is%20comprised%20of%20many%20individual%20time-series%20-%20one%20for%20each%20unique%20combination%20of%26nbsp%3B%3CEM%3EStore%3C%2FEM%3E%26nbsp%3Band%26nbsp%3B%3CEM%3EBrand%3C%2FEM%3E.%20To%20distinguish%20the%20individual%20time-series%2C%20we%20thus%20define%20the%26nbsp%3B%3CSTRONG%3Egrain%3C%2FSTRONG%3E%26nbsp%3B-%20the%20columns%20whose%20values%20determine%20the%20boundaries%20between%20time-series%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Egrain_column_names%20%3D%20%5B'Store'%2C%20'Brand'%5D%0Anseries%20%3D%20data.groupby(grain_column_names).ngroups%0Aprint('Data%20contains%20%7B0%7D%20individual%20time-series.'.format(nseries))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20demonstration%20purposes%2C%20we%20extract%20sales%20time-series%20for%20just%20a%20few%20of%20the%20stores%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Euse_stores%20%3D%20%5B2%2C%205%2C%208%5D%0Adata_subset%20%3D%20data%5Bdata.Store.isin(use_stores)%5D%0Anseries%20%3D%20data_subset.groupby(grain_column_names).ngroups%0Aprint('Data%20subset%20contains%20%7B0%7D%20individual%20time-series.'.format(nseries))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--76632260%22%20id%3D%22toc-hId--76632260%22%3E4.%20Splitting%20data%20and%20uploading%20it%20to%20data%20store%3C%2FH2%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EWe%20now%20split%20the%20data%20into%20a%20training%20and%20a%20testing%20set%20for%20later%20forecast%20evaluation.%20The%20test%20set%20will%20contain%20the%20final%2020%20weeks%20of%20observed%20sales%20for%20each%20time-series.%20The%20splits%20should%20be%20stratified%20by%20series%2C%20so%20we%20use%20a%20group-by%20statement%20on%20the%20grain%20columns%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3En_test_periods%20%3D%2020%0Adef%20split_last_n_by_grain(df%2C%20n)%3A%0A%0A%20%20%20%20%22%22%22Group%20df%20by%20grain%20and%20split%20on%20last%20n%20rows%20for%20each%20group.%22%22%22%0A%20%20%20%20df_grouped%20%3D%20(df.sort_values(time_column_name)%20%23%20Sort%20by%20ascending%20time%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.groupby(grain_column_names%2C%20group_keys%3DFalse))%0A%20%20%20%20df_head%20%3D%20df_grouped.apply(lambda%20dfg%3A%20dfg.iloc%5B%3A-n%5D)%0A%20%20%20%20df_tail%20%3D%20df_grouped.apply(lambda%20dfg%3A%20dfg.iloc%5B-n%3A%5D)%0A%20%20%20%20return%20df_head%2C%20df_tail%0A%0Atrain%2C%20test%20%3D%20split_last_n_by_grain(data_subset%2C%20n_test_periods)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fconcept-workspacehttps%3A%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fhow-to-manage-workspace%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMachine%20Learning%20service%20workspace%3C%2FA%3E%2C%20is%20paired%20with%20the%20storage%20account%2C%20which%20contains%20the%20default%20data%20store.%20We%20will%20use%20it%20to%20upload%20the%20train%20and%20test%20data%20and%20create%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpython%2Fapi%2Fazureml-core%2Fazureml.data.tabulardataset%3Fview%3Dazure-ml-pyhttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmachine-learning%2Fservice%2Fhow-to-manage-workspace%3FWT.mc_id%3Dtechcommunity-article-lazzeri%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etabular%20datasets%3C%2FA%3E%26nbsp%3Bfor%20training%20and%20testing%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Etrain.to_csv%20(r'.%2Fdominicks_OJ_train.csv'%2C%20index%20%3D%20None%2C%20header%3DTrue)%0Atest.to_csv%20(r'.%2Fdominicks_OJ_test.csv'%2C%20index%20%3D%20None%2C%20header%3DTrue)%20%0A%0Adatastore%20%3D%20ws.get_default_datastore()%0Adatastore.upload_files(files%20%3D%20%5B'.%2Fdominicks_OJ_train.csv'%2C%20'.%2Fdominicks_OJ_test.csv'%5D%2C%20target_path%20%3D%20'dataset%2F'%2C%20overwrite%20%3D%20True%2Cshow_progress%20%3D%20True)%0A%0A%23Let%E2%80%99s%20now%20create%20the%20dataset%20that%20we%20will%20use%20for%20our%20training%20part%3A%0Afrom%20azureml.core.dataset%20import%20Dataset%0Atrain_dataset%20%3D%20Dataset.Tabular.from_delimited_files(path%3Ddatastore.path('dataset%2Fdominicks_OJ_train.csv'))%0A%0Atrain_dataset.to_pandas_dataframe().tail()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1884086723%22%20id%3D%22toc-hId--1884086723%22%3E5.%20Modeling%20and%20training%3C%2FH2%3E%0A%3CP%3EFor%20forecasting%20tasks%2C%20AutoML%20uses%20pre-processing%20and%20estimation%20steps%20that%20are%20specific%20to%20time-series.%20Automated%20ML%20will%20undertake%20the%20following%20pre-processing%20steps%3A%3C%2FP%3E%0A%3CUL%20style%3D%22text-align%3A%20justify%3B%22%3E%0A%3CLI%3EDetect%20time-series%20sample%20frequency%20(e.g.%20hourly%2C%20daily%2C%20weekly)%20and%20create%20new%20records%20for%20absent%20time%20points%20to%20make%20the%20series%20regular.%20A%20regular%20time%20series%20has%20a%20well-defined%20frequency%20and%20has%20a%20value%20at%20every%20sample%20point%20in%20a%20contiguous%20time%20span%3C%2FLI%3E%0A%3CLI%3EImpute%20missing%20values%20in%20the%20target%20(via%20forward-fill)%20and%20feature%20columns%20(using%20median%20column%20values)%3C%2FLI%3E%0A%3CLI%3ECreate%20grain-based%20features%20to%20enable%20fixed%20effects%20across%20different%20series%3C%2FLI%3E%0A%3CLI%3ECreate%20time-based%20features%20to%20assist%20in%20learning%20seasonal%20patterns%3C%2FLI%3E%0A%3CLI%3EEncode%20categorical%20variables%20to%20numeric%20quantities%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EIn%20this%20notebook%2C%20AutoML%20will%20train%20a%20single%2C%20regression-type%20model%20across%26nbsp%3Ball%26nbsp%3Btime-series%20in%20a%20given%20training%20set.%20This%20allows%20the%20model%20to%20generalize%20across%20related%20series.%20If%20you're%20looking%20for%20training%20multiple%20models%20for%20different%20time-series%2C%20please%20check%20out%20the%20forecasting%20grouping%20notebook.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EYou%20are%20almost%20ready%20to%20start%20an%20AutoML%20training%20job.%20First%2C%20we%20need%20to%20separate%20the%20target%20column%20from%20the%20rest%20of%20the%20DataFrame%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Etarget_column_name%20%3D%20'Quantity'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%20AutoMLConfig%20object%20defines%20the%20settings%20and%20data%20for%20an%20AutoML%20training%20job.%20Here%2C%20we%20set%20necessary%20inputs%20like%20the%20task%20type%2C%20the%20number%20of%20AutoML%20iterations%20to%20try%2C%20the%20training%20data%2C%20and%20cross-validation%20parameters.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EFor%20forecasting%20tasks%2C%20there%20are%20some%20additional%20parameters%20that%20can%20be%20set%3A%20the%20name%20of%20the%20column%20holding%20the%20date%2Ftime%2C%20the%20grain%20column%20names%2C%20and%20the%20maximum%20forecast%20horizon.%20A%20time%20column%20is%20required%20for%20forecasting%2C%20while%20the%20grain%20is%20optional.%20If%20a%20grain%20is%20not%20given%2C%20AutoML%20assumes%20that%20the%20whole%20data%20set%20is%20a%20single%20time-series.%20We%20also%20pass%20a%20list%20of%20columns%20to%20drop%20prior%20to%20modeling.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%26nbsp%3B%3CEM%3ElogQuantity%3C%2FEM%3E%26nbsp%3Bcolumn%20is%20completely%20correlated%20with%20the%20target%20quantity%2C%20so%20it%20must%20be%20removed%20to%20prevent%20a%20target%20leak.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%20forecast%20horizon%20is%20given%20in%20units%20of%20the%20time-series%20frequency%3B%20for%20instance%2C%20the%20OJ%20series%20frequency%20is%20weekly%2C%20so%20a%20horizon%20of%2020%20means%20that%20a%20trained%20model%20will%20estimate%20sales%20up%20to%2020%20weeks%20beyond%20the%20latest%20date%20in%20the%20training%20data%20for%20each%20series.%20In%20this%20example%2C%20we%20set%20the%20maximum%20horizon%20to%20the%20number%20of%20samples%20per%20series%20in%20the%20test%20set%20(n_test_periods).%20Generally%2C%20the%20value%20of%20this%20parameter%20will%20be%20dictated%20by%20business%20needs.%20For%20example%2C%20a%20demand%20planning%20organization%20that%20needs%20to%20estimate%20the%20next%20month%20of%20sales%20would%20set%20the%20horizon%20accordingly.%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EFinally%2C%20a%20note%20about%20the%20cross-validation%20(CV)%20procedure%20for%20time-series%20data.%20AutoML%20uses%20out-of-sample%20error%20estimates%20to%20select%20a%20best%20pipeline%2Fmodel%2C%20so%20it%20is%20important%20that%20the%20CV%20fold%20splitting%20is%20done%20correctly.%20Time-series%20can%20violate%20the%20basic%20statistical%20assumptions%20of%20the%20canonical%20K-Fold%20CV%20strategy%2C%20so%20AutoML%20implements%20a%26nbsp%3Bspecific%20procedure%20to%20create%20CV%20folds%20for%20time-series%20data.%20To%20use%20this%20procedure%2C%20you%20just%20need%20to%20specify%20the%20desired%20number%20of%20CV%20folds%20in%20the%20AutoMLConfig%20object.%20It%20is%20also%20possible%20to%20bypass%20CV%20and%20use%20your%20own%20validation%20set%20by%20setting%20the%26nbsp%3Bvalidation_data%26nbsp%3Bparameter%20of%20AutoMLConfig%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Etime_series_settings%20%3D%20%7B%0A%20%20%20%20'time_column_name'%3A%20time_column_name%2C%0A%20%20%20%20'grain_column_names'%3A%20grain_column_names%2C%0A%20%20%20%20'drop_column_names'%3A%20%5B'logQuantity'%5D%2C%20%20%23%20'logQuantity'%20is%20a%20leaky%20feature%2C%20so%20we%20remove%20it.%0A%20%20%20%20'max_horizon'%3A%20n_test_periods%0A%7D%0A%0Aautoml_config%20%3D%20AutoMLConfig(task%3D'forecasting'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20debug_log%3D'automl_oj_sales_errors.log'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20primary_metric%3D'normalized_mean_absolute_error'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20experiment_timeout_minutes%3D15%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20training_data%3Dtrain_dataset%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label_column_name%3Dtarget_column_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20compute_target%3Dcompute_target%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20enable_early_stopping%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n_cross_validations%3D3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20verbosity%3Dlogging.INFO%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20**time_series_settings)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EYou%20can%20now%20submit%20a%20new%20training%20run.%20Depending%20on%20the%20data%20and%20number%20of%20iterations%20this%20operation%20may%20take%20several%20minutes.%20Information%20from%20each%20iteration%20will%20be%20printed%20to%20the%20console%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eremote_run%20%3D%20experiment.submit(automl_config%2C%20show_output%3DFalse)%0Aremote_run%0Aremote_run.wait_for_completion()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-603426110%22%20id%3D%22toc-hId-603426110%22%3E6.%20Retrieve%20the%20best%20model%20and%20forecasting%3C%2FH2%3E%0A%3CP%3EEach%20run%20within%20an%20Experiment%20stores%20serialized%20(i.e.%20pickled)%20pipelines%20from%20the%20AutoML%20iterations.%20We%20can%20now%20retrieve%20the%20pipeline%20with%20the%20best%20performance%20on%20the%20validation%20data%20set%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Ebest_run%2C%20fitted_model%20%3D%20remote_run.get_output()%0Aprint(fitted_model.steps)%0Amodel_name%20%3D%20best_run.properties%5B'model_name'%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3ENow%20that%20we%20have%20retrieved%20the%20best%20pipeline%2Fmodel%2C%20it%20can%20be%20used%20to%20make%20predictions%20on%20test%20data.%20First%2C%20we%20remove%20the%20target%20values%20from%20the%20test%20set%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EX_test%20%3D%20test%0Ay_test%20%3D%20X_test.pop(target_column_name).values%0AX_test.head()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3ETo%20produce%20predictions%20on%20the%20test%20set%2C%20we%20need%20to%20know%20the%20feature%20values%20at%20all%20dates%20in%20the%20test%20set.%20This%20requirement%20is%20somewhat%20reasonable%20for%20the%20OJ%20sales%20data%20since%20the%20features%20mainly%20consist%20of%20price%2C%20which%20is%20usually%20set%20in%20advance%2C%20and%20customer%20demographics%20which%20are%20approximately%20constant%20for%20each%20store%20over%20the%2020%20week%20forecast%20horizon%20in%20the%20testing%20data%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20The%20featurized%20data%2C%20aligned%20to%20y%2C%20will%20also%20be%20returned.%0A%23%20This%20contains%20the%20assumptions%20that%20were%20made%20in%20the%20forecast%0A%23%20and%20helps%20align%20the%20forecast%20to%20the%20original%20data%0Ay_predictions%2C%20X_trans%20%3D%20fitted_model.forecast(X_test)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EIf%20you%20are%20used%20to%20scikit%20pipelines%2C%20perhaps%20you%20expected%26nbsp%3Bpredict(X_test).%20However%2C%20forecasting%20requires%20a%20more%20general%20interface%20that%20also%20supplies%20the%20past%20target%26nbsp%3By%26nbsp%3Bvalues.%20Please%20use%26nbsp%3Bforecast(X%2Cy)%26nbsp%3Bas%26nbsp%3Bpredict(X)%26nbsp%3Bis%20reserved%20for%20internal%20purposes%20on%20forecasting%20models.%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1204028353%22%20id%3D%22toc-hId--1204028353%22%3E7.%20Evaluate%20the%20model%3C%2FH2%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3ETo%20evaluate%20the%20accuracy%20of%20the%20forecast%2C%20we'll%20compare%20against%20the%20actual%20sales%20quantities%20for%20some%20select%20metrics%2C%20included%20the%20mean%20absolute%20percentage%20error%20(MAPE).%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EIt%20is%20a%20good%20practice%20to%20always%20align%20the%20output%20explicitly%20to%20the%20input%2C%20as%20the%20count%20and%20order%20of%20the%20rows%20may%20have%20changed%20during%20transformations%20that%20span%20multiple%20rows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Efrom%20forecasting_helper%20import%20align_outputs%0Adf_all%20%3D%20align_outputs(y_predictions%2C%20X_trans%2C%20X_test%2C%20y_test%2C%20target_column_name)%0A%0Afrom%20azureml.automl.core._vendor.automl.client.core.common%20import%20metrics%0Afrom%20matplotlib%20import%20pyplot%20as%20plt%0Afrom%20automl.client.core.common%20import%20constants%0A%0A%23%20use%20automl%20metrics%20module%0Ascores%20%3D%20metrics.compute_metrics_regression(%0A%20%20%20%20df_all%5B'predicted'%5D%2C%0A%20%20%20%20df_all%5Btarget_column_name%5D%2C%0A%20%20%20%20list(constants.Metric.SCALAR_REGRESSION_SET)%2C%0A%20%20%20%20None%2C%20None%2C%20None)%0A%0Aprint(%22%5BTest%20data%20scores%5D%5Cn%22)%0Afor%20key%2C%20value%20in%20scores.items()%3A%20%20%20%0A%20%20%20%20print('%7B%7D%3A%20%20%20%7B%3A.3f%7D'.format(key%2C%20value))%0A%0A%23%20Plot%20outputs%0A%25matplotlib%20inline%0Atest_pred%20%3D%20plt.scatter(df_all%5Btarget_column_name%5D%2C%20df_all%5B'predicted'%5D%2C%20color%3D'b')%0Atest_test%20%3D%20plt.scatter(df_all%5Btarget_column_name%5D%2C%20df_all%5Btarget_column_name%5D%2C%20color%3D'g')%0Aplt.legend((test_pred%2C%20test_test)%2C%20('prediction'%2C%20'truth')%2C%20loc%3D'upper%20left'%2C%20fontsize%3D8)%0Aplt.show()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1283484480%22%20id%3D%22toc-hId-1283484480%22%3E8.%20Operationalization%3A%20deploy%20the%20model%20as%20a%20Web%20Service%20on%20Azure%20Container%20Instance%3C%2FH2%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%3CEM%3EOperationalization%3C%2FEM%3E%26nbsp%3Bmeans%20getting%20the%20model%20into%20the%20cloud%20so%20that%20other%20can%20run%20it%20after%20you%20close%20the%20notebook.%20We%20will%20create%20a%20docker%20running%20on%20Azure%20Container%20Instances%20with%20the%20model%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Edescription%20%3D%20'AutoML%20OJ%20forecaster'%0Atags%20%3D%20None%0Amodel%20%3D%20remote_run.register_model(model_name%20%3D%20model_name%2C%20description%20%3D%20description%2C%20tags%20%3D%20tags)%0Aprint(remote_run.model_id)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EFor%20the%20deployment%20we%20need%20a%20function%20which%20will%20run%20the%20forecast%20on%20serialized%20data.%20It%20can%20be%20obtained%20from%20the%20best_run%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Escript_file_name%20%3D%20'score_fcast.py'%0Abest_run.download_file('outputs%2Fscoring_file_v_1_0_0.py'%2C%20script_file_name)%0A%0Afrom%20azureml.core.model%20import%20InferenceConfig%0Afrom%20azureml.core.webservice%20import%20AciWebservice%0Afrom%20azureml.core.webservice%20import%20Webservice%0Afrom%20azureml.core.model%20import%20Model%0A%0Ainference_config%20%3D%20InferenceConfig(environment%20%3D%20best_run.get_environment()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20entry_script%20%3D%20script_file_name)%0Aaciconfig%20%3D%20AciWebservice.deploy_configuration(cpu_cores%20%3D%201%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20memory_gb%20%3D%202%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tags%20%3D%20%7B'type'%3A%20%22automl-forecasting%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20description%20%3D%20%22Automl%20forecasting%20sample%20service%22)%0Aaci_service_name%20%3D%20'automl-oj-forecast-01'%0Aprint(aci_service_name)%0Aaci_service%20%3D%20Model.deploy(ws%2C%20aci_service_name%2C%20%5Bmodel%5D%2C%20inference_config%2C%20aciconfig)%0A%0Aaci_service.wait_for_deployment(True)%0Aprint(aci_service.state)%0Aaci_service.get_logs()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--523969983%22%20id%3D%22toc-hId--523969983%22%3E9.%20Call%20the%20service%20and%20consume%20the%20model%3C%2FH2%3E%0A%3CP%3EFinally%2C%20in%20order%20to%20call%20the%20service%20and%20consume%20your%20machine%20learning%20model%2C%20you%20can%20run%20the%20following%20script%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20json%0A%23%20The%20request%20data%20frame%20needs%20to%20have%20y_query%20column%20which%20corresponds%20to%20query.%0AX_query%20%3D%20X_test.copy()%0AX_query%5B'y_query'%5D%20%3D%20np.NaN%0A%0A%23%20We%20have%20to%20convert%20datetime%20to%20string%2C%20because%20Timestamps%20cannot%20be%20serialized%20to%20JSON.%0AX_query%5Btime_column_name%5D%20%3D%20X_query%5Btime_column_name%5D.astype(str)%0A%0A%23%20The%20Service%20object%20accept%20the%20complex%20dictionary%2C%20which%20is%20internally%20converted%20to%20JSON%20string.%0A%23%20The%20section%20'data'%20contains%20the%20data%20frame%20in%20the%20form%20of%20dictionary.%0Atest_sample%20%3D%20json.dumps(%7B'data'%3A%20X_query.to_dict(orient%3D'records')%7D)%0Aresponse%20%3D%20aci_service.run(input_data%20%3D%20test_sample)%0A%0A%23%20translate%20from%20networkese%20to%20datascientese%0Atry%3A%0A%20%20%20%20res_dict%20%3D%20json.loads(response)%0A%20%20%20%20y_fcst_all%20%3D%20pd.DataFrame(res_dict%5B'index'%5D)%0A%20%20%20%20y_fcst_all%5Btime_column_name%5D%20%3D%20pd.to_datetime(y_fcst_all%5Btime_column_name%5D%2C%20unit%20%3D%20'ms')%0A%20%20%20%20y_fcst_all%5B'forecast'%5D%20%3D%20res_dict%5B'forecast'%5D%20%20%20%0A%0Aexcept%3A%0A%20%20%20%20print(res_dict)%0A%0Ay_fcst_all.head()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--657654707%22%20id%3D%22toc-hId--657654707%22%3E10.%20Final%20resources%20to%20learn%20more%3C%2FH2%3E%0A%3CP%3ETo%20learn%20more%2C%20you%20can%20read%20the%20following%20articles%20and%20notebooks%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EAzure%20Machine%20Learning%20Notebooks%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAzureMLServiceGithub%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eaka.ms%2FAzureMLServiceGithub%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EAzure%20Machine%20Learning%20Service%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAzureMLservice%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eaka.ms%2FAzureMLservice%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EGet%20started%20with%20Azure%20ML%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FGetStartedAzureML%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eaka.ms%2FGetStartedAzureML%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EAutomated%20Machine%20Learning%20Documentation%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAutomatedMLDocs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2FAutomatedMLDocs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EWhat%20is%20Automated%20Machine%20Learning%3F%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAutomatedML%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2FAutomatedML%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EModel%20Interpretability%20with%20Azure%20ML%20Service%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAutomatedML%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2F%3C%2FA%3E%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAzureMLModelInterpretability%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzureMLModelInterpretability%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EAzure%20Notebooks%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAzureNB%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2FAzureNB%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EPython%20Microsoft%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FPythonMS%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2FPythonMS%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EAzure%20ML%20for%20VS%20Code%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FAzureMLforVSCode%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eaka.ms%2FAzureMLforVSCode%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1091578%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20past%20semester%20we%20have%20been%20collaborating%20on%20a%20machine%20learning%20%3CEM%3ECapstone%20Project%3C%2FEM%3E%20with%20%3CA%20href%3D%22https%3A%2F%2Fsps.columbia.edu%2Facademics%2Fmasters%2Fapplied-analytics%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EColumbia%20University%E2%80%99s%20Master%20of%20Science%20in%20Applied%20Analytics%3C%2FA%3E%3A%20capstone%20projects%20are%20applied%20and%20experimental%20projects%20where%20students%20take%20what%20they%20have%20learned%20throughout%20the%20course%20of%20their%20graduate%20program%20and%20apply%20it%20to%20examine%20a%20specific%20area%20of%20study.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1091578%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAutomated%20Machine%20Learning%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAutoML%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EColumbia%20University%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMachine%20Learning%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETime%20Series%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E

This past semester we have been collaborating on a machine learning Capstone Project with Columbia University’s Master of Science in Applied Analytics: capstone projects are applied and experimental projects where students take what they have learned throughout the course of their graduate program and apply it to examine a specific area of study.

 

Capstone projects are specifically designed to encourage students to think critically, solve challenging data science problems, and develop analytical skills. Two group of students built an end-to-end data science solution using Azure Machine Learning to accurately forecast sales. Azure Machine Learning is a cloud-based environment that you can use to train, deploy, automate, manage, and track ML models.

 

Azure Machine Learning can be used for any kind of machine learning, from classical machine learning to deep learning, supervised, and unsupervised learning. Whether you prefer to write Python or R code or zero-code/low-code options such as the designer, you can build, train, and track highly accurate machine learning and deep-learning models in an Azure Machine Learning Workspace.

 

To explore the solution developed by students at Columbia University, you can look at their Time-Series-Prediction repository on GitHub. In this article, we use an approach also used by Columbia University students, which is Automated Machine Learning (Automated ML or AutoML) to train, select, and operationalize a time-series forecasting model for multiple time-series. Make sure you have executed the configuration notebook before running this notebook.

 

Automated ML (as illustrated in Figure 1 below) is the process of automating the time consuming, iterative tasks of machine learning model development. It allows data scientists, analysts, and developers to build ML models with high scale, efficiency, and productivity all while sustaining model quality:

clipboard_image_0.png

Figure 1 - Automated ML process on Azure - Source: www.aka.ms/AutomatedMLDocs

 

The examples below use the Dominick's Finer Foods data set from James M. Kilts Center, University of Chicago Booth School of Business, to forecast orange juice sales. In the rest of this article we will go through the following steps:

 

clipboard_image_1.png

 

1. Azure ML set up and create a machine learning experiment

 

 

import azureml.core
import pandas as pd
import numpy as np
import logging

from azureml.core.workspace import Workspace
from azureml.core.experiment import Experiment
from azureml.train.automl import AutoMLConfig

 

 

 

As part of the setup you have already created a Workspace. To run AutoML, you also need to create an Experiment. An Experiment corresponds to a prediction problem you are trying to solve, while a Run corresponds to a specific approach to the problem:

 

 

 

ws = Workspace.from_config()

# choose a name for the run history container in the workspace
experiment_name = 'automl-ojforecasting'
experiment = Experiment(ws, experiment_name)
output = {}
output['SDK version'] = azureml.core.VERSION
output['Subscription ID'] = ws.subscription_id
output['Workspace'] = ws.name
output['SKU'] = ws.sku
output['Resource Group'] = ws.resource_group
output['Location'] = ws.location
output['Run History Name'] = experiment_name

pd.set_option('display.max_colwidth', -1)
outputDf = pd.DataFrame(data = output, index = [''])
outputDf.T

 

 

2. Create a compute target

You will need to create a compute target for your AutoML run. In this tutorial, you create AmlCompute as your training compute resource.

If the AmlCompute with that name is already in your workspace this code will skip the creation process. As with other Azure services, there are limits on certain resources (e.g. AmlCompute) associated with the Azure Machine Learning service:

 

 

from azureml.core.compute import AmlCompute
from azureml.core.compute import ComputeTarget

# Choose a name for your cluster.
amlcompute_cluster_name = "cpu-cluster-oj"
found = False

# Check if this compute target already exists in the workspace.
cts = ws.compute_targets
if amlcompute_cluster_name in cts and cts[amlcompute_cluster_name].type == 'AmlCompute':
    found = True
    print('Found existing compute target.')
    compute_target = cts[amlcompute_cluster_name]

if not found:
    print('Creating a new compute target...')
    provisioning_config = AmlCompute.provisioning_configuration(vm_size = "STANDARD_D2_V2", # for GPU, use "STANDARD_NC6"
#vm_priority = 'lowpriority', # optional max_nodes = 6)

    # Create the cluster
    compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, provisioning_config)

print('Checking cluster status...')

# Can poll for a minimum number of nodes and for a specific timeout.
# If no min_node_count is provided, it will use the scale settings for the cluster.

compute_target.wait_for_completion(show_output = True, min_node_count = None, timeout_in_minutes = 20)

   # For a more detailed view of current AmlCompute status, use get_status().

 

 

3. Loading and handling historical data

You are now ready to load the historical orange juice sales data. We will load the CSV file into a plain pandas DataFrame; the time column in the CSV is called WeekStarting, so it will be specially parsed into the datetime type:

 

 

time_column_name = 'WeekStarting'
data = pd.read_csv("dominicks_OJ.csv", parse_dates=[time_column_name])
data.head()

 

 

Each row in the DataFrame holds a quantity of weekly sales for an OJ brand at a single store. The data also includes the sales price, a flag indicating if the OJ brand was advertised in the store that week, and some customer demographic information based on the store location. For historical reasons, the data also include the logarithm of the sales quantity. The Dominick's grocery data is commonly used to illustrate econometric modeling techniques where logarithms of quantities are generally preferred.

The task is now to build a time-series model for the Quantity column. It is important to note that this dataset is comprised of many individual time-series - one for each unique combination of Store and Brand. To distinguish the individual time-series, we thus define the grain - the columns whose values determine the boundaries between time-series:

 

 

grain_column_names = ['Store', 'Brand']
nseries = data.groupby(grain_column_names).ngroups
print('Data contains {0} individual time-series.'.format(nseries))

 

 

For demonstration purposes, we extract sales time-series for just a few of the stores:

 

 

use_stores = [2, 5, 8]
data_subset = data[data.Store.isin(use_stores)]
nseries = data_subset.groupby(grain_column_names).ngroups
print('Data subset contains {0} individual time-series.'.format(nseries))

 

 

4. Splitting data and uploading it to data store

We now split the data into a training and a testing set for later forecast evaluation. The test set will contain the final 20 weeks of observed sales for each time-series. The splits should be stratified by series, so we use a group-by statement on the grain columns:

 

 

n_test_periods = 20
def split_last_n_by_grain(df, n):

    """Group df by grain and split on last n rows for each group."""
    df_grouped = (df.sort_values(time_column_name) # Sort by ascending time
                  .groupby(grain_column_names, group_keys=False))
    df_head = df_grouped.apply(lambda dfg: dfg.iloc[:-n])
    df_tail = df_grouped.apply(lambda dfg: dfg.iloc[-n:])
    return df_head, df_tail

train, test = split_last_n_by_grain(data_subset, n_test_periods)

 

 

The Machine Learning service workspace, is paired with the storage account, which contains the default data store. We will use it to upload the train and test data and create tabular datasets for training and testing:

 

 

train.to_csv (r'./dominicks_OJ_train.csv', index = None, header=True)
test.to_csv (r'./dominicks_OJ_test.csv', index = None, header=True) 

datastore = ws.get_default_datastore()
datastore.upload_files(files = ['./dominicks_OJ_train.csv', './dominicks_OJ_test.csv'], target_path = 'dataset/', overwrite = True,show_progress = True)

#Let’s now create the dataset that we will use for our training part:
from azureml.core.dataset import Dataset
train_dataset = Dataset.Tabular.from_delimited_files(path=datastore.path('dataset/dominicks_OJ_train.csv'))

train_dataset.to_pandas_dataframe().tail()

 

 

5. Modeling and training

For forecasting tasks, AutoML uses pre-processing and estimation steps that are specific to time-series. Automated ML will undertake the following pre-processing steps:

  • Detect time-series sample frequency (e.g. hourly, daily, weekly) and create new records for absent time points to make the series regular. A regular time series has a well-defined frequency and has a value at every sample point in a contiguous time span
  • Impute missing values in the target (via forward-fill) and feature columns (using median column values)
  • Create grain-based features to enable fixed effects across different series
  • Create time-based features to assist in learning seasonal patterns
  • Encode categorical variables to numeric quantities

In this notebook, AutoML will train a single, regression-type model across all time-series in a given training set. This allows the model to generalize across related series. If you're looking for training multiple models for different time-series, please check out the forecasting grouping notebook.

You are almost ready to start an AutoML training job. First, we need to separate the target column from the rest of the DataFrame:

 

 

target_column_name = 'Quantity'

 

 

The AutoMLConfig object defines the settings and data for an AutoML training job. Here, we set necessary inputs like the task type, the number of AutoML iterations to try, the training data, and cross-validation parameters.

For forecasting tasks, there are some additional parameters that can be set: the name of the column holding the date/time, the grain column names, and the maximum forecast horizon. A time column is required for forecasting, while the grain is optional. If a grain is not given, AutoML assumes that the whole data set is a single time-series. We also pass a list of columns to drop prior to modeling.

The logQuantity column is completely correlated with the target quantity, so it must be removed to prevent a target leak.

The forecast horizon is given in units of the time-series frequency; for instance, the OJ series frequency is weekly, so a horizon of 20 means that a trained model will estimate sales up to 20 weeks beyond the latest date in the training data for each series. In this example, we set the maximum horizon to the number of samples per series in the test set (n_test_periods). Generally, the value of this parameter will be dictated by business needs. For example, a demand planning organization that needs to estimate the next month of sales would set the horizon accordingly. 

Finally, a note about the cross-validation (CV) procedure for time-series data. AutoML uses out-of-sample error estimates to select a best pipeline/model, so it is important that the CV fold splitting is done correctly. Time-series can violate the basic statistical assumptions of the canonical K-Fold CV strategy, so AutoML implements a specific procedure to create CV folds for time-series data. To use this procedure, you just need to specify the desired number of CV folds in the AutoMLConfig object. It is also possible to bypass CV and use your own validation set by setting the validation_data parameter of AutoMLConfig:

 

 

time_series_settings = {
    'time_column_name': time_column_name,
    'grain_column_names': grain_column_names,
    'drop_column_names': ['logQuantity'],  # 'logQuantity' is a leaky feature, so we remove it.
    'max_horizon': n_test_periods
}

automl_config = AutoMLConfig(task='forecasting',
                             debug_log='automl_oj_sales_errors.log',
                             primary_metric='normalized_mean_absolute_error',
                             experiment_timeout_minutes=15,
                             training_data=train_dataset,
                             label_column_name=target_column_name,
                             compute_target=compute_target,
                             enable_early_stopping=True,
                             n_cross_validations=3,
                             verbosity=logging.INFO,
                             **time_series_settings)

 

 

You can now submit a new training run. Depending on the data and number of iterations this operation may take several minutes. Information from each iteration will be printed to the console:

 

 

remote_run = experiment.submit(automl_config, show_output=False)
remote_run
remote_run.wait_for_completion()

 

 

6. Retrieve the best model and forecasting

Each run within an Experiment stores serialized (i.e. pickled) pipelines from the AutoML iterations. We can now retrieve the pipeline with the best performance on the validation data set:

 

 

best_run, fitted_model = remote_run.get_output()
print(fitted_model.steps)
model_name = best_run.properties['model_name']

 

 

Now that we have retrieved the best pipeline/model, it can be used to make predictions on test data. First, we remove the target values from the test set:

 

 

X_test = test
y_test = X_test.pop(target_column_name).values
X_test.head()

 

 

To produce predictions on the test set, we need to know the feature values at all dates in the test set. This requirement is somewhat reasonable for the OJ sales data since the features mainly consist of price, which is usually set in advance, and customer demographics which are approximately constant for each store over the 20 week forecast horizon in the testing data:

 

 

# The featurized data, aligned to y, will also be returned.
# This contains the assumptions that were made in the forecast
# and helps align the forecast to the original data
y_predictions, X_trans = fitted_model.forecast(X_test)

 

 

If you are used to scikit pipelines, perhaps you expected predict(X_test). However, forecasting requires a more general interface that also supplies the past target y values. Please use forecast(X,y) as predict(X) is reserved for internal purposes on forecasting models. 

 

7. Evaluate the model

To evaluate the accuracy of the forecast, we'll compare against the actual sales quantities for some select metrics, included the mean absolute percentage error (MAPE).

It is a good practice to always align the output explicitly to the input, as the count and order of the rows may have changed during transformations that span multiple rows:

 

 

from forecasting_helper import align_outputs
df_all = align_outputs(y_predictions, X_trans, X_test, y_test, target_column_name)

from azureml.automl.core._vendor.automl.client.core.common import metrics
from matplotlib import pyplot as plt
from automl.client.core.common import constants

# use automl metrics module
scores = metrics.compute_metrics_regression(
    df_all['predicted'],
    df_all[target_column_name],
    list(constants.Metric.SCALAR_REGRESSION_SET),
    None, None, None)

print("[Test data scores]\n")
for key, value in scores.items():   
    print('{}:   {:.3f}'.format(key, value))

# Plot outputs
%matplotlib inline
test_pred = plt.scatter(df_all[target_column_name], df_all['predicted'], color='b')
test_test = plt.scatter(df_all[target_column_name], df_all[target_column_name], color='g')
plt.legend((test_pred, test_test), ('prediction', 'truth'), loc='upper left', fontsize=8)
plt.show()

 

 

8. Operationalization: deploy the model as a Web Service on Azure Container Instance

Operationalization means getting the model into the cloud so that other can run it after you close the notebook. We will create a docker running on Azure Container Instances with the model:

 

 

description = 'AutoML OJ forecaster'
tags = None
model = remote_run.register_model(model_name = model_name, description = description, tags = tags)
print(remote_run.model_id)

 

 

For the deployment we need a function which will run the forecast on serialized data. It can be obtained from the best_run:

 

 

script_file_name = 'score_fcast.py'
best_run.download_file('outputs/scoring_file_v_1_0_0.py', script_file_name)

from azureml.core.model import InferenceConfig
from azureml.core.webservice import AciWebservice
from azureml.core.webservice import Webservice
from azureml.core.model import Model

inference_config = InferenceConfig(environment = best_run.get_environment(),
                                   entry_script = script_file_name)
aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1,                                                memory_gb = 2,
                                               tags = {'type': "automl-forecasting"},
                                               description = "Automl forecasting sample service")
aci_service_name = 'automl-oj-forecast-01'
print(aci_service_name)
aci_service = Model.deploy(ws, aci_service_name, [model], inference_config, aciconfig)

aci_service.wait_for_deployment(True)
print(aci_service.state)
aci_service.get_logs()

 

 

9. Call the service and consume the model

Finally, in order to call the service and consume your machine learning model, you can run the following script:

 

 

import json
# The request data frame needs to have y_query column which corresponds to query.
X_query = X_test.copy()
X_query['y_query'] = np.NaN

# We have to convert datetime to string, because Timestamps cannot be serialized to JSON.
X_query[time_column_name] = X_query[time_column_name].astype(str)

# The Service object accept the complex dictionary, which is internally converted to JSON string.
# The section 'data' contains the data frame in the form of dictionary.
test_sample = json.dumps({'data': X_query.to_dict(orient='records')})
response = aci_service.run(input_data = test_sample)

# translate from networkese to datascientese
try:
    res_dict = json.loads(response)
    y_fcst_all = pd.DataFrame(res_dict['index'])
    y_fcst_all[time_column_name] = pd.to_datetime(y_fcst_all[time_column_name], unit = 'ms')
    y_fcst_all['forecast'] = res_dict['forecast']   

except:
    print(res_dict)

y_fcst_all.head()

 

 

10. Final resources to learn more

To learn more, you can read the following articles and notebooks:

1 Comment

Nice Blogpost :cool: I Shared it with Education Netherlands in Amsterdam.