{"id":16679,"date":"2021-09-22T21:04:50","date_gmt":"2021-09-22T15:34:50","guid":{"rendered":"http:\/\/ismiletechnologies.com\/?p=16679"},"modified":"2023-02-24T13:42:24","modified_gmt":"2023-02-24T08:12:24","slug":"how-can-a-forecast-time-series-be-more-efficient-than-predict-linear-regression","status":"publish","type":"post","link":"https:\/\/ismiletechnologies.com\/en_us\/technology\/how-can-a-forecast-time-series-be-more-efficient-than-predict-linear-regression\/","title":{"rendered":"How can a Forecast Time Series be more efficient than Predict Linear Regression?"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">By,\u00a0<strong><em>Ahmed Nabil Atwa<\/em><\/strong>, Data Scientist | Machine Learning | Deep learning Researcher<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"Definition-of-Time-Series\">Definition of Time Series<\/h2>\r\n\r\n\r\n\r\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\r\n<p>Time series is an ordered sequence of\u00a0values of variables at equally spaced time intervals.\u00a0<\/p>\r\n<\/blockquote>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">To understand time series, we shall start with understanding the difference between prediction and forecasting.<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Prediction is a definitive and specific statement that gives you a future perspective depending on a given dataset.\r\n<ul>\r\n<li>For example, the share of this stock will increase in September 2021.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li>Forecasting is a probabilistic statement, over a specific time scale.\r\n<ul>\r\n<li>For example, the share of this stock will increase by 30% over the next couple of days.<img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/alan-turing-institute\/sktime\/main\/examples\/img\/forecasting.png\" width=\"900px\" \/><\/li>\r\n<\/ul>\r\n<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">So, from what we understand, we can see that time series depends on the\u00a0<strong>probability of the observations<\/strong>. Time Series may typically be\u00a0hourly, daily, weekly, monthly, quarterly and annual.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">But, linear regression depends on\u00a0<strong>past values<\/strong>\u00a0to estimate the\u00a0predicted values, whether it is a future, current or past.<\/p>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"So,-How-to-work-on-time-series-forecasting?\">So, How to work on time series forecasting?<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">There&#8217;s a library called\u00a0<code>sktime<\/code>.<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\" id=\"Anaconda\">Anaconda<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You can install it using\u00a0<code>conda<\/code>:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>conda install -c conda-forge sktime<\/code><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">or with maximum dependencies,<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>conda install -c conda-forge sktime-all-extras<\/code><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">or<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\" id=\"pip\">pip<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Using pip, sktime releases are available as source packages and binary wheels. You can see all available wheels\u00a0<a href=\"https:\/\/pypi.org\/simple\/sktime\/\">here<\/a>.<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>pip install sktime<\/code><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">or, with maximum dependencies,<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>pip install sktime[all_extras]<\/code><\/li>\r\n<\/ul>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"What's-sktime?\">What&#8217;s sktime?<\/h3>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>sktime is a unified toolbox for machine learning with time series.<\/li>\r\n<li>Time series give rise to multiple learning tasks (e.g. forecasting and time series classification).<\/li>\r\n<li>The goal of sktime is to provide all the necessary tools to solve these tasks, including dedicated time series algorithms as well as tools for building, tuning and evaluating composite models.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Many of these tasks are related. An algorithm that can solve one of them can often be re-used to help solve another one, an idea called reduction. sktime&#8217;s unified interface allows to easily adapt an algorithm for one task to another.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"Forecasting-with-sktime\">Forecasting with sktime<\/h3>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>In forecasting, past data is used to make temporal forward predictions of a time series. This is notably different from tabular prediction tasks supported by\u00a0<code>scikit-learn<\/code>\u00a0and similar libraries.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"Time-series-data-and-statistical-dependence\">Time series data and statistical dependence<\/h3>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>An intrinsic characteristic of time series is that observations are statistically dependent on past observations. So, they don&#8217;t naturally fit into the standard machine learning setting where we assume to have instances.<\/li>\r\n<li>In multivariate data, it is implausible to assume that the different univariate component time series are independent and identically distributed.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><code>sktime<\/code>\u00a0uses\u00a0<code>pandas<\/code>\u00a0for representing time series:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>pd.Series<\/code>\u00a0for univariate time series and sequences.<\/li>\r\n<li><code>pd.DataFrame<\/code>\u00a0for multivariate time series and sequenes.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The\u00a0<code>Series.index<\/code>\u00a0and\u00a0<code>DataFrame.index<\/code>\u00a0are used for representing the time series or sequence index.\u00a0<code>sktime<\/code>\u00a0supports pandas\u00a0integer,\u00a0period\u00a0and\u00a0timestamp\u00a0indices.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"Structure\">Structure<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">For More details, you can check these\u00a0<a href=\"https:\/\/github.com\/alan-turing-institute\/sktime\/tree\/main\/examples\">notebooks<\/a>\u00a0and see more about the library and forecasting. In this Article, we will work on using Forecasting.<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li>Load dataset.<\/li>\r\n<li>Dat Preprocessing.\r\n<ul>\r\n<li>Data Wrangling.<\/li>\r\n<li>Exploratory Data Analysis.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li>Train Test Split<\/li>\r\n<li>Algorithm Setup.<\/li>\r\n<li>Fit Model.<\/li>\r\n<li>Predict Model.<\/li>\r\n<li>Evaluate Model.<\/li>\r\n<\/ol>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"Import-Libraries\">Import Libraries<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[58]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>import<\/strong> pandas <strong>as<\/strong> pd\r\n<strong>import<\/strong> numpy <strong>as<\/strong> np\r\n<strong>from<\/strong> sktime.datasets <strong>import<\/strong> load_airline\r\n<strong>from<\/strong> sktime.utils.plotting <strong>import<\/strong> plot_series\r\n<strong>import<\/strong> matplotlib.pylab <strong>as<\/strong> plt\r\n<strong>%<\/strong><strong>matplotlib<\/strong> inline\r\n<\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"1.-Load-dataset\">1. Load dataset<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The dataset we have is\u00a0<strong>Box-Jenkins<\/strong>\u00a0airline dataset, which consists of the number of monthly totals of international airline passengers, from 1949 &#8211; 1960. Values are in thousands.In\u00a0[7]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">series_airline <strong>=<\/strong> load_airline()\r\nseries_airline\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[7]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">Period\r\n1949-01    112.0\r\n1949-02    118.0\r\n1949-03    132.0\r\n1949-04    129.0\r\n1949-05    121.0\r\n           ...  \r\n1960-08    606.0\r\n1960-09    508.0\r\n1960-10    461.0\r\n1960-11    390.0\r\n1960-12    432.0\r\nFreq: M, Name: Number of airline passengers, Length: 144, dtype: float64<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">After loading the data \u2013\u00a0<code>sktime<\/code>\u00a0follows a workflow similar to the\u00a0<code>sklearn<\/code>\u00a0workflow.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The steps in this workflow are as follows:<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li>Data Preprocessing.\r\n<ul>\r\n<li>Build specification time points for which forecasts are requested.<\/li>\r\n<li>We can use\u00a0<code>numpy.array<\/code>\u00a0or the\u00a0<code>ForecastingHorizon<\/code>\u00a0object.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li>Split our data into train and test datasets.<\/li>\r\n<li>Building forecasting model and prepare its algorithm.<\/li>\r\n<li>Fitting the forecaster to the data, using the forecaster&#8217;s\u00a0<code>fit<\/code>\u00a0method.<\/li>\r\n<li>Making a forecast; using the forecaster&#8217;s\u00a0<code>predict<\/code>\u00a0method.<\/li>\r\n<li>Evaluate the forecast model.<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"2.-Data-Preprocessing\">2. Data Preprocessing<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Display our data to start work on it.In\u00a0[8]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><em># Plotting for visualization<\/em>\r\nplot_series(series_airline)\r\nplt<strong>.<\/strong>show()\r\n<\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"947\" height=\"248\" class=\"wp-image-16681\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-97.png\" alt=\"How can a Forecast Time Series be more efficient than Predict Linear Regression?\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-97.png 947w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-97-300x79.png 300w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-97-768x201.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[10]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><em># Check the index of the series<\/em>\r\nseries_airline<strong>.<\/strong>index\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[10]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">PeriodIndex(['1949-01', '1949-02', '1949-03', '1949-04', '1949-05', '1949-06',\r\n             '1949-07', '1949-08', '1949-09', '1949-10',\r\n             ...\r\n             '1960-03', '1960-04', '1960-05', '1960-06', '1960-07', '1960-08',\r\n             '1960-09', '1960-10', '1960-11', '1960-12'],\r\n            dtype='period[M]', name='Period', length=144, freq='M')<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>NOTE:<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Generally, users are expected to use the in-built loading functionality of pandas and pandas-compatible packages to load data sets for forecasting, such as\u00a0<code>read_csv<\/code>\u00a0or the\u00a0<code>Series<\/code>\u00a0or\u00a0<code>DataFrame<\/code>\u00a0constructors if data is available in another in-memory format, e.g.,\u00a0<code>numpy.array<\/code>.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">sktime forecasters may accept input in pandas-adjacent formats, but will produce outputs in, and attempt to coerce inputs to,\u00a0<code>pandas<\/code>\u00a0formats.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">NOTE: if your favourite format is not properly converted or coerced, kindly consider to contribute that functionality to\u00a0<code>sktime<\/code>.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Now, we need to specify the\u00a0<strong>forecasting horizon<\/strong>\u00a0and pass that to our forecasting algorithm<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>numpy.array<\/strong>In\u00a0[12]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">fh <strong>=<\/strong> np<strong>.<\/strong>arange(1, 37)\r\nfh\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[12]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\r\n       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,\r\n       35, 36])<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This will ask for monthly predictions for the next three years, since the original series period is 1 month. In another example, to predict only the\u00a0second\u00a0and\u00a0fifth\u00a0month ahead, one could write:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>import<\/strong> numpy <strong>as<\/strong> np\r\nfh <strong>=<\/strong> np<strong>.<\/strong>array([2, 5]) <em># 2nd and 5th step ahead<\/em>\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>ForecastingHorizon<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The\u00a0<code>ForecastingHorizon<\/code>\u00a0object takes absolute indices as input, but considers the input absolute or relative depending on the\u00a0<code>is_relative<\/code>\u00a0flag.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><code>ForecastingHorizon<\/code>\u00a0will automatically assume a relative horizon if temporal difference types from\u00a0<code>pandas<\/code>\u00a0are passed; if value types from\u00a0<code>pandas<\/code>\u00a0are passed, it will assume an absolute horizon.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">To define an absolute\u00a0<code>ForecastingHorizon<\/code>\u00a0in our example:In\u00a0[13]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sktime.forecasting.base <strong>import<\/strong> ForecastingHorizon\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[32]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">three_yrs_range <strong>=<\/strong> pd<strong>.<\/strong>PeriodIndex(pd<strong>.<\/strong>date_range('1961-01', periods<strong>=<\/strong>36, freq<strong>=<\/strong>'M'))\r\nfh <strong>=<\/strong> ForecastingHorizon(three_yrs_range, is_relative<strong>=<\/strong><strong>False<\/strong>)\r\nfh\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[32]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">ForecastingHorizon(['1961-01', '1961-02', '1961-03', '1961-04', '1961-05', '1961-06',\r\n             '1961-07', '1961-08', '1961-09', '1961-10', '1961-11', '1961-12',\r\n             '1962-01', '1962-02', '1962-03', '1962-04', '1962-05', '1962-06',\r\n             '1962-07', '1962-08', '1962-09', '1962-10', '1962-11', '1962-12',\r\n             '1963-01', '1963-02', '1963-03', '1963-04', '1963-05', '1963-06',\r\n             '1963-07', '1963-08', '1963-09', '1963-10', '1963-11', '1963-12'],\r\n            dtype='period[M]', freq='M', is_relative=False)<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><code>ForecastingHorizon<\/code>-s can be converted from relative to absolute and back via the\u00a0<code>to_relative<\/code>\u00a0and\u00a0<code>to_absolute<\/code>\u00a0methods. Both of these conversions require a compatible\u00a0<code>cutoff<\/code>\u00a0to be passed:In\u00a0[20]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">cutoff <strong>=<\/strong> pd<strong>.<\/strong>Period(\"1960-12\", freq<strong>=<\/strong>'M')\r\ncutoff\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[20]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">Period('1960-12', 'M')<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[29]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">fh<strong>.<\/strong>to_relative(cutoff)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[29]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">ForecastingHorizon([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\r\n            18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,\r\n            35, 36],\r\n           dtype='int64', is_relative=True)<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[24]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">fh<strong>.<\/strong>to_absolute(cutoff)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[24]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">ForecastingHorizon(['1961-01-31', '1961-02-28', '1961-03-31', '1961-04-30',\r\n               '1961-05-31', '1961-06-30', '1961-07-31', '1961-08-31',\r\n               '1961-09-30', '1961-10-31', '1961-11-30', '1961-12-31',\r\n               '1962-01-31', '1962-02-28', '1962-03-31', '1962-04-30',\r\n               '1962-05-31', '1962-06-30', '1962-07-31', '1962-08-31',\r\n               '1962-09-30', '1962-10-31', '1962-11-30', '1962-12-31',\r\n               '1963-01-31', '1963-02-28', '1963-03-31', '1963-04-30',\r\n               '1963-05-31', '1963-06-30', '1963-07-31', '1963-08-31',\r\n               '1963-09-30', '1963-10-31', '1963-11-30', '1963-12-31'],\r\n              dtype='datetime64[ns]', freq='M', is_relative=False)<\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"3.-Temporal-Train-and-test-batch\">3. Temporal Train and test batch<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[33]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sktime.forecasting.model_selection <strong>import<\/strong> temporal_train_test_split\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[128]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">y_train, y_test <strong>=<\/strong> temporal_train_test_split(series_airline, test_size<strong>=<\/strong>36)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">We will try to forecast y_test from y_trainIn\u00a0[38]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><em># Plotting for illustration<\/em>\r\nplot_series(y_train, y_test, labels<strong>=<\/strong>['y_train', 'y_test'])\r\nplt<strong>.<\/strong>show()\r\nprint(f\"Y Train Shape: {y_train<strong>.<\/strong>shape[0]}\\nY Test Shape: {y_test<strong>.<\/strong>shape[0]}\")\r\n<\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"947\" height=\"248\" class=\"wp-image-16682\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98.png\" alt=\"How can a Forecast Time Series be more efficient than Predict Linear Regression?\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98.png 947w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98-300x79.png 300w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98-768x201.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/figure>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">Y Train Shape: 108\r\nY Test Shape: 36\r\n<\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\" id=\"Why-can-you-use--sklearn-train_test_split-instead-of-sktime-temporal_train_test_split?\">Why can you use sklearn train_test_split instead of sktime temporal_train_test_split?<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[59]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sklearn.model_selection <strong>import<\/strong> train_test_split\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[60]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">learn_y_train, learn_y_test <strong>=<\/strong> train_test_split(series_airline) \r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[65]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><em># Visulaize the data<\/em>\r\nplot_series(learn_y_train<strong>.<\/strong>sort_index(), learn_y_test<strong>.<\/strong>sort_index(), labels<strong>=<\/strong>['y_train', 'y_test'])\r\nplt<strong>.<\/strong>show()\r\n<\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"947\" height=\"248\" class=\"wp-image-16683\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98.png\" alt=\"How can a Forecast Time Series be more efficient than Predict Linear Regression?\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98.png 947w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98-300x79.png 300w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-98-768x201.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This leads to\u00a0<strong>leakage<\/strong>:<\/p>\r\n\r\n\r\n\r\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\r\n<p>The data you are using to train a machine learning algorithm happens to have the information you&#8217;re trying to predict.<\/p>\r\n<\/blockquote>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">But\u00a0<code>train_test_split(learn_y, shuffle=False)<\/code>\u00a0works, Which is what\u00a0<code>temporal_train_test_split(y)<\/code>\u00a0does in\u00a0<code>sktime<\/code>.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">That&#8217;s mean, the data you shuffled has lost the dependencies on its previous variables which connected with.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"4.-Algorithm-setup\">4. Algorithm setup<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[39]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sktime.forecasting.naive <strong>import<\/strong> NaiveForecaster\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[41]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><em># We can simply take the indices from `y_test` where they already are stored<\/em>\r\nfh <strong>=<\/strong> ForecastingHorizon(y_test<strong>.<\/strong>index, is_relative<strong>=<\/strong><strong>False<\/strong>)\r\n\r\n<em># apply the algorithm<\/em>\r\nforecast_model <strong>=<\/strong> NaiveForecaster(strategy<strong>=<\/strong>'last', sp<strong>=<\/strong>12)\r\n<\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"5.-Fitting-Model\">5. Fitting Model<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[42]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">forecast_model<strong>.<\/strong>fit(y_train)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[42]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">NaiveForecaster(sp=12)<\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"6.-Predict-Model\">6. Predict Model<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[129]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">y_pred <strong>=<\/strong> forecast_model<strong>.<\/strong>predict(fh)\r\n<\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\" id=\"Plotting-for-illustration\">Plotting for illustration<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[45]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">plot_series(y_train, y_test, y_pred, labels<strong>=<\/strong>['y_train', 'y_test', 'y_pred'])\r\nplt<strong>.<\/strong>show()\r\n<\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"248\" class=\"wp-image-16684\" src=\"http:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-99.png\" alt=\"How can a Forecast Time Series be more efficient than Predict Linear Regression?\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-99.png 947w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-99-300x79.png 300w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/09\/image-99-768x201.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"7.-Evaluate-Model\">7. Evaluate Model<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The next step is to specify a forecasting metric. These are functions that return a number when input with prediction and actual series. They are different from\u00a0<code>sklearn<\/code>\u00a0metrics in that they accept series with indices rather than\u00a0<code>np.array<\/code>s. Forecasting metrics can be invoked in two ways:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>using the lean\u00a0<strong>function interface<\/strong>, e.g.,\u00a0<code>mean_absolute_percentage_error<\/code>\u00a0which is a python function\u00a0<code>(y_true : pd.Series, y_pred : pd.Series) -&gt; float<\/code><\/li>\r\n<li>using the composable\u00a0<strong>class<\/strong>\u00a0interface, e.g.,\u00a0<code>MeanAbsolutePercentageError<\/code>, which is a python class, callable with the same signature (*compu<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h5 class=\"wp-block-heading\" id=\"mean_absolute_percentage_error\"><code>mean_absolute_percentage_error<\/code><\/h5>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[46]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sktime.performance_metrics.forecasting <strong>import<\/strong> mean_absolute_percentage_error\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[47]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mean_absolute_percentage_error(y_test, y_pred)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[47]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">0.145427686270316<\/pre>\r\n\r\n\r\n\r\n<h5 class=\"wp-block-heading\" id=\"MeanAbsolutePercentageError\"><code>MeanAbsolutePercentageError<\/code><\/h5>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[49]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sktime.performance_metrics.forecasting <strong>import<\/strong> MeanAbsolutePercentageError\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[50]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mape <strong>=<\/strong> MeanAbsolutePercentageError(symmetric<strong>=<\/strong><strong>False<\/strong>)\r\nmape(y_test, y_pred)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[50]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">0.13189432350948402<\/pre>\r\n\r\n\r\n\r\n<h5 class=\"wp-block-heading\" id=\"mean_squared_percentage_error\"><code>mean_squared_percentage_error<\/code><\/h5>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[66]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> sktime.performance_metrics.forecasting <strong>import<\/strong> mean_squared_percentage_error\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In\u00a0[67]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mean_squared_percentage_error(y_test, y_pred)\r\n<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Out[67]:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">0.030181651132460994<br \/><br \/><br \/><\/pre>\r\n<p><b><span data-contrast=\"auto\">How ISmile Technologies Help:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\r\n<ul>\r\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"auto\">ISmile Technologies provides advanced machine learning and predictive modeling techniques to analyze and forecast time series data.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\r\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"auto\">With its expertise in statistical modeling and time series analysis, ISmile can provide more accurate and reliable forecasts than simple linear regression.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\r\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span data-contrast=\"auto\">ISmile&#8217;s forecasting models take into account various factors such as seasonality, trends, and cyclic patterns, which are often overlooked in linear regression.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\r\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"4\" data-aria-level=\"1\"><span data-contrast=\"auto\">By using machine learning algorithms and advanced statistical models, ISmile can quickly and efficiently process large amounts of data, resulting in more timely and accurate forecasts.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\r\n<\/ul>\r\n<ul>\r\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"auto\">In addition to forecasting, ISmile can provide valuable insights and recommendations for businesses to improve their decision-making and optimize their operations.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\r\n<\/ul>\r\n<p>&nbsp;<\/p>\r\n<p><span data-contrast=\"auto\">ISmile Technologies can help businesses gain a competitive advantage by providing more accurate and efficient time series forecasting than traditional linear regression methods. By leveraging advanced machine learning algorithms and statistical models, ISmile can provide valuable insights and recommendations to optimize business operations and drive growth.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"Summary\">Summary<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>To summarize that, we can say, if your model starts working on observations that depend on time scale \u2013 it will be more efficient to use time series forecasting \u2013 this will help you to have precise forecasting according to the selected time.\r\n<ul>\r\n<li>For instance, if you&#8217;re looking for getting the price of a share over the next day, you will need to use time-series to forecast the share&#8217;s price.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li>But, If your observations contain only continuous data and you&#8217;re looking to predict estimate values, whether it is future, current, or past values.\r\n<ul>\r\n<li>For instance, if you&#8217;re looking for getting the prices of the shares in August, you need to give the model the last three months of the observation to analyze it and give the prediction of these shares.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<\/ul>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"References\">References<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=Prpu_U5tKkE&amp;list=PLy6al93uC2HW3tN7Qzl0yvtySjQnhA5FY&amp;index=2\">Lecture 13 Time Series Analysis<\/a><\/li>\r\n<li><a href=\"https:\/\/stats.stackexchange.com\/questions\/65287\/difference-between-forecast-and-prediction\">Difference between forecast and prediction?<\/a><\/li>\r\n<li><a href=\"https:\/\/github.com\/alan-turing-institute\/sktime\/blob\/main\/docs\/source\/user_guide\/introduction.rst\">sktime library examples<\/a><\/li>\r\n<li><a href=\"https:\/\/www.machinelearningplus.com\/time-series\/time-series-analysis-python\/\">Time Series Analysis in Python<\/a><\/li>\r\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=wqQKFu41FIw&amp;list=PLy6al93uC2HW3tN7Qzl0yvtySjQnhA5FY&amp;index=1\">Introduction to Machine Learning with Time Series || Markus Loning<\/a><\/li>\r\n<\/ul>\r\n","protected":false},"excerpt":{"rendered":"<p>By,\u00a0Ahmed Nabil Atwa, Data Scientist | Machine Learning | Deep learning Researcher Definition of Time Series Time series is an ordered sequence of\u00a0values of variables at equally spaced time intervals.\u00a0 To understand time series, we shall start with understanding the difference between prediction and forecasting. Prediction is a definitive and specific statement that gives you [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":16795,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-16679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/16679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/comments?post=16679"}],"version-history":[{"count":5,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/16679\/revisions"}],"predecessor-version":[{"id":37973,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/16679\/revisions\/37973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media\/16795"}],"wp:attachment":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media?parent=16679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/categories?post=16679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/tags?post=16679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}