How to compute averages using a sliding window over an array?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# 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

``` moving_average(np.arange(20),5) ```

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

##### Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

##### Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

##### Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

##### Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

##### House Price Prediction Project using Machine Learning
Use the Zillow dataset to follow a test-driven approach and build a regression machine learning model to predict the price of the house based on other variables.

##### Time Series Analysis Project in R on Stock Market forecasting
In this time series project, you will build a model to predict the stock prices and identify the best time series forecasting model that gives reliable and authentic results for decision making.

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

##### Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

##### Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

##### Machine learning for Retail Price Recommendation with Python
Use the Mercari Dataset with dynamic pricing to build a price recommendation algorithm using machine learning in Python to automatically suggest the right product prices.