Package: skits
skitsA library forSciKit-learn-InspiredTimeSeries models.The primary goal of this library is to allow one to train time series prediction models using a similar API toscikit-learn. Consequently, similar toscikit-learn, this library consists ofpreprocessors,feature_extractors, andpipelines.InstallationInstall with pip:pip install skitsPreprocessorsThe preprocessors expect to receive time series data, and then end up storing some data about the time series such that they can fully invert a transform. The following example shows how to create aDifferenceTransformertransform data, and then invert it back to its original form. TheDifferenceTransformersubtracts the point shifted byperiodaway from each point.importnumpyasnpfromskits.preprocessingimportDifferenceTransformery=np.random.random(10)# scikit-learn expects 2D design matrices,# so we duplicate the time series.X=y[:,np.newaxis]dt=DifferenceTransformer(period=2)Xt=dt.fit_transform(X,y)X_inv=dt.inverse_transform(Xt)assertnp.allclose(X,X_inv)Feature ExtractorsAfter all preprocessing transformations are completed, multiple features may be built out of the time series. These can be built via feature extractors, which one should combine together into a largeFeatureUnion. Current features include autoregressive, seasonal, and integrated features (covering the AR and I of ARIMA models).PipelinesThere are two types of pipelines. TheForecasterPipelineis for forecasting time series (duh). Specifically, one should build this pipeline with a regressor as the final step such that one can make appropriate predictions. The functionality is similar to a regularscikit-learnpipeline. Differences include the addition of aforecast()method along with ato_scalekeyword argument topredict()such that one can make sure that their prediction is on the same scale as the original data.These classes are likely subject to change as they are fairly hacky right now. For example, one must transform bothXandyfor all transformations before the introduction of aDifferenceTransformer. While the pipeline handles this, one must prefix all of these transformations withpre_in the step names.Anywho, here's an example:importnumpyasnpfromsklearn.linear_modelimportLinearRegressionfromsklearn.preprocessingimportStandardScalerfromsklearn.pipelineimportFeatureUnionfromskits.pipelineimportForecasterPipelinefromskits.preprocessingimportReversibleImputerfromskits.feature_extractionimport(AutoregressiveTransformer,SeasonalTransformer)steps=[('pre_scaling',StandardScaler()),('features',FeatureUnion([('ar_transformer',AutoregressiveTransformer(num_lags=3)),('seasonal_transformer',SeasonalTransformer(seasonal_period=20))])),('post_features_imputer',ReversibleImputer()),('regressor',LinearRegression(fit_intercept=False))]l=np.linspace(0,1,101)y=5*np.sin(2*np.pi*5*l)+np.random.normal(0,1,size=101)X=y[:,np.newaxis]pipeline=ForecasterPipeline(steps)pipeline.fit(X,y)y_pred=pipeline.predict(X,to_scale=True,refit=True)And this ends up looking like:importmatplotlib.pyplotaspltplt.plot(y,lw=2)plt.plot(y_pred,lw=2)plt.legend(['y_true','y_pred'],bbox_to_anchor=(1,1));And forecasting looks likestart_idx=70plt.plot(y,lw=2);plt.plot(pipeline.forecast(y[:,np.newaxis],start_idx=start_idx),lw=2);ax=plt.gca();ylim=ax.get_ylim();plt.plot((start_idx,start_idx),ylim,lw=4);plt.ylim(ylim);plt.legend(['y_true','y_pred','forecast start'],bbox_to_anchor=(1,1));