线性SVR#
- class sklearn.svm.LinearSVR(*, epsilon=0.0, tol=0.0001, C=1.0, loss='epsilon_insensitive', fit_intercept=True, intercept_scaling=1.0, dual='auto', verbose=0, random_state=None, max_iter=1000)[源代码]#
线性支持向量回归机
与参数core =' linear '的SVR类似,但以liblinear而不是libsvm的方式实现,因此它在惩罚和损失函数的选择方面具有更大的灵活性,并且应该更好地扩展到大量样本。
的主要区别
LinearSVR
和SVR
取决于默认使用的损失函数,以及这两个实现之间的拦截正规化的处理。此类支持密集和稀疏输入。
阅读更多的 User Guide .
Added in version 0.16.
- 参数:
- epsilonfloat,默认=0.0
不敏感损失函数中的Epsilon参数。请注意,此参数的值取决于目标变量y的大小。如果不确定,请设置
epsilon=0
.- tolfloat,默认= 1 e-4
停止标准的容忍度。
- Cfloat,默认=1.0
正规化参数。正规化的强度与C成正比。必须严格积极。
- loss' 默认='_麻木不仁'
指定损失函数。epsilon不敏感损失(标准SVR)是L1损失,而epsilon不敏感损失的平方(“squared_ð_insensitive”)是L2损失。
- fit_intercept布尔,默认=True
是否适合拦截。如果设置为True,则特征载体将扩展为包括拦截项:
[x_1, ..., x_n, 1]
,其中1对应于截取。如果设置为False,则计算中不会使用任何拦截(即数据预计已居中)。- intercept_scalingfloat,默认=1.0
当
fit_intercept
为True,实例载体x变成[x_1, ..., x_n, intercept_scaling]
,即“合成”特征,其常值等于intercept_scaling
被附加到实例载体。拦截变成intercept_scaling * synthetic feature weight. Note that liblinear internally penalizes the intercept, treating it like any other term in the feature vector. To reduce the impact of the regularization on the intercept, theintercept_scaling
parameter can be set to a value greater than 1; the higher the value ofintercept_scaling
, the lower the impact of regularization on it. Then, the weights become[w_x_1, ..., w_x_n, w_intercept* intersection_scaling]',其中 `w_x_1, ..., w_x_n
表示特征权重,截取权重通过以下方式进行缩放intercept_scaling
.与其他特征相比,这种缩放允许拦截项具有不同的正规化行为。- dual“Auto”或布尔,默认=“Auto”
选择算法来解决二元或原始优化问题。当n_samples > n_features时,首选dual=False。
dual="auto"
将根据值自动选择参数的值n_samples
,n_features
和loss
.如果n_samples
<n_features
和优化器支持选择loss
,则dual将设置为True,否则将设置为False。在 1.3 版本发生变更: 的
"auto"
选项在1.3版本中添加,并在1.5版本中成为默认选项。- verboseint,默认=0
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in liblinear that, if enabled, may not work properly in a multithreaded context.
- random_stateint,RandomState实例或无,默认=无
控制用于洗牌数据的伪随机数生成。传递一个int值,以便在多个函数调用中获得可重复的输出。看到 Glossary .
- max_iterint,默认=1000
要运行的最大迭代次数。
- 属性:
- coef_如果n_classes == 2,则形状(n_features)nd数组 else(n_classes,n_features)
分配给特征的权重(原始问题中的系数)。
coef_
是派生自的只读属性raw_coef_
这遵循liblinear的内存布局。- intercept_如果n_classes == 2,则形状(1)的nd数组否则(n_classes)
决策功能中的常数。
- n_features_in_int
期间看到的功能数量 fit .
Added in version 0.24.
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
- n_iter_int
所有类中运行的最大迭代次数。
参见
LinearSVC
使用与此类相同的库(liblinear)实现支持载体机分类器。
SVR
使用libsvm实现支持向量机回归:内核可以是非线性的,但其MMO算法无法扩展到大量样本,
LinearSVR
可以这sklearn.linear_model.SGDRegressor
SGDRegressor可以通过调整惩罚和损失参数来优化与LinearSVR相同的成本函数。此外,它需要更少的内存,允许增量(在线)学习,并实现各种损失函数和正规化制度。
示例
>>> from sklearn.svm import LinearSVR >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.datasets import make_regression >>> X, y = make_regression(n_features=4, random_state=0) >>> regr = make_pipeline(StandardScaler(), ... LinearSVR(random_state=0, tol=1e-5)) >>> regr.fit(X, y) Pipeline(steps=[('standardscaler', StandardScaler()), ('linearsvr', LinearSVR(random_state=0, tol=1e-05))])
>>> print(regr.named_steps['linearsvr'].coef_) [18.582... 27.023... 44.357... 64.522...] >>> print(regr.named_steps['linearsvr'].intercept_) [-4...] >>> print(regr.predict([[0, 0, 0, 0]])) [-2.384...]
- fit(X, y, sample_weight=None)[源代码]#
根据给定的训练数据对模型进行匹配。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的数量。- y形状类似阵列(n_samples,)
相对于X的目标载体。
- sample_weight形状类似数组(n_samples,),默认=无
分配给各个样本的权重数组。如果未提供,则给予每个样本单位重量。
Added in version 0.18.
- 返回:
- self对象
估计器的一个实例。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- predict(X)[源代码]#
Predict using the linear model.
- 参数:
- X类阵列或稀疏矩阵,形状(n_samples,n_features)
样品
- 返回:
- C数组,形状(n_samples,)
返回预测值。
- score(X, y, sample_weight=None)[源代码]#
返回预测的决定系数。
决定系数 \(R^2\) 被定义为 \((1 - \frac{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形状类似阵列(n_samples,n_features)
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted)
, wheren_samples_fitted
is the number of samples used in the fitting for the estimator.- y形状的类似阵列(n_samples,)或(n_samples,n_outputs)
真正的价值观
X
.- sample_weight形状类似数组(n_samples,),默认=无
样本重量。
- 返回:
- score浮子
\(R^2\) 的
self.predict(X)
w.r.t.y
.
注意到
的 \(R^2\) 呼叫时使用的分数
score
在回归器上使用multioutput='uniform_average'
从0.23版本开始,与默认值保持一致r2_score
.这影响了score
所有多输出回归器的方法(除了MultiOutputRegressor
).
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LinearSVR [源代码]#
请求元数据传递给
fit
法请注意,此方法仅适用于以下情况
enable_metadata_routing=True
(见sklearn.set_config
).请参阅 User Guide 关于路由机制如何工作。The options for each parameter are:
True
:元数据被请求并传递给fit
如果提供的话。如果未提供元数据,则会忽略请求。False
:未请求元数据,元估计器不会将其传递给fit
.None
:不请求元数据,如果用户提供元估计器,则元估计器将引发错误。str
:元数据应通过此给定别名而不是原始名称传递给元估计器。
默认 (
sklearn.utils.metadata_routing.UNCHANGED
)保留现有请求。这允许您更改某些参数的请求,而不是其他参数。Added in version 1.3.
备注
只有当该估计器用作元估计器的子估计器时,该方法才相关,例如在
Pipeline
.否则就没有效果了。- 参数:
- sample_weight字符串、真、假或无, 默认=sklearn.utils. metalics_Routing.UNChanged
元数据路由
sample_weight
参数fit
.
- 返回:
- self对象
更新的对象。
- set_params(**params)[源代码]#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
).后者具有以下形式的参数<component>__<parameter>
以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计参数。
- 返回:
- self估计器实例
估计实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LinearSVR [源代码]#
请求元数据传递给
score
法请注意,此方法仅适用于以下情况
enable_metadata_routing=True
(见sklearn.set_config
).请参阅 User Guide 关于路由机制如何工作。The options for each parameter are:
True
:元数据被请求并传递给score
如果提供的话。如果未提供元数据,则会忽略请求。False
:未请求元数据,元估计器不会将其传递给score
.None
:不请求元数据,如果用户提供元估计器,则元估计器将引发错误。str
:元数据应通过此给定别名而不是原始名称传递给元估计器。
默认 (
sklearn.utils.metadata_routing.UNCHANGED
)保留现有请求。这允许您更改某些参数的请求,而不是其他参数。Added in version 1.3.
备注
只有当该估计器用作元估计器的子估计器时,该方法才相关,例如在
Pipeline
.否则就没有效果了。- 参数:
- sample_weight字符串、真、假或无, 默认=sklearn.utils. metalics_Routing.UNChanged
元数据路由
sample_weight
参数score
.
- 返回:
- self对象
更新的对象。