Show Sidebar Hide Sidebar

# Orthogonal Matching Pursuit in Scikit-learn

Using orthogonal matching pursuit for recovering a sparse signal from a noisy measurement encoded with a dictionary.

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version¶

In [1]:
import sklearn
sklearn.__version__

Out[1]:
'0.18.1'

### Imports¶

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
from sklearn.linear_model import OrthogonalMatchingPursuit
from sklearn.linear_model import OrthogonalMatchingPursuitCV
from sklearn.datasets import make_sparse_coded_signal


### Calculations¶

In [3]:
n_components, n_features = 512, 100
n_nonzero_coefs = 17

# generate the data
###################
# y = Xw
# |x|_0 = n_nonzero_coefs
y, X, w = make_sparse_coded_signal(n_samples=1,
n_components=n_components,
n_features=n_features,
n_nonzero_coefs=n_nonzero_coefs,
random_state=0)

idx, = w.nonzero()


Distort the clean signal

In [4]:
y_noisy = y + 0.05 * np.random.randn(len(y))


### Plot The Sparse Signal¶

In [5]:
data=[]

trace1 = go.Scatter(x=idx, y=w[idx], mode='markers',
marker=dict(color='blue'),
showlegend=False)
data.append(trace1)

for i in range(0, len(idx)):
trace = go.Scatter(x= [idx[i], idx[i]], y=[0, w[idx][i]],
mode='lines',
line=dict(color='blue', width=1),
showlegend=False
)
data.append(trace)

layout = go.Layout(title='The Sparse Signal', hovermode='closest')
fig = go.Figure(data=data, layout=layout)

In [6]:
py.iplot(fig)

Out[6]:

### Plot The Noise-Free Reconstruction¶

In [7]:
omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
omp.fit(X, y)
coef = omp.coef_
idx_r, = coef.nonzero()

In [8]:
data=[]

trace1 = go.Scatter(x=idx_r, y=coef[idx_r], mode='markers',
marker=dict(color='blue'),
showlegend=False)
data.append(trace1)

for i in range(0, len(idx)):
trace = go.Scatter(x= [idx_r[i], idx_r[i]], y=[0, coef[idx_r][i]],
mode='lines',
line=dict(color='blue', width=1), showlegend=False
)
data.append(trace)

layout = go.Layout(title='Recovered Signal From Noise-Free Measurements',
hovermode='closest')
fig = go.Figure(data=data, layout=layout)

In [9]:
py.iplot(fig)

Out[9]:

### Plot the Noisy Reconstruction With Number of Non-Zeros Set by CV¶

In [10]:
omp.fit(X, y_noisy)
coef = omp.coef_
idx_r, = coef.nonzero()

In [11]:
data=[]

trace1 = go.Scatter(x=idx_r, y=coef[idx_r], mode='markers',
marker=dict(color='blue'),
showlegend=False)
data.append(trace1)

for i in range(0, len(idx)):
trace = go.Scatter(x= [idx_r[i], idx_r[i]], y=[0, coef[idx_r][i]],
mode='lines',
line=dict(color='blue', width=1),
showlegend=False
)
data.append(trace)

layout = go.Layout(title='Recovered signal from noisy measurements with CV',
hovermode='closest')
fig = go.Figure(data=data, layout=layout)

In [12]:
py.iplot(fig)

Out[12]:
Still need help?