Scaling the regularization parameter for SVCs
C is used to set the amount of regularization
L is a loss function of our samples and our model parameters.
Ω is a penalty function of our model parameters
If we consider the loss function to be the individual error per sample, then the
data-fit term, or the sum of the error for each sample, increases as we add more
samples. The penalization term, however, does not increase.
When using, for example, cross validation, to set the
amount of regularization with C, there would be a different amount of samples
between the main problem and the smaller problems within the folds of the cross
validation.
Since the loss function depends on the amount of samples, the latter
influences the selected value of C. The question that arises is “How do we
optimally adjust C to account for the different amount of training samples?”
2.3
,
-
1.3
,
10
)
train_sizes
=
(
0.3
,
0.7
,
3
)
labels
=
[
f
"fraction:
{
train_size
}
"
for
train_size
in
train_sizes
]
shuffle_params
=
{
"test_size"
:
0.3
,
"n_splits"
:
150
,
"random_state"
:
1
,
}
results
=
{
"C"
:
Cs
}
for
label
,
train_size
in
zip
(
labels
,
train_sizes
):
cv
=
(
train_size
=
train_size
,
**
shuffle_params
)
train_scores
,
test_scores
=
(
model_l1
,
X
,
y
,
param_name
=
"C"
,
param_range
=
Cs
,
cv
=
cv
,
n_jobs
=
2
,
)
results
[
label
]
=
test_scores
.
mean
(
axis
=
1
)
results
=
(
results
)
results
.
plot
(
x
=
"C"
,
ax
=
axes
[
0
],
logx
=
True
)
axes
[
0
]
.
set_ylabel
(
"CV score"
)
axes
[
0
]
.
set_title
(
"No scaling"
)
for
label
in
labels
:
best_C
=
results
.
loc
[
results
[
label
]
.
idxmax
(),
"C"
]
axes
[
0
]
.
axvline
(
x
=
best_C
,
linestyle
=
"--"
,
color
=
"grey"
,
alpha
=
0.7
)
for
train_size_idx
,
label
in
enumerate
(
labels
):
train_size
=
train_sizes
[
train_size_idx
]
results_scaled
=
results
[[
label
]]
.
assign
(
C_scaled
=
Cs
*
float
(
n_samples
*
(
train_size
))
)
results_scaled
.
plot
(
x
=
"C_scaled"
,
ax
=
axes
[
1
],
logx
=
True
,
label
=
label
)
best_C_scaled
=
results_scaled
[
"C_scaled"
]
.
loc
[
results
[
label
]
.
idxmax
()]
axes
[
1
]
.
axvline
(
x
=
best_C_scaled
,
linestyle
=
"--"
,
color
=
"grey"
,
alpha
=
0.7
)
axes
[
1
]
.
set_title
(
"Scaling C by sqrt(1 / n_samples)"
)
_
=
fig
.
suptitle
(
"Effect of scaling C with L1 penalty"
)
.
plot
(
x
=
"C"
,
ax
=
axes
[
0
],
logx
=
True
)
axes
[
0
]
.
set_ylabel
(
"CV score"
)
axes
[
0
]
.
set_title
(
"No scaling"
)
for
label
in
labels
:
best_C
=
results
.
loc
[
results
[
label
]
.
idxmax
(),
"C"
]
axes
[
0
]
.
axvline
(
x
=
best_C
,
linestyle
=
"--"
,
color
=
"grey"
,
alpha
=
0.8
)
for
train_size_idx
,
label
in
enumerate
(
labels
):
results_scaled
=
results
[[
label
]]
.
assign
(
C_scaled
=
Cs
*
float
(
n_samples
*
(
train_sizes
[
train_size_idx
]))
)
results_scaled
.
plot
(
x
=
"C_scaled"
,
ax
=
axes
[
1
],
logx
=
True
,
label
=
label
)
best_C_scaled
=
results_scaled
[
"C_scaled"
]
.
loc
[
results
[
label
]
.
idxmax
()]
axes
[
1
]
.
axvline
(
x
=
best_C_scaled
,
linestyle
=
"--"
,
color
=
"grey"
,
alpha
=
0.8
)
axes
[
1
]
.
set_title
(
"Scaling C by sqrt(1 / n_samples)"
)
fig
.
suptitle
(
"Effect of scaling C with L2 penalty"
)
()
Download zipped: plot_svm_scale_c.zip