Finding moving average from data points in Python

Finding moving average from data points in Python

To calculate the moving average of data points in Python, you can use libraries like NumPy to efficiently perform the moving average operation. The moving average is a commonly used statistical calculation that helps smooth out fluctuations in data by averaging a specified number of consecutive data points.

Here's how you can calculate a simple moving average in Python using NumPy:

import numpy as np def moving_average(data, window_size): """ Calculate the simple moving average of a list of data points. Args: - data: List or NumPy array of data points. - window_size: Size of the moving average window. Returns: - List of moving average values with the same length as the input data. """ cumsum = np.cumsum(data, dtype=float) cumsum[window_size:] = cumsum[window_size:] - cumsum[:-window_size] return cumsum[window_size - 1:] / window_size # Example data data = [10, 20, 30, 40, 50, 60, 70, 80, 90] # Specify the window size for the moving average window_size = 3 # Calculate the moving average moving_avg = moving_average(data, window_size) print(moving_avg) 

In this example:

  • We define a function called moving_average that takes a list or NumPy array of data points and a window size as arguments.
  • Inside the function, we calculate the cumulative sum of the data using np.cumsum().
  • We then calculate the moving average by subtracting the cumulative sum of elements before the window and dividing by the window size.
  • The result is a list of moving average values with the same length as the input data.

In the example data provided, the moving average with a window size of 3 will be calculated, and the result will be printed. You can adjust the window_size and input data as needed for your specific use case.

Examples

  1. Python moving average calculation using a simple loop: Description: This query involves calculating the moving average of a list of data points in Python using a simple loop and summing up the values within the specified window size.

    def moving_average(data, window_size): moving_averages = [] for i in range(len(data) - window_size + 1): window = data[i:i + window_size] average = sum(window) / window_size moving_averages.append(average) return moving_averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 
  2. Python moving average calculation using numpy convolutions: Description: This query demonstrates calculating the moving average of a list of data points in Python using numpy's convolve() function with a kernel of ones.

    import numpy as np def moving_average(data, window_size): kernel = np.ones(window_size) / window_size averages = np.convolve(data, kernel, mode='valid') return averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 
  3. Python moving average calculation using pandas rolling window: Description: This query involves calculating the moving average of a list of data points in Python using pandas' rolling() function with a specified window size.

    import pandas as pd def moving_average(data, window_size): series = pd.Series(data) averages = series.rolling(window=window_size).mean().dropna().tolist() return averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 
  4. Python moving average calculation using deque: Description: This query demonstrates calculating the moving average of a list of data points in Python using a deque to efficiently handle the sliding window.

    from collections import deque def moving_average(data, window_size): moving_averages = [] window_sum = sum(data[:window_size]) moving_averages.append(window_sum / window_size) window = deque(data[:window_size], maxlen=window_size) for i in range(window_size, len(data)): window_sum += data[i] - window.popleft() moving_averages.append(window_sum / window_size) window.append(data[i]) return moving_averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 
  5. Python moving average calculation using numpy cumsum: Description: This query involves calculating the moving average of a list of data points in Python using numpy's cumsum() function to efficiently compute the cumulative sum.

    import numpy as np def moving_average(data, window_size): cumsum = np.cumsum(data) cumsum[window_size:] = cumsum[window_size:] - cumsum[:-window_size] averages = cumsum[window_size - 1:] / window_size return averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 
  6. Python moving average calculation using itertools: Description: This query demonstrates calculating the moving average of a list of data points in Python using the itertools module's tee() function to efficiently create window iterators.

    from itertools import tee, islice def moving_average(data, window_size): def window(iterable, size): iterators = tee(iterable, size) for i in range(1, size): for iterator in iterators[i:]: next(iterator, None) return zip(*iterators) moving_averages = [] for window_data in window(data, window_size): moving_averages.append(sum(window_data) / window_size) return moving_averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 
  7. Python moving average calculation using queue.Queue: Description: This query involves calculating the moving average of a list of data points in Python using the queue module's Queue class to efficiently handle the sliding window.

    from queue import Queue def moving_average(data, window_size): moving_averages = [] window_sum = sum(data[:window_size]) moving_averages.append(window_sum / window_size) window = Queue(maxsize=window_size) for d in data[window_size:]: popped = window.get() window_sum -= popped window_sum += d window.put(d) moving_averages.append(window_sum / window_size) return moving_averages # Example usage: data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] window_size = 3 averages = moving_average(data, window_size) print("Moving averages:", averages) 

More Tags

copy-paste raspberry-pi2 email-client cordova spring-cloud-gateway file-permissions symfony4 spring-boot-devtools nlp swig

More Python Questions

More Biology Calculators

More Everyday Utility Calculators

More Other animals Calculators

More Auto Calculators