@@ -22,8 +22,7 @@ def __init__(
22
22
n_components = 1000 ,
23
23
subsample_size = "auto" ,
24
24
kernel = "rbf" ,
25
- bandwidth = 5 ,
26
- gamma = None ,
25
+ gamma = "scale" ,
27
26
degree = 3 ,
28
27
coef0 = 1 ,
29
28
kernel_params = None ,
@@ -34,7 +33,6 @@ def __init__(
34
33
self .n_components = n_components
35
34
self .subsample_size = subsample_size
36
35
self .kernel = kernel
37
- self .bandwidth = bandwidth
38
36
self .gamma = gamma
39
37
self .degree = degree
40
38
self .coef0 = coef0
@@ -66,25 +64,23 @@ def _kernel(self, X, Y):
66
64
params = self .kernel_params or {}
67
65
else :
68
66
params = {
69
- "gamma" : np .float32 (
70
- 0.5 / (self .bandwidth * self .bandwidth )
71
- ),
67
+ "gamma" : self .gamma_ ,
72
68
"degree" : self .degree ,
73
69
"coef0" : self .coef0 ,
74
70
}
75
71
return pairwise_kernels (
76
72
X , Y , metric = self .kernel , filter_params = True , ** params
77
73
)
78
74
distance = euclidean_distances (X , Y , squared = True )
79
- bandwidth = np .float32 (self .bandwidth )
75
+ bandwidth = np .float32 (1.0 / np . sqrt ( 2.0 * self .gamma_ ) )
80
76
if self .kernel == "rbf" :
81
- distance = distance / ( - 2.0 * bandwidth * bandwidth )
77
+ distance = - self . gamma_ * distance
82
78
K = np .exp (distance )
83
79
elif self .kernel == "laplace" :
84
80
d = np .maximum (distance , 0 )
85
81
K = np .exp (- np .sqrt (d ) / bandwidth )
86
82
else : # self.kernel == "cauchy":
87
- K = 1 / (1 + distance / ( bandwidth * bandwidth ) )
83
+ K = 1 / (1 + 2.0 * self . gamma_ * distance )
88
84
return K
89
85
90
86
def _nystrom_svd (self , X , n_components ):
@@ -247,6 +243,10 @@ def _initialize_params(self, X, Y, random_state):
247
243
pinx = random_state .choice (n , sample_size , replace = False ).astype (
248
244
"int32"
249
245
)
246
+ if self .gamma == "scale" :
247
+ self .gamma_ = np .float32 (1.0 / (X .var () * d ))
248
+ else :
249
+ self .gamma_ = self .gamma
250
250
max_S , beta , E , Lambda = self ._setup (
251
251
X [pinx ], n_components , mG , alpha = 0.95
252
252
)
@@ -294,9 +294,9 @@ def validate_parameters(self):
294
294
raise ValueError (
295
295
"batch_size should be positive, was " + str (self .batch_size )
296
296
)
297
- if self .bandwidth <= 0 :
297
+ if self .gamma != "scale" and self . gamma <= 0 :
298
298
raise ValueError (
299
- "bandwidth should be positive, was " + str (self .bandwidth )
299
+ "gamma should be positive, was " + str (self .gamma )
300
300
)
301
301
302
302
def _raw_fit (self , X , Y ):
@@ -369,7 +369,9 @@ def _raw_predict(self, X):
369
369
Y : {float, array}, shape = [n_samples, n_targets]
370
370
Predicted targets.
371
371
"""
372
- check_is_fitted (self , ["bs_" , "centers_" , "coef_" , "was_1D_" ])
372
+ check_is_fitted (
373
+ self , ["bs_" , "centers_" , "coef_" , "was_1D_" , "gamma_" ]
374
+ )
373
375
X = np .asarray (X , dtype = np .float64 )
374
376
375
377
if len (X .shape ) == 1 :
@@ -428,11 +430,11 @@ class EigenProRegressor(BaseEigenPro, RegressorMixin):
428
430
rbf, laplace, and cauchy kernels. If a callable is given, it should
429
431
accept two arguments and return a floating point number.
430
432
431
- bandwidth : float, default=5
432
- Bandwidth to use with the given kernel. For kernels that use gamma,
433
- gamma = .5/(bandwidth^2). Interpretation of the default value is left to
434
- the kernel; see the documentation for sklearn.metrics.pairwise.
435
- Ignored by other kernels .
433
+ gamma : float, default='scale'
434
+ Kernel coefficient. If 'scale', gamma = 1/(n_features*X.var()).
435
+ Interpretation of the default value is left to the kernel;
436
+ see the documentation for sklearn.metrics.pairwise.
437
+ For kernels that use bandwidth, bandwidth = 1/sqrt(2*gamma) .
436
438
437
439
degree : float, default=3
438
440
Degree of the polynomial kernel. Ignored by other kernels.
@@ -466,11 +468,11 @@ class EigenProRegressor(BaseEigenPro, RegressorMixin):
466
468
>>> rng = np.random.RandomState(1)
467
469
>>> x_train = rng.randn(n_samples, n_features)
468
470
>>> y_train = rng.randn(n_samples, n_targets)
469
- >>> rgs = EigenProRegressor(n_epoch=3, bandwidth=1 , subsample_size=50)
471
+ >>> rgs = EigenProRegressor(n_epoch=3, gamma=.5 , subsample_size=50)
470
472
>>> rgs.fit(x_train, y_train)
471
- EigenProRegressor(bandwidth=1, batch_size='auto', coef0=1, degree=3, gamma=None ,
472
- kernel='rbf', kernel_params=None, n_components=1000,
473
- n_epoch=3, random_state=None, subsample_size=50)
473
+ EigenProRegressor(batch_size='auto', coef0=1, degree=3, gamma=0.5, kernel='rbf' ,
474
+ kernel_params=None, n_components=1000, n_epoch=3 ,
475
+ random_state=None, subsample_size=50)
474
476
>>> y_pred = rgs.predict(x_train)
475
477
>>> loss = np.mean(np.square(y_train - y_pred))
476
478
"""
@@ -482,8 +484,7 @@ def __init__(
482
484
n_components = 1000 ,
483
485
subsample_size = "auto" ,
484
486
kernel = "rbf" ,
485
- bandwidth = 5 ,
486
- gamma = None ,
487
+ gamma = "scale" ,
487
488
degree = 3 ,
488
489
coef0 = 1 ,
489
490
kernel_params = None ,
@@ -495,7 +496,6 @@ def __init__(
495
496
n_components = n_components ,
496
497
subsample_size = subsample_size ,
497
498
kernel = kernel ,
498
- bandwidth = bandwidth ,
499
499
gamma = gamma ,
500
500
degree = degree ,
501
501
coef0 = coef0 ,
@@ -543,17 +543,11 @@ class EigenProClassifier(BaseEigenPro, ClassifierMixin):
543
543
rbf, laplace, and cauchy kernels. If a callable is given, it should
544
544
accept two arguments and return a floating point number.
545
545
546
- bandwidth : float, default=5
547
- Bandwidth to use with the given kernel. For kernels that use gamma,
548
- gamma = .5/(bandwidth^2). Interpretation of the default value is left to
549
- the kernel; see the documentation for sklearn.metrics.pairwise.
550
- Ignored by other kernels.
551
-
552
- gamma : float, default=None
553
- Gamma parameter for the RBF, polynomial, exponential chi2
554
- and sigmoid kernels. Interpretation of the default value is left
555
- to the kernel; see the documentation for
556
- sklearn.metrics.pairwise. Ignored by other kernels.
546
+ gamma : float, default='scale'
547
+ Kernel coefficient. If 'scale', gamma = 1/(n_features*X.var()).
548
+ Interpretation of the default value is left to the kernel;
549
+ see the documentation for sklearn.metrics.pairwise.
550
+ For kernels that use bandwidth, bandwidth = 1/sqrt(2*gamma).
557
551
558
552
degree : float, default=3
559
553
Degree of the polynomial kernel. Ignored by other kernels.
@@ -588,12 +582,11 @@ class EigenProClassifier(BaseEigenPro, ClassifierMixin):
588
582
>>> rng = np.random.RandomState(1)
589
583
>>> x_train = rng.randn(n_samples, n_features)
590
584
>>> y_train = rng.randint(n_targets, size=n_samples)
591
- >>> rgs = EigenProClassifier(n_epoch=3, bandwidth=1 , subsample_size=50)
585
+ >>> rgs = EigenProClassifier(n_epoch=3, gamma=.01 , subsample_size=50)
592
586
>>> rgs.fit(x_train, y_train)
593
- EigenProClassifier(bandwidth=1, batch_size='auto', coef0=1, degree=3,
594
- gamma=None, kernel='rbf', kernel_params=None,
595
- n_components=1000, n_epoch=3, random_state=None,
596
- subsample_size=50)
587
+ EigenProClassifier(batch_size='auto', coef0=1, degree=3, gamma=0.01,
588
+ kernel='rbf', kernel_params=None, n_components=1000,
589
+ n_epoch=3, random_state=None, subsample_size=50)
597
590
>>> y_pred = rgs.predict(x_train)
598
591
>>> loss = np.mean(y_train != y_pred)
599
592
"""
@@ -605,8 +598,7 @@ def __init__(
605
598
n_components = 1000 ,
606
599
subsample_size = "auto" ,
607
600
kernel = "rbf" ,
608
- bandwidth = 5 ,
609
- gamma = None ,
601
+ gamma = 0.02 ,
610
602
degree = 3 ,
611
603
coef0 = 1 ,
612
604
kernel_params = None ,
@@ -618,7 +610,6 @@ def __init__(
618
610
n_components = n_components ,
619
611
subsample_size = subsample_size ,
620
612
kernel = kernel ,
621
- bandwidth = bandwidth ,
622
613
gamma = gamma ,
623
614
degree = degree ,
624
615
coef0 = coef0 ,
0 commit comments