Skip to content

Commit b3fd849

Browse files
committed
smoothing tutorial
1 parent 667743f commit b3fd849

File tree

1 file changed

+52
-73
lines changed

1 file changed

+52
-73
lines changed

unconverted/python/smoothing.md

+52-73
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ jupyter:
88
format_version: '1.1'
99
jupytext_version: 1.1.1
1010
kernelspec:
11-
display_name: Python 2
11+
display_name: Python 3
1212
language: python
13-
name: python2
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.7
1424
plotly:
1525
description: Learn how to perform smoothing using various methods in Python.
1626
display_as: signal-analysis
@@ -25,18 +35,12 @@ jupyter:
2535
title: Smoothing in Python | plotly
2636
---
2737

28-
#### New to Plotly?
29-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
30-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
31-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
32-
3338

3439
#### Imports
3540
The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plot.ly/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/) and [Plotly](https://plot.ly/python/getting-started/).
3641

3742
```python
38-
import plotly.plotly as py
39-
import plotly.graph_objs as go
43+
import plotly.graph_objects as go
4044

4145
import numpy as np
4246
import pandas as pd
@@ -50,53 +54,59 @@ from scipy import signal
5054

5155
There is reason to smooth data if there is little to no small-scale structure in the data. The danger to this thinking is that one may skew the representation of the data enough to change its percieved meaning, so for the sake of scientific honesty it is an imperative to at the very minimum explain one's reason's for using a smoothing algorithm to their dataset.
5256

57+
In this example we use the [Savitzky-Golay Filter](https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter), which fits subsequents windows of adjacent data with a low-order polynomial.
58+
5359
```python
60+
import plotly.graph_objects as go
61+
62+
import numpy as np
63+
import pandas as pd
64+
import scipy
65+
66+
from scipy import signal
67+
5468
x = np.linspace(0, 10, 100)
5569
y = np.sin(x)
56-
y_noise = [y_item + np.random.choice([-1, 1])*np.random.random() for y_item in y]
70+
noise = 2 * np.random.random(len(x)) - 1 # uniformly distributed between -1 and 1
71+
y_noise = y + noise
5772

58-
trace1 = go.Scatter(
73+
fig = go.Figure()
74+
fig.add_trace(go.Scatter(
5975
x=x,
6076
y=y,
6177
mode='markers',
62-
marker=dict(
63-
size=2,
64-
color='rgb(0, 0, 0)',
65-
),
78+
marker=dict(size=2, color='black'),
6679
name='Sine'
67-
)
80+
))
6881

69-
trace2 = go.Scatter(
82+
fig.add_trace(go.Scatter(
7083
x=x,
7184
y=y_noise,
7285
mode='markers',
7386
marker=dict(
7487
size=6,
75-
color='#5E88FC',
88+
color='royalblue',
7689
symbol='circle-open'
7790
),
7891
name='Noisy Sine'
79-
)
92+
))
8093

81-
trace3 = go.Scatter(
94+
fig.add_trace(go.Scatter(
8295
x=x,
83-
y=signal.savgol_filter(y, 53, 3),
96+
y=signal.savgol_filter(y,
97+
53, # window size used for filtering
98+
3), # order of fitted polynomial
8499
mode='markers',
85100
marker=dict(
86101
size=6,
87-
color='#C190F0',
102+
color='mediumpurple',
88103
symbol='triangle-up'
89104
),
90105
name='Savitzky-Golay'
91-
)
106+
))
92107

93-
layout = go.Layout(
94-
showlegend=True
95-
)
96108

97-
data = [trace1, trace2, trace3]
98-
fig = go.Figure(data=data, layout=layout)
99-
py.iplot(fig, filename='smoothing-savitzky-golay-filter')
109+
fig.show()
100110
```
101111

102112
#### Triangular Moving Average
@@ -111,7 +121,7 @@ SMA_i = \frac{y_i + ... + y_{i+n}}{n}
111121
\end{align*}
112122
$$
113123

114-
In the `Triangular Moving Average`, two simple moving averages are computed on top of each other. This means that our $SMA_i$ are computed then a Triangular Moving Average $TMA_i$ is computed as:
124+
In the `Triangular Moving Average`, two simple moving averages are computed on top of each other, in order to give more weight to closer (adjacent) points. This means that our $SMA_i$ are computed then a Triangular Moving Average $TMA_i$ is computed as:
115125

116126
$$
117127
\begin{align*}
@@ -120,25 +130,21 @@ TMA_i = \frac{SMA_i + ... + SMA_{i+n}}{n}
120130
$$
121131

122132
```python
123-
np.array(list(range(5)) + [5] + list(range(5)[::-1]))
124-
```
125-
126-
```python
127-
def smoothTriangle(data, degree, dropVals=False):
128-
triangle=np.array(list(range(degree)) + [degree] + list(range(degree)[::-1])) + 1
133+
def smoothTriangle(data, degree):
134+
triangle=np.concatenate((np.arange(degree + 1), np.arange(degree)[::-1])) # up then down
129135
smoothed=[]
130136

131137
for i in range(degree, len(data) - degree * 2):
132138
point=data[i:i + len(triangle)] * triangle
133-
smoothed.append(sum(point)/sum(triangle))
134-
if dropVals:
135-
return smoothed
139+
smoothed.append(np.sum(point)/np.sum(triangle))
140+
# Handle boundaries
136141
smoothed=[smoothed[0]]*int(degree + degree/2) + smoothed
137142
while len(smoothed) < len(data):
138143
smoothed.append(smoothed[-1])
139144
return smoothed
140145

141-
trace1 = go.Scatter(
146+
fig = go.Figure()
147+
fig.add_trace(go.Scatter(
142148
x=x,
143149
y=y,
144150
mode='markers',
@@ -147,9 +153,9 @@ trace1 = go.Scatter(
147153
color='rgb(0, 0, 0)',
148154
),
149155
name='Sine'
150-
)
156+
))
151157

152-
trace2 = go.Scatter(
158+
fig.add_trace(go.Scatter(
153159
x=x,
154160
y=y_noise,
155161
mode='markers',
@@ -159,9 +165,9 @@ trace2 = go.Scatter(
159165
symbol='circle-open'
160166
),
161167
name='Noisy Sine'
162-
)
168+
))
163169

164-
trace3 = go.Scatter(
170+
fig.add_trace(go.Scatter(
165171
x=x,
166172
y=smoothTriangle(y_noise, 10), # setting degree to 10
167173
mode='markers',
@@ -171,34 +177,7 @@ trace3 = go.Scatter(
171177
symbol='triangle-up'
172178
),
173179
name='Moving Triangle - Degree 10'
174-
)
175-
176-
layout = go.Layout(
177-
showlegend=True
178-
)
179-
180-
data = [trace1, trace2, trace3]
181-
fig = go.Figure(data=data, layout=layout)
182-
py.iplot(fig, filename='smoothing-triangular-moving-average-degree-10')
183-
```
184-
185-
```python
186-
from IPython.display import display, HTML
187-
188-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
189-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
190-
191-
! pip install git+https://github.com/plotly/publisher.git --upgrade
192-
import publisher
193-
publisher.publish(
194-
'python-Smoothing.ipynb', 'python/smoothing/', 'Smoothing | plotly',
195-
'Learn how to perform smoothing using various methods in Python.',
196-
title='Smoothing in Python | plotly',
197-
name='Smoothing',
198-
language='python',
199-
page_type='example_index', has_thumbnail='false', display_as='signal-analysis', order=1)
200-
```
201-
202-
```python
180+
))
203181

182+
fig.show()
204183
```

0 commit comments

Comments
 (0)