11
11
# License: BSD 3 clause
12
12
13
13
import itertools
14
+ import math
14
15
15
16
import numpy as np
16
17
from scipy .spatial import distance
@@ -508,11 +509,9 @@ def cosine_distances(X, Y=None):
508
509
509
510
Parameters
510
511
----------
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).
513
513
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).
516
515
517
516
Returns
518
517
-------
@@ -613,10 +612,10 @@ def paired_distances(X, Y, metric="euclidean", **kwds):
613
612
614
613
Parameters
615
614
----------
616
- X : ndarray (n_samples, n_features)
615
+ X : array, shape (n_samples, n_features)
617
616
Array 1 for distance computation.
618
617
619
- Y : ndarray (n_samples, n_features)
618
+ Y : array, shape (n_samples, n_features)
620
619
Array 2 for distance computation.
621
620
622
621
metric : string or callable
@@ -631,7 +630,7 @@ def paired_distances(X, Y, metric="euclidean", **kwds):
631
630
632
631
Returns
633
632
-------
634
- distances : ndarray (n_samples, )
633
+ distances : array, shape (n_samples, )
635
634
636
635
Examples
637
636
--------
@@ -667,13 +666,13 @@ def linear_kernel(X, Y=None):
667
666
668
667
Parameters
669
668
----------
670
- X : array of shape (n_samples_1 , n_features)
669
+ X : array, shape (n_samples_X , n_features)
671
670
672
- Y : array of shape (n_samples_2 , n_features)
671
+ Y : array, shape (n_samples_Y , n_features)
673
672
674
673
Returns
675
674
-------
676
- Gram matrix : array of shape (n_samples_1, n_samples_2 )
675
+ Gram matrix : array, shape (n_samples_X, n_samples_Y )
677
676
"""
678
677
X , Y = check_pairwise_arrays (X , Y )
679
678
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):
687
686
688
687
Parameters
689
688
----------
690
- X : ndarray of shape (n_samples_1 , n_features)
689
+ X : array, shape (n_samples_X , n_features)
691
690
692
- Y : ndarray of shape (n_samples_2 , n_features)
691
+ Y : array, shape (n_samples_Y , n_features)
693
692
694
693
coef0 : int, default 1
695
694
696
695
degree : int, default 3
697
696
698
697
Returns
699
698
-------
700
- Gram matrix : array of shape (n_samples_1, n_samples_2 )
699
+ Gram matrix : array, shape (n_samples_X, n_samples_Y )
701
700
"""
702
701
X , Y = check_pairwise_arrays (X , Y )
703
702
if gamma is None :
@@ -718,15 +717,15 @@ def sigmoid_kernel(X, Y=None, gamma=None, coef0=1):
718
717
719
718
Parameters
720
719
----------
721
- X : ndarray of shape (n_samples_1 , n_features)
720
+ X : array, shape (n_samples_X , n_features)
722
721
723
- Y : ndarray of shape (n_samples_2 , n_features)
722
+ Y : array, shape (n_samples_Y , n_features)
724
723
725
724
coef0 : int, default 1
726
725
727
726
Returns
728
727
-------
729
- Gram matrix: array of shape (n_samples_1, n_samples_2 )
728
+ Gram matrix: array, shape (n_samples_X, n_samples_Y )
730
729
"""
731
730
X , Y = check_pairwise_arrays (X , Y )
732
731
if gamma is None :
@@ -749,15 +748,15 @@ def rbf_kernel(X, Y=None, gamma=None):
749
748
750
749
Parameters
751
750
----------
752
- X : array of shape (n_samples_X, n_features)
751
+ X : array, shape (n_samples_X, n_features)
753
752
754
- Y : array of shape (n_samples_Y, n_features)
753
+ Y : array, shape (n_samples_Y, n_features)
755
754
756
755
gamma : float
757
756
758
757
Returns
759
758
-------
760
- kernel_matrix : array of shape (n_samples_X, n_samples_Y)
759
+ kernel_matrix : array, shape (n_samples_X, n_samples_Y)
761
760
"""
762
761
X , Y = check_pairwise_arrays (X , Y )
763
762
if gamma is None :
@@ -785,27 +784,27 @@ def matern_kernel(X, Y=None, gamma=None, coef0=1.5):
785
784
786
785
Parameters
787
786
----------
788
- X : array of shape (n_samples_X, n_features)
787
+ X : array, shape (n_samples_X, n_features)
789
788
790
- Y : array of shape (n_samples_Y, n_features)
789
+ Y : array, shape (n_samples_Y, n_features)
791
790
792
791
gamma : float
793
792
794
793
coef0 : float>0.0 (the parameter nu)
795
794
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
801
800
[0.5, 1.5, 2.5, inf] incur a considerably higher computational cost
802
801
(appr. 10 times higher) since they require to evaluate the modified
803
802
Bessel function.
804
803
805
804
806
805
Returns
807
806
-------
808
- kernel_matrix : array of shape (n_samples_X, n_samples_Y)
807
+ kernel_matrix : array, shape (n_samples_X, n_samples_Y)
809
808
"""
810
809
if coef0 == np .inf : # fall back to rbf-kernel
811
810
return rbf_kernel (X , Y , gamma )
@@ -819,17 +818,17 @@ def matern_kernel(X, Y=None, gamma=None, coef0=1.5):
819
818
K = euclidean_distances (X , Y , squared = False )
820
819
if coef0 == 0.5 :
821
820
K *= - gamma
822
- np .exp (K , K ) # exponentiate K in-place
821
+ np .exp (K , K ) # exponentiate K in-place
823
822
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 )
826
825
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 )
829
828
else : # general case; expensive to evaluate
830
829
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 )
833
832
K *= tmp ** coef0
834
833
K *= scipy .special .kv (coef0 , tmp )
835
834
return K
@@ -847,16 +846,13 @@ def cosine_similarity(X, Y=None):
847
846
848
847
Parameters
849
848
----------
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).
852
850
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).
855
852
856
853
Returns
857
854
-------
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).
860
856
"""
861
857
# to avoid recursive import
862
858
@@ -894,13 +890,13 @@ def additive_chi2_kernel(X, Y=None):
894
890
895
891
Parameters
896
892
----------
897
- X : array-like of shape (n_samples_X, n_features)
893
+ X : array, shape (n_samples_X, n_features)
898
894
899
- Y : array of shape (n_samples_Y, n_features)
895
+ Y : array, shape (n_samples_Y, n_features)
900
896
901
897
Returns
902
898
-------
903
- kernel_matrix : array of shape (n_samples_X, n_samples_Y)
899
+ kernel_matrix : array, shape (n_samples_X, n_samples_Y)
904
900
905
901
References
906
902
----------
@@ -947,16 +943,16 @@ def chi2_kernel(X, Y=None, gamma=1.):
947
943
948
944
Parameters
949
945
----------
950
- X : array-like of shape (n_samples_X, n_features)
946
+ X : array, shape (n_samples_X, n_features)
951
947
952
- Y : array of shape (n_samples_Y, n_features)
948
+ Y : array, shape (n_samples_Y, n_features)
953
949
954
950
gamma : float, default=1.
955
951
Scaling parameter of the chi2 kernel.
956
952
957
953
Returns
958
954
-------
959
- kernel_matrix : array of shape (n_samples_X, n_samples_Y)
955
+ kernel_matrix : array, shape (n_samples_X, n_samples_Y)
960
956
961
957
References
962
958
----------
@@ -1111,11 +1107,11 @@ def pairwise_distances(X, Y=None, metric="euclidean", n_jobs=1, **kwds):
1111
1107
1112
1108
Parameters
1113
1109
----------
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
1116
1112
Array of pairwise distances between samples, or a feature array.
1117
1113
1118
- Y : array [ n_samples_b, n_features]
1114
+ Y : array, shape ( n_samples_b, n_features)
1119
1115
A second feature array only if X has shape [n_samples_a, n_features].
1120
1116
1121
1117
metric : string, or callable
@@ -1146,7 +1142,7 @@ def pairwise_distances(X, Y=None, metric="euclidean", n_jobs=1, **kwds):
1146
1142
1147
1143
Returns
1148
1144
-------
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)
1150
1146
A distance matrix D such that D_{i, j} is the distance between the
1151
1147
ith and jth vectors of the given matrix X, if Y is None.
1152
1148
If Y is not None, then D_{i, j} is the distance between the ith array
@@ -1212,6 +1208,7 @@ def kernel_metrics():
1212
1208
'rbf' sklearn.pairwise.rbf_kernel
1213
1209
'sigmoid' sklearn.pairwise.sigmoid_kernel
1214
1210
'cosine' sklearn.pairwise.cosine_similarity
1211
+ 'matern' sklearn.pairwise.matern_kernel
1215
1212
=============== ========================================
1216
1213
"""
1217
1214
return PAIRWISE_KERNEL_FUNCTIONS
@@ -1247,12 +1244,12 @@ def pairwise_kernels(X, Y=None, metric="linear", filter_params=False,
1247
1244
kernel between the arrays from both X and Y.
1248
1245
1249
1246
Valid values for metric are::
1250
- ['rbf', 'sigmoid', 'polynomial', 'poly', 'linear', 'cosine']
1247
+ ['rbf', 'sigmoid', 'polynomial', 'poly', 'linear', 'cosine', 'matern' ]
1251
1248
1252
1249
Parameters
1253
1250
----------
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
1256
1253
Array of pairwise kernels between samples, or a feature array.
1257
1254
1258
1255
Y : array [n_samples_b, n_features]
@@ -1286,7 +1283,7 @@ def pairwise_kernels(X, Y=None, metric="linear", filter_params=False,
1286
1283
1287
1284
Returns
1288
1285
-------
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)
1290
1287
A kernel matrix K such that K_{i, j} is the kernel between the
1291
1288
ith and jth vectors of the given matrix X, if Y is None.
1292
1289
If Y is not None, then K_{i, j} is the kernel between the ith array
0 commit comments