Normal, Ledoit-Wolf and OAS Linear Discriminant Analysis for classification
OAS
from
sklearn.datasets
import
from
sklearn.discriminant_analysis
import
n_train
=
20
n_test
=
200
n_averages
=
50
n_features_max
=
75
step
=
4
def
generate_data
(
n_samples
,
n_features
):
"""Generate random blob-ish data with noisy features.
This returns an array of input data with shape `(n_samples, n_features)`
and an array of `n_samples` target labels.
Only one feature contains discriminative information, the other features
contain only noise.
"""
X
,
y
=
(
n_samples
=
n_samples
,
n_features
=
1
,
centers
=
[[
-
2
],
[
2
]])
if
n_features
>
1
:
X
=
([
X
,
(
n_samples
,
n_features
-
1
)])
return
X
,
y
acc_clf1
,
acc_clf2
,
acc_clf3
=
[],
[],
[]
n_features_range
=
range
(
1
,
n_features_max
+
1
,
step
)
for
n_features
in
n_features_range
:
score_clf1
,
score_clf2
,
score_clf3
=
0
,
0
,
0
for
_
in
range
(
n_averages
):
X
,
y
=
generate_data
(
n_train
,
n_features
)
clf1
=
(
solver
=
"lsqr"
,
shrinkage
=
None
)
.
fit
(
X
,
y
)
clf2
=
(
solver
=
"lsqr"
,
shrinkage
=
"auto"
)
.
fit
(
X
,
y
)
oa
=
(
store_precision
=
False
,
assume_centered
=
False
)
clf3
=
(
solver
=
"lsqr"
,
covariance_estimator
=
oa
)
.
fit
(
X
,
y
)
X
,
y
=
generate_data
(
n_test
,
n_features
)
score_clf1
+=
clf1
.
score
(
X
,
y
)
score_clf2
+=
clf2
.
score
(
X
,
y
)
score_clf3
+=
clf3
.
score
(
X
,
y
)
acc_clf1
.
append
(
score_clf1
/
n_averages
)
acc_clf2
.
append
(
score_clf2
/
n_averages
)
acc_clf3
.
append
(
score_clf3
/
n_averages
)
features_samples_ratio
=
(
n_features_range
)
/
n_train
(
features_samples_ratio
,
acc_clf1
,
linewidth
=
2
,
label
=
"LDA"
,
color
=
"gold"
,
linestyle
=
"solid"
,
)
(
features_samples_ratio
,
acc_clf2
,
linewidth
=
2
,
label
=
"LDA with Ledoit Wolf"
,
color
=
"navy"
,
linestyle
=
"dashed"
,
)
(
features_samples_ratio
,
acc_clf3
,
linewidth
=
2
,
label
=
"LDA with OAS"
,
color
=
"red"
,
linestyle
=
"dotted"
,
)
(
"n_features / n_samples"
)
(
"Classification accuracy"
)
(
loc
=
"lower left"
)
((
0.65
,
1.0
))
(
"LDA (Linear Discriminant Analysis) vs."
"
\n
"
"LDA with Ledoit Wolf vs."
"
\n
"
"LDA with OAS (1 discriminative feature)"
)
()
Total running time of the script: (0 minutes 9.988 seconds)
Related examples