Last active
May 7, 2018 11:31
-
-
Save dinya/4db9dfb88ca6c369e9f4921bf480a18b to your computer and use it in GitHub Desktop.
Revisions
-
dinya revised this gist
May 7, 2018 . 1 changed file with 8 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,11 @@ def moving_average(a, window=3): """https://stackoverflow.com/a/14314054/716469 and modified to be like ``pandas.Series.rolling(window=n).mean()``: a = np.array([1,2,3,4,5]) pd.Series(a).rolling(window=3).mean().values == moving_average(a, window=3) """ ret = np.cumsum(a, dtype=float) ret[window:] = ret[window:] - ret[:-window] ret[:window-1] = np.NaN return ret / window -
dinya created this gist
May 7, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ def moving_average(a, n=3): """https://stackoverflow.com/a/14314054/716469 and modified to be like [``pandas.DataFrame.rolling(win=n).mean()``](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html) """ ret = np.cumsum(a, dtype=float) ret[n:] = ret[n:] - ret[:-n] ret[:n] = np.NaN return ret / n