8000 PEP8 conformity improved · scikit-learn/scikit-learn@fa582b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa582b4

Browse files
author
Jan Hendrik Metzen
committed
PEP8 conformity improved
DOC Add "matern" as valid kernel name in various places in the doc MISC Revising Matern kernel PR based on @agramfort's review
1 parent bb8bfbb commit fa582b4

File tree

4 files changed

+62
-62
lines changed

4 files changed

+62
-62
lines changed

examples/metrics/plot_matern_kernel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222

2323
import numpy as np
2424

25-
from sklearn.metrics.pairwise import matern_kernel
26-
2725
import matplotlib.pyplot as plt
2826

27+
from sklearn.metrics.pairwise import matern_kernel
28+
2929
d = np.linspace(-4, 4, 500)[:, None]
3030

3131
for coef0 in [0.5, 1.5, 2.5, np.inf]:
32-
K = matern_kernel(d, [[0.0]], gamma=1, coef0=coef0)
33-
plt.plot(d[:, 0], K[:, 0], label=coef0)
32+
K = matern_kernel(d, [[0.0]], gamma=1, coef0=coef0)
33+
plt.plot(d[:, 0], K[:, 0], label=coef0)
3434

3535
plt.xlabel("distance")
3636
plt.ylabel("covariance")
3737
plt.yscale("log")
3838
plt.ylim(1e-3, 1e0)
3939
plt.legend(title="coef0")
40-
plt.show()
40+
plt.show()

examples/svm/plot_svm_matern_kernel.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
is compared on a (discontinuous) step-function:
1111
* The Matern kernel for coef0==1.5, learning a once differentiable function
1212
* The Matern kernel for coef0==2.5, learning a twice differentiable function
13-
* The Matern kernel for coef0==3.5, learning a three-times differentiable
13+
* The Matern kernel for coef0==3.5, learning a three-times differentiable
1414
function
1515
* The absolute-exponential kernel which corresponds to a Matern kernel
1616
with coef0==0.5
@@ -31,10 +31,12 @@
3131
from functools import partial
3232

3333
import numpy as np
34+
35+
import matplotlib.pyplot as plt
36+
3437
from sklearn.svm import NuSVR
3538
from sklearn.metrics.pairwise import matern_kernel
3639

37-
import matplotlib.pyplot as plt
3840

3941
np.random.seed(0)
4042

sklearn/decomposition/kernel_pca.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class KernelPCA(BaseEstimator, TransformerMixin):
2424
n_components: int or None
2525
Number of components. If None, all non-zero components are kept.
2626
27-
kernel: "linear" | "poly" | "rbf" | "sigmoid" | "cosine" | "precomputed"
27+
kernel: "linear" | "poly" | "rbf" | "sigmoid" | "cosine" | "matern" |
28+
"precomputed"
2829
Kernel.
2930
Default: "linear"
3031

sklearn/metrics/pairwise.py

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# License: BSD 3 clause
1212

1313
import itertools
14+
import math
1415

1516
import numpy as np
1617
from scipy.spatial import distance
@@ -508,11 +509,9 @@ def cosine_distances(X, Y=None):
508509
509510
Parameters
510511
----------
511-
X : array_like, sparse matrix
512-
with shape (n_samples_X, n_features).
512+
X : array_like, sparse matrix, shape (n_samples_X, n_features).
513513
514-
Y : array_like, sparse matrix (optional)
515-
with shape (n_samples_Y, n_features).
514+
Y : array_like, sparse matrix (optional), shape (n_samples_Y, n_features).
516515
517516
Returns
518517
-------
@@ -613,10 +612,10 @@ def paired_distances(X, Y, metric="euclidean", **kwds):
613612
614613
Parameters
615614
----------
616-
X : ndarray (n_samples, n_features)
615+
X : array, shape (n_samples, n_features)
617616
Array 1 for distance computation.
618617
619-
Y : ndarray (n_samples, n_features)
618+
Y : array, shape (n_samples, n_features)
620619
Array 2 for distance computation.
621620
622621
metric : string or callable
@@ -631,7 +630,7 @@ def paired_distances(X, Y, metric="euclidean", **kwds):
631630
632631
Returns
633632
-------
634-
distances : ndarray (n_samples, )
633+
distances : array, shape (n_samples, )
635634
636635
Examples
637636
--------
@@ -667,13 +666,13 @@ def linear_kernel(X, Y=None):
667666
668667
Parameters
669668
----------
670-
X : array of shape (n_samples_1, n_features)
669+
X : array, shape (n_samples_X, n_features)
671670
672-
Y : array of shape (n_samples_2, n_features)
671+
Y : array, shape (n_samples_Y, n_features)
673672
674673
Returns
675674
-------
676-
Gram matrix : array of shape (n_samples_1, n_samples_2)
675+
Gram matrix : array, shape (n_samples_X, n_samples_Y)
677676
"""
678677
X, Y = check_pairwise_arrays(X, Y)
679678
return safe_sparse_dot(X, Y.T, dense_output=True)
@@ -687,17 +686,17 @@ def polynomial_kernel(X, Y=None, degree=3, gamma=None, coef0=1):
687686
688687
Parameters
689688
----------
690-
X : ndarray of shape (n_samples_1, n_features)
689+
X : array, shape (n_samples_X, n_features)
691690
692-
Y : ndarray of shape (n_samples_2, n_features)
691+
Y : array, shape (n_samples_Y, n_features)
693692
694693
coef0 : int, default 1
695694
696695
degree : int, default 3
697696
698697
Returns
699698
-------
700-
Gram matrix : array of shape (n_samples_1, n_samples_2)
699+
Gram matrix : array, shape (n_samples_X, n_samples_Y)
701700
"""
702701
X, Y = check_pairwise_arrays(X, Y)
703702
if gamma is None:
@@ -718,15 +717,15 @@ def sigmoid_kernel(X, Y=None, gamma=None, coef0=1):
718717
719718
Parameters
720719
----------
721-
X : ndarray of shape (n_samples_1, n_features)
720+
X : array, shape (n_samples_X, n_features)
722721
723-
Y : ndarray of shape (n_samples_2, n_features)
722+
Y : array, shape (n_samples_Y, n_features)
724723
725724
coef0 : int, default 1
726725
727726
Returns
728727
-------
729-
Gram matrix: array of shape (n_samples_1, n_samples_2)
728+
Gram matrix: array, shape (n_samples_X, n_samples_Y)
730729
"""
731730
X, Y = check_pairwise_arrays(X, Y)
732731
if gamma is None:
@@ -749,15 +748,15 @@ def rbf_kernel(X, Y=None, gamma=None):
749748
750749
Parameters
751750
----------
752-
X : array of shape (n_samples_X, n_features)
751+
X : array, shape (n_samples_X, n_features)
753752
754-
Y : array of shape (n_samples_Y, n_features)
753+
Y : array, shape (n_samples_Y, n_features)
755754
756755
gamma : float
757756
758757
Returns
759758
-------
760-
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
759+
kernel_matrix : array, shape (n_samples_X, n_samples_Y)
761760
"""
762761
X, Y = check_pairwise_arrays(X, Y)
763762
if gamma is None:
@@ -785,27 +784,27 @@ def matern_kernel(X, Y=None, gamma=None, coef0=1.5):
785784
786785
Parameters
787786
----------
788-
X : array of shape (n_samples_X, n_features)
787+
X : array, shape (n_samples_X, n_features)
789788
790-
Y : array of shape (n_samples_Y, n_features)
789+
Y : array, shape (n_samples_Y, n_features)
791790
792791
gamma : float
793792
794793
coef0 : float>0.0 (the parameter nu)
795794
The parameter nu controlling the smoothness of the learned function.
796-
The smaller coef0, the less smooth the approximated function is.
797-
For nu=inf, the kernel becomes equivalent to the RBF kernel and for
798-
nu=0.5 to the absolute exponential kernel. Important intermediate
799-
values are nu=1.5 (once differentiable functions) and nu=2.5
800-
(twice differentiable functions). Note that values of nu not in
795+
The smaller coef0, the less smooth the approximated function is.
796+
For nu=inf, the kernel becomes equivalent to the RBF kernel and for
797+
nu=0.5 to the absolute exponential kernel. Important intermediate
798+
values are nu=1.5 (once differentiable functions) and nu=2.5
799+
(twice differentiable functions). Note that values of nu not in
801800
[0.5, 1.5, 2.5, inf] incur a considerably higher computational cost
802801
(appr. 10 times higher) since they require to evaluate the modified
803802
Bessel function.
804803
805804
806805
Returns
807806
-------
808-
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
807+
kernel_matrix : array, shape (n_samples_X, n_samples_Y)
809808
"""
810809
if coef0 == np.inf: # fall back to rbf-kernel
811810
return rbf_kernel(X, Y, gamma)
@@ -819,17 +818,17 @@ def matern_kernel(X, Y=None, gamma=None, coef0=1.5):
819818
K = euclidean_distances(X, Y, squared=False)
820819
if coef0 == 0.5:
821820
K *= -gamma
822-
np.exp(K, K) # exponentiate K in-place
821+
np.exp(K, K) # exponentiate K in-place
823822
elif coef0 == 1.5:
824-
K *= np.sqrt(3) * gamma
825-
K = (1 + K) * np.exp(-K)
823+
K *= math.sqrt(3) * gamma
824+
K = (1. + K) * np.exp(-K)
826825
elif coef0 == 2.5:
827-
K *= np.sqrt(5) * gamma
828-
K = (1 + K + K ** 2 / 3.0) * np.exp(-K)
826+
K *= math.sqrt(5) * gamma
827+
K = (1. + K + K ** 2 / 3.0) * np.exp(-K)
829828
else: # general case; expensive to evaluate
830829
K[K == 0.0] += np.finfo(float).eps # strict zeros would result in nan
831-
tmp = (np.sqrt(2 * coef0) * gamma * K)
832-
K[:] = (2 ** (1 - coef0)) / scipy.special.gamma(coef0)
830+
tmp = (math.sqrt(2 * coef0) * gamma * K)
831+
K[:] = (2 ** (1. - coef0)) / scipy.special.gamma(coef0)
833832
K *= tmp ** coef0
834833
K *= scipy.special.kv(coef0, tmp)
835834
return K
@@ -847,16 +846,13 @@ def cosine_similarity(X, Y=None):
847846
848847
Parameters
849848
----------
850-
X : array_like, sparse matrix
851-
with shape (n_samples_X, n_features).
849+
X : array_like, sparse matrix, shape (n_samples_X, n_features).
852850
853-
Y : array_like, sparse matrix (optional)
854-
with shape (n_samples_Y, n_features).
851+
Y : array_like, sparse matrix (optional), shape (n_samples_Y, n_features).
855852
856853
Returns
857854
-------
858-
kernel matrix : array
859-
An array with shape (n_samples_X, n_samples_Y).
855+
kernel matrix : array, shape (n_samples_X, n_samples_Y).
860856
"""
861857
# to avoid recursive import
862858

@@ -894,13 +890,13 @@ def additive_chi2_kernel(X, Y=None):
894890
895891
Parameters
896892
----------
897-
X : array-like of shape (n_samples_X, n_features)
893+
X : array, shape (n_samples_X, n_features)
898894
899-
Y : array of shape (n_samples_Y, n_features)
895+
Y : array, shape (n_samples_Y, n_features)
900896
901897
Returns
902898
-------
903-
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
899+
kernel_matrix : array, shape (n_samples_X, n_samples_Y)
904900
905901
References
906902
----------
@@ -947,16 +943,16 @@ def chi2_kernel(X, Y=None, gamma=1.):
947943
948944
Parameters
949945
----------
950-
X : array-like of shape (n_samples_X, n_features)
946+
X : array, shape (n_samples_X, n_features)
951947
952-
Y : array of shape (n_samples_Y, n_features)
948+
Y : array, shape (n_samples_Y, n_features)
953949
954950
gamma : float, default=1.
955951
Scaling parameter of the chi2 kernel.
956952
957953
Returns
958954
-------
959-
kernel_matrix : array of shape (n_samples_X, n_samples_Y)
955+
kernel_matrix : array, shape (n_samples_X, n_samples_Y)
960956
961957
References
962958
----------
@@ -1111,11 +1107,11 @@ def pairwise_distances(X, Y=None, metric="euclidean", n_jobs=1, **kwds):
11111107
11121108
Parameters
11131109
----------
1114-
X : array [n_samples_a, n_samples_a] if metric == "precomputed", or, \
1115-
[n_samples_a, n_features] otherwise
1110+
X : array, shape (n_samples_a, n_samples_a) if metric == "precomputed", \
1111+
(n_samples_a, n_features) otherwise
11161112
Array of pairwise distances between samples, or a feature array.
11171113
1118-
Y : array [n_samples_b, n_features]
1114+
Y : array, shape (n_samples_b, n_features)
11191115
A second feature array only if X has shape [n_samples_a, n_features].
11201116
11211117
metric : string, or callable
@@ -1146,7 +1142,7 @@ def pairwise_distances(X, Y=None, metric="euclidean", n_jobs=1, **kwds):
11461142
11471143
Returns
11481144
-------
1149-
D : array [n_samples_a, n_samples_a] or [n_samples_a, n_samples_b]
1145+
D : array, shape (n_samples_a, n_samples_a) or (n_samples_a, n_samples_b)
11501146
A distance matrix D such that D_{i, j} is the distance between the
11511147
ith and jth vectors of the given matrix X, if Y is None.
11521148
If Y is not None, then D_{i, j} is the distance between the ith array
@@ -1212,6 +1208,7 @@ def kernel_metrics():
12121208
'rbf' sklearn.pairwise.rbf_kernel
12131209
'sigmoid' sklearn.pairwise.sigmoid_kernel
12141210
'cosine' sklearn.pairwise.cosine_similarity
1211+
'matern' sklearn.pairwise.matern_kernel
12151212
=============== ========================================
12161213
"""
12171214
return PAIRWISE_KERNEL_FUNCTIONS
@@ -1247,12 +1244,12 @@ def pairwise_kernels(X, Y=None, metric="linear", filter_params=False,
12471244
kernel between the arrays from both X and Y.
12481245
12491246
Valid values for metric are::
1250-
['rbf', 'sigmoid', 'polynomial', 'poly', 'linear', 'cosine']
1247+
['rbf', 'sigmoid', 'polynomial', 'poly', 'linear', 'cosine', 'matern']
12511248
12521249
Parameters
12531250
----------
1254-
X : array [n_samples_a, n_samples_a] if metric == "precomputed", or, \
1255-
[n_samples_a, n_features] otherwise
1251+
X : array, shape (n_samples_a, n_samples_a) if metric == "precomputed", \
1252+
(n_samples_a, n_features) otherwise
12561253
Array of pairwise kernels between samples, or a feature array.
12571254
12581255
Y : array [n_samples_b, n_features]
@@ -1286,7 +1283,7 @@ def pairwise_kernels(X, Y=None, metric="linear", filter_params=False,
12861283
12871284
Returns
12881285
-------
1289-
K : array [n_samples_a, n_samples_a] or [n_samples_a, n_samples_b]
1286+
K : array, shape (n_samples_a, n_samples_a) or (n_samples_a, n_samples_b)
12901287
A kernel matrix K such that K_{i, j} is the kernel between the
12911288
ith and jth vectors of the given matrix X, if Y is None.
12921289
If Y is not None, then K_{i, j} is the kernel between the ith array

0 commit comments

Comments
 (0)
0