Package: time-series-dataset
Time Series DatasetDescriptionTime series dataset forTime series predictorInstallationpip install time-series-datasetUsage example"""FlightsDataset"""importcalendarimportmathimportnumpyasnpimportpandasaspdimportseabornassnsfromdatetimeimportdatetimefromtime_series_datasetimportTimeSeriesDatasetdef_raw_make_predictor(features,*reshape_args):#pylint: disable=too-many-function-argsreturnnp.concatenate(features,axis=-1).reshape(list(reshape_args)+[len(features)]).astype(np.float32)def_make_predictor(features,number_of_training_examples):#pylint: disable=too-many-function-argsreturn_raw_make_predictor(features,number_of_training_examples,-1)def_get_labels(input_features,output_features):def_features_to_label_list(features):return[list(feature)[0]forfeatureinfeatures]labels={}labels['x']=_features_to_label_list(input_features)labels['y']=_features_to_label_list(output_features)returnlabelsclassFlightsDataset(TimeSeriesDataset):"""FlightsDataset class:param except_last_n: initialize the FlightsDataset without n last months"""# pylint: disable=too-many-localsdef__init__(self,except_last_n=0):flights_dataset=sns.load_dataset("flights")chopped_flights_dataset=flights_dataset[:len(flights_dataset)-except_last_n]passengers=chopped_flights_dataset['passengers']month=chopped_flights_dataset['month']year=chopped_flights_dataset['year']months_3l=[month_name[0:3]formonth_nameinlist(calendar.month_name)]month_number=[months_3l.index(_month)for_monthinmonth]passengers_df=pd.DataFrame(passengers)month_number_df=pd.DataFrame(data={'month_number':month_number})year_df=pd.DataFrame(year)number_of_training_examples=1# Store month_number and year as _xinput_features=[month_number_df,year_df]_x=_make_predictor(input_features,number_of_training_examples)# Store passengers as _youtput_features=[passengers_df]_y=_make_predictor(output_features,number_of_training_examples)super().__init__(_x,_y,_get_labels(input_features,output_features))self.month_number_df=month_number_dfself.year_df=year_df# pylint: disable=arguments-differdefmake_future_dataframe(self,number_of_months,include_history=True):"""make_future_dataframe:param number_of_months: number of months to predict ahead:param include_history: optional, selects if training history is to be included or not:returns: future dataframe with the selected amount of months"""defcreate_dataframe(name,data):returnpd.DataFrame(data={name:data})defcreate_month_dataframe(data):returncreate_dataframe('month_number',data)defcreate_year_dataframe(data):returncreate_dataframe('year',data)month_number_df=self.month_number_dfyear_df=self.year_dflast_month=month_number_df.values[-1][0]last_year=year_df.values[-1][0]ifnotinclude_history:month_number_df=create_month_dataframe([])year_df=create_year_dataframe([])foriinrange(number_of_months):month_index=last_month+inew_months=[math.fmod(month_index,12)+1]new_years=[last_year+math.floor(month_index/12)]month_number_df=month_number_df.append(create_month_dataframe(new_months),ignore_index=True)year_df=year_df.append(create_year_dataframe(new_years),ignore_index=True)input_features=[month_number_df,year_df]return_make_predictor(input_features,1)defconvert_year_month_array_to_datetime(year_month_array):defconvert_singe_year_month_array_to_datetime(single_year_month_array):returndatetime(year=single_year_month_array[1],month=single_year_month_array[0],day=15)year_month_array_size=year_month_array.sizeifyear_month_array_size==2:returnconvert_singe_year_month_array_to_datetime(year_month_array)return[convert_singe_year_month_array_to_datetime(year_month_array[idx,...])foridxinrange(len(year_month_array))]Oze dataset history