.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_omp.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_omp.py: 正交匹配追踪 =========================== 使用正交匹配追踪从带噪测量中恢复稀疏信号,该测量使用字典进行编码 .. GENERATED FROM PYTHON SOURCE LINES 8-78 .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_omp_001.png :alt: 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 :srcset: /auto_examples/linear_model/images/sphx_glr_plot_omp_001.png :class: sphx-glr-single-img .. code-block:: Python 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 # 生成数据 #``` # 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() # 扭曲干净的信号 y_noisy = y + 0.05 * np.random.randn(len(y)) # 绘制稀疏信号 plt.figure(figsize=(7, 7)) plt.subplot(4, 1, 1) plt.xlim(0, 512) plt.title("Sparse signal") plt.stem(idx, w[idx]) # 绘制无噪声重建图 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]) # 绘制有噪声的重建图 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]) # 绘制由交叉验证设定非零数目的噪声重建图 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() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.093 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_omp.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/linear_model/plot_omp.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_omp.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_omp.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_omp.zip ` .. include:: plot_omp.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_