GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Install
  • User Guide
  • API
  • Examples
  • Community
  • Getting Started
  • Release History
  • Glossary
  • Development
  • FAQ
  • Support
  • Related Projects
  • Roadmap
  • Governance

This view

  • Original page
  • New address

GeezerVison is showing scikit-learn.org — Orthogonal Matching Pursuit

Note

Go to the end to download the full example code or to run this example in your browser via JupyterLite or Binder.

Orthogonal Matching Pursuit#

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

Sparse signal recovery with Orthogonal Matching Pursuit, Sparse signal, Recovered signal from noise-free measurements, Recovered signal from noisy measurements, Recovered signal from noisy measurements with CV
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt
import numpy as np

from sklearn.datasets import make_sparse_coded_signal
from sklearn.linear_model import OrthogonalMatchingPursuit, OrthogonalMatchingPursuitCV

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,
)
X = X.T

(idx,) = w.nonzero()

# distort the clean signal
y_noisy = y + 0.05 * np.random.randn(len(y))

# plot the sparse signal
plt.figure(figsize=(7, 7))
plt.subplot(4, 1, 1)
plt.xlim(0, 512)
plt.title("Sparse signal")
plt.stem(idx, w[idx])

# plot the noise-free reconstruction
omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
omp.fit(X, y)
coef = omp.coef_
(idx_r,) = coef.nonzero()
plt.subplot(4, 1, 2)
plt.xlim(0, 512)
plt.title("Recovered signal from noise-free measurements")
plt.stem(idx_r, coef[idx_r])

# plot the noisy reconstruction
omp.fit(X, y_noisy)
coef = omp.coef_
(idx_r,) = coef.nonzero()
plt.subplot(4, 1, 3)
plt.xlim(0, 512)
plt.title("Recovered signal from noisy measurements")
plt.stem(idx_r, coef[idx_r])

# plot the noisy reconstruction with number of non-zeros set by CV
omp_cv = OrthogonalMatchingPursuitCV()
omp_cv.fit(X, y_noisy)
coef = omp_cv.coef_
(idx_r,) = coef.nonzero()
plt.subplot(4, 1, 4)
plt.xlim(0, 512)
plt.title("Recovered signal from noisy measurements with CV")
plt.stem(idx_r, coef[idx_r])

plt.subplots_adjust(0.06, 0.04, 0.94, 0.90, 0.20, 0.38)
plt.suptitle("Sparse signal recovery with Orthogonal Matching Pursuit", fontsize=16)
plt.show()

Total running time of the script: (0 minutes 0.228 seconds)

Launch binder
Launch JupyterLite

Download Jupyter notebook: plot_omp.ipynb

Download Python source code: plot_omp.py

Download zipped: plot_omp.zip

Related examples

Blind source separation using FastICA

Blind source separation using FastICA

Feature agglomeration vs. univariate selection

Feature agglomeration vs. univariate selection

Joint feature selection with multi-task Lasso

Joint feature selection with multi-task Lasso

Plot multi-class SGD on the iris dataset

Plot multi-class SGD on the iris dataset

Gallery generated by Sphinx-Gallery

Download source code
Download Jupyter notebook
Download zipped
Launch JupyterLite
Launch binder

scikit-learn is financially supported by Probabl and other institutions.

Enterprise-grade solutions and services

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.