OrthogonalMatchingPursuit
sklearn.linear_model.OrthogonalMatchingPursuit
class sklearn.linear_model.OrthogonalMatchingPursuit(*, n_nonzero_coefs=None, tol=None,
fit_intercept=True, normalize=True, precompute='auto')
参数:
n_nonzero_coefs:int, optional
解决方案中所需的非零条目数。如果没有(默认情况下),则此值设置为n_要素的10%。
tol:float, optional
残差的最大范数。
fit_intercept:boolean, optional
是否计算此模型的截距。如果设置为false,则计算中将不使用截距(即数据应居中)。
normalize:boolean, optional, default True
当fit_intercept设置为False时,忽略此参数。如果为真,则回归前,通过减去平均值并除以l2范数,
对回归数X进行归一化。
precompute:{True, False, ‘auto’}, default ‘auto’
是否使用预计算的程序和Xy矩阵加快计算速度。
属性:
coef_:array, shape (n_features,) or (n_targets, n_features)
参数向量(公式中的w)
intercept_:float or array, shape (n_targets,)
决策函数中的独立项。
n_iter_:int or array-like
每个目标上的活动功能数。
另见:
orthogonal_mp
orthogonal_mp_gram
lars_path
Lars
LassoLars
decomposition.sparse_encode
OrthogonalMatchingPursuitCV
例子:
>>> from sklearn.linear_model import OrthogonalMatchingPursuit
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(noise=4, random_state=0)
>>> reg = OrthogonalMatchingPursuit().fit(X, y)
>>> reg.score(X, y)
0.9991...
>>> reg.predict(X[:1,])
array([-78.3854...])
方法:
__init__(self, *, n_nonzero_coefs=None, tol=None, fit_intercept=True, normalize=True,
precompute='auto')
初始化自身。
fit(self, X, y, Xy=None)
使用X,y作为训练数据拟合模型。
参数:
X:array-like of shape (n_samples, n_features)
训练数据
Y:array-like of shape (n_samples,) or (n_samples, n_targets)
目标值
返回:
self:object
返回self的实例。
get_params(self, deep=True)
获取此估计器的参数。
参数:
deep:bool, default=True
如果为True,则返回此估计器的参数以及包含的子对象(即估计器)。
返回:
params:mapping of string to any
映射到其值的参数名。
predict(self, X)
参数:
X:array_like or sparse matrix, shape (n_samples, n_features)
返回:
C:array, shape (n_samples,)
返回预测值
score(self, X, y, sample_weight=None)
返回预测的决定系数R^2。
系数R^2定义为(1-u/v),其中u是残差平方和((y_true-y_pred)**2).sum(),v是平方和的总和
((y_true-y_true.mean())**2).sum()。最好的分数是1.0,它可以是负的(因为模型可以任意恶化)
一个常数模型总是预测y的期望值,而不考虑输入特性,则得到R^2分数为0.0。
参数:
X:array-like of shape (n_samples, n_features)
测试样本。
y:array-like of shape (n_samples,) or (n_samples, n_outputs)
X的真值。
sample_weight:array-like of shape (n_samples,), default=None
样本权重。
返回:
scorefloat
得分
set_params(self, **params)
设置此估计器的参数。
参数:
**params:dict
估计参数
返回:
Self:object
估计实例
例子
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/6/27 21:40
# @Author : LaoChen
"""
用正交匹配追踪法从带噪测量信号中提取稀疏信号
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import OrthogonalMatchingPursuit
from sklearn.linear_model import OrthogonalMatchingPursuitCV
from sklearn.datasets import make_sparse_coded_signal
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
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], use_line_collection=True)
# 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], use_line_collection=True)
# 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], use_line_collection=True)
# 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], use_line_collection=True)
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()