Source code for EDAspy.timeseries.TS_transformations

import numpy as np
import matplotlib.pyplot as plt


[docs]class TSTransformations: """ Tool to calculate time series transformations. Some time series transformations are given. This is just a very simple tool. It is not mandatory to use this tool to use the time series transformations selector. It is only disposed to be handy. """ data = -1 # init function def __init__(self, data): """ Constructor of the class :param data: data to work with :type data: pandas DataFrame """ self.data = data # remove trend from TS variable # calculate log of TS
[docs] def log(self, variable, plot=False): """ Calculate the logarithm of the time series. :param variable: name of variables :type variable: string :param plot: if True a plot is given. :type plot: bool :return: time series transformation :rtype: list """ log_trans = np.log(self.data[variable]) if plot: plt.plot(self.data[variable]) plt.show() plt.plot(log_trans) plt.show() return list(log_trans)
# Box-Cox transformation
[docs] def box_cox(self, variable, lmbda, plot=False): """ Calculate Box Cox time series transformation. :param variable: name of variable :type variable: string :param lmbda: lambda parameter of Box Cox transformation :type lmbda: float :param plot: if True, plot is given.รง :type plot: bool :return: time series transformation :rtype: list """ from scipy.stats import boxcox transformed = boxcox(self.data[variable], lmbda=lmbda) if plot: plt.plot(self.data[variable]) plt.show() plt.plot(transformed) plt.show() return list(transformed)
# smooth of the TS, to reduce noise
[docs] def smoothing(self, variable, window, plot=False): """ Calculate time series smoothing transformation. :param variable: name of variable :type variable: string :param window: number of previous instances taken to smooth. :type window: int :param plot: if True, plot is given :type plot: bool :return: time series transformation :rtype: list """ rolling_mean = self.data[variable].rolling(window=window, min_periods=1).mean() if plot: plt.plot(rolling_mean) self.data[variable].plot() plt.show() return list(rolling_mean)
# power transformations
[docs] def power(self, variable, power, plot=False): """ Calculate power time series transformation. :param variable: name of variable :type variable: string :param power: exponent to calculate :type power: int :param plot: if True, plot is given :type plot: bool :return: time series transformation :rtype: list """ values = self.data[variable].values transformation = [i**power for i in values] if plot: plt.plot(transformation) plt.plot(values) plt.show() return list(transformation)
# exponential transformation
[docs] def exponential(self, variable, numerator, plot=False): """ Calculate exponential time series transformation. :param variable: name of variable :type variable: string :param numerator: numerator of the transformation :type numerator: int :param plot: if True, plot is given :type plot: bool :return: time series transformation :rtype: list """ values = self.data[variable].values transformation = [np.exp(numerator/i) for i in values] if plot: plt.plot(transformation) plt.plot(values) plt.show() return transformation