One of the main reasons that we want to analyze time series data is to extract interesting statistics from it. This provides a lot of information regarding the nature of the data. In this recipe, we will take a look at how to extract these stats.
import numpy as np import pandas as pd import matplotlib.pyplot as plt from convert_to_timeseries import convert_data_to_timeseries
# Input file containing data input_file = 'data_timeseries.txt'
# Load data data1 = convert_data_to_timeseries(input_file, 2) data2 = convert_data_to_timeseries(input_file, 3)
dataframe = pd.DataFrame({'first': data1, 'second': data2})
# Print max and min print ' Maximum: ', dataframe.max() print ' Minimum: ', dataframe.min()
# Print mean print ' Mean: ', dataframe.mean() print ' Mean row-wise: ', dataframe.mean(1)[:10]
24
and plot this, as follows:# Plot rolling mean pd.rolling_mean(dataframe, window=24).plot()
# Print correlation coefficients print ' Correlation coefficients: ', dataframe.corr()
60
:# Plot rolling correlation plt.figure() pd.rolling_corr(dataframe['first'], dataframe['second'], window=60).plot() plt.show()
extract_stats.py
file that is already provided to you. If you run the code, the rolling mean will look like the following:18.222.95.7