7 examples of 'numpy rolling mean' in Python

Every line of 'numpy rolling mean' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
81def mean(self):
82 def mean(scol):
83 return F.when(
84 F.row_number().over(self._unbounded_window) >= self._min_periods,
85 F.mean(scol).over(self._window)
86 ).otherwise(F.lit(None))
87
88 return self._apply_as_series_or_frame(mean)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

273def rolling_mean_by_h(x, h, w, name):
274 """Compute a rolling mean of x, after first aggregating by h.
275
276 Right-aligned. Computes a single mean for each unique value of h. Each
277 mean is over at least w samples.
278
279 Parameters
280 ----------
281 x: Array.
282 h: Array of horizon for each value in x.
283 w: Integer window size (number of elements).
284 name: Name for metric in result dataframe
285
286 Returns
287 -------
288 Dataframe with columns horizon and name, the rolling mean of x.
289 """
290 # Aggregate over h
291 df = pd.DataFrame({'x': x, 'h': h})
292 df2 = (
293 df.groupby('h').agg(['mean', 'count']).reset_index().sort_values('h')
294 )
295 xm = df2['x']['mean'].values
296 ns = df2['x']['count'].values
297 hs = df2['h'].values
298
299 res_h = []
300 res_x = []
301 # Start from the right and work backwards
302 i = len(hs) - 1
303 while i >= 0:
304 # Construct a mean of at least w samples.
305 n = int(ns[i])
306 xbar = float(xm[i])
307 j = i - 1
308 while ((n < w) and j >= 0):
309 # Include points from the previous horizon. All of them if still
310 # less than w, otherwise just enough to get to w.
311 n2 = min(w - n, ns[j])
312 xbar = xbar * (n / (n + n2)) + xm[j] * (n2 / (n + n2))
313 n += n2
314 j -= 1
315 if n < w:
316 # Ran out of horizons before enough points.
317 break
318 res_h.append(hs[i])
319 res_x.append(xbar)
320 i -= 1
321 res_h.reverse()
322 res_x.reverse()
323 return pd.DataFrame({'horizon': res_h, name: res_x})
85def sma(data, span=100):
86 """Computes and returns the simple moving average.
87
88 Note: the moving average is computed on all columns.
89
90 :Input:
91 :data: pandas.DataFrame with stock prices in columns
92 :span: int (defaul: 100), number of days/values over which
93 the average is computed
94
95 :Output:
96 :sma: pandas.DataFrame of simple moving average
97 """
98 return data.rolling(window=span, center=False).mean()
14def period_mean(data, freq):
15 '''
16 Method to calculate mean for each frequency
17 '''
18 return np.array(
19 [np.mean(data[i::freq]) for i in range(freq)])
2155def hpat_pandas_series_mean_impl(self, axis=None, skipna=None, level=None, numeric_only=None):
2156 if skipna is None:
2157 skipna = True
2158
2159 if skipna:
2160 return numpy.nanmean(self._data)
2161
2162 return self._data.mean()
314def __mean(xarr):
315 """mean = x.sum() / len(x)""" #interface is [lb,ub]; not lb,ub
316 from numpy import mean
317 return mean(xarr)
174@rabbit
175def mean(self):
176 """Finds The Arithmetic Mean."""
177 return sum(self.units)/float(len(self))

Related snippets