How to compute averages using a sliding window over an array?

How to compute averages using a sliding window over an array?

How to compute averages using a sliding window over an array?

This recipe helps you compute averages using a sliding window over an array


Recipe Objective

While handling arrays, random arrays are often used for calcuation. Sometimes, the number might not be following sequence. To handle such instance, moving average becomes quite handy.

So this recipe is a short example on how to compute moving averages using a sliding window over an array. Let's get started.

Step 1 - Import the library

import numpy as np

Let's pause and look at these imports. Numpy is generally helpful in data manipulation while working with arrays. It also helps in performing mathematical operation.

Step 2 - Defining moving_array function

def moving_average(a, n) : test = np.cumsum(a, dtype=float) test[n:] = test[n:] - test[:-n] return test[n - 1:] / n

We have a defined a function that helps in returning moving average. It uses cumulative sum for calculation of the same.

Step 3 - Printing the moving average


We have send a array of size 20 and then calling the moving_average function, defined earlier, simply printing away the output.

Step 4 - Lets look at our dataset now

Once we run the above code snippet, we will see:

array([ 2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12., 13., 14.,
       15., 16., 17.])

Relevant Projects

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.