-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoothing.py
96 lines (73 loc) · 3.76 KB
/
smoothing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import numpy as np
from itertools import islice
def simple_moving_average(num_arr, window_size=7):
"""
Calculate the simple moving average of an input array with a given window size.
Parameters:
num_arr (array-like): Input array or list of numerical values.
window_size (int, optional): The size of the moving window. Default is 7.
Returns:
list: A list containing the moving averages. If a window contains NaN values, the average will ignore them.
Example:
>>> simple_moving_average([1, 2, np.nan, 4, 5, np.nan, 7, 8, 9, np.nan], window_size=3)
[1.3333333333333333, 1.5, 3.0, 4.5, 4.5, 6.0, 7.5, 8.0, 8.5, 9.0]
"""
half_window = window_size // 2
# Handle different input types: list, NumPy array, or Pandas Series
first_element = num_arr[0] if isinstance(num_arr, (list, np.ndarray)) else num_arr.iloc[0]
last_element = num_arr[-1] if isinstance(num_arr, (list, np.ndarray)) else num_arr.iloc[-1]
padded_arr = [first_element] * half_window + list(num_arr) + [last_element] * half_window
# Sliding window generator
def sliding_window(iterable, size):
it = iter(iterable)
result = tuple(islice(it, size))
if len(result) == size:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
# Calculate moving averages ignoring NaNs
return [np.nanmean(window) for window in sliding_window(padded_arr, window_size)]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# def simple_moving_average(num_arr, window_size=7):
# """
# Calculate the simple moving average of an input array with a given window size.
# Parameters:
# num_arr (array-like): Input array or list of numerical values.
# window_size (int, optional): The size of the moving window. Default is 7. Must be an odd number.
# Returns:
# list: A list containing the moving averages. If a window contains NaN values, the average will ignore them.
# Example:
# >>> simple_moving_average([1, 2, np.nan, 4, 5, np.nan, 7, 8, 9, np.nan], window_size=3)
# [1.3333333333333333, 1.5, 3.0, 4.5, 4.5, 6.0, 7.5, 8.0, 8.5, 9.0]
# """
# # Get the length of the input array
# data_length = len(num_arr)
# # Calculate the half window size
# half_window_size = int((window_size - 1) / 2)
# # Initialize a list to store the moving averages
# window_average_cache = []
# # Iterate through each element in the input array
# for i in range(data_length):
# # Initialize a temporary list to store values within the window
# temp = []
# # Iterate through the window range centered around the current element
# for j in list(range(-int(window_size / 2), int(window_size / 2) + 1, 1)):
# try:
# # Calculate the index of the element within the window
# out_index = i + j
# # If the index is out of bounds (negative), append the current element value
# if out_index < 0:
# temp.append(num_arr[i])
# else:
# # Otherwise, append the value at the calculated index
# temp.append(num_arr[out_index])
# except Exception as e:
# # raise ValueError(f"{e}")
# pass
# # Calculate the average of the values in the window, ignoring NaNs
# window_average = np.nanmean(temp)
# # Append the calculated average to the result list
# window_average_cache.append(window_average)
# return window_average_cache
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -