@@ -80,7 +80,7 @@ class KLIEP:
80
80
If get_estimator is ``None``, a ``LinearRegression`` object will be
81
81
used by default as estimator.
82
82
83
- sigmas : float or list of float, optional (default=0.1 )
83
+ sigmas : float or list of float, optional (default=1/nb_features )
84
84
Kernel bandwidths.
85
85
If ``sigmas`` is a list of multiple values, the
86
86
kernel bandwidth is selected with the LCV procedure.
@@ -89,10 +89,20 @@ class KLIEP:
89
89
Cross-validation split parameter.
90
90
Used only if sigmas has more than one value.
91
91
92
- max_points : int, optional (default=100)
92
+ max_centers : int, optional (default=100)
93
93
Maximal number of target instances use to
94
94
compute kernels.
95
95
96
+ lr: float, optional (default=1e-4)
97
+ Learning rate of the gradient ascent.
98
+
99
+ tol: float, optional (default=1e-6)
100
+ Optimization threshold.
101
+
102
+ max_iter: int, optional (default=5000)
103
+ Maximal iteration of the gradient ascent
104
+ optimization.
105
+
96
106
kwargs : key, value arguments, optional
97
107
Additional arguments for the constructor.
98
108
@@ -128,11 +138,11 @@ class KLIEP:
128
138
"Direct importance estimation with model selection and its application \
129
139
to covariateshift adaptation". In NIPS 2007
130
140
"""
131
- def __init__ (self , estimator = None ,
141
+ def __init__ (self , get_estimator = None ,
132
142
sigmas = None , max_centers = 100 ,
133
143
cv = 5 , lr = 1e-4 , tol = 1e-6 , max_iter = 5000 ,
134
144
verbose = 1 , ** kwargs ):
135
- self .estimator = estimator
145
+ self .get_estimator = get_estimator
136
146
self .sigmas = sigmas
137
147
self .cv = cv
138
148
self .max_centers = max_centers
@@ -142,8 +152,8 @@ def __init__(self, estimator=None,
142
152
self .verbose = verbose
143
153
self .kwargs = kwargs
144
154
145
- if self .estimator is None :
146
- self .estimator = LinearRegression ()
155
+ if self .get_estimator is None :
156
+ self .get_estimator = LinearRegression
147
157
148
158
149
159
def fit_weights (self , Xs , Xt = None ):
@@ -187,24 +197,26 @@ def fit_weights(self, Xs, Xt=None):
187
197
188
198
189
199
def fit_estimator (self , X , y , ** fit_params ):
200
+ self .estimator_ = self .get_estimator (** self .kwargs )
190
201
if hasattr (self , "weights_" ):
191
- if "sample_weight" in inspect .signature (self .estimator .fit ).parameters :
192
- self .estimator .fit (X , y ,
202
+ if "sample_weight" in inspect .signature (self .estimator_ .fit ).parameters :
203
+ self .estimator_ .fit (X , y ,
193
204
sample_weight = self .weights_ ,
194
205
** fit_params )
195
206
else :
196
207
bootstrap_index = np .random .choice (
197
208
len (X ), size = len (X ), replace = True ,
198
209
p = self .weights_ / self .weights_ .sum ())
199
- self .estimator .fit (X [bootstrap_index ], y [bootstrap_index ],
210
+ self .estimator_ .fit (X [bootstrap_index ], y [bootstrap_index ],
200
211
** fit_params )
201
212
else :
202
213
raise NotFittedError ("Weights are not fitted yet, please "
203
214
"call 'fit_weights' first." )
204
215
return self
205
216
206
217
207
- def fit (self , Xs , ys , Xt = None , ** fit_params ):
218
+ def fit (self , X , y , src_index , tgt_index ,
219
+ tgt_index_labeled = None , ** fit_params ):
208
220
"""
209
221
Fit KLIEP.
210
222
@@ -233,6 +245,14 @@ def fit(self, Xs, ys, Xt=None, **fit_params):
233
245
-------
234
246
self : returns an instance of self
235
247
"""
248
+ if tgt_index_labeled is None :
249
+ Xs = X [src_index ]
250
+ ys = y [src_index ]
251
+ else :
252
+ Xs = X [np .concatenate ((src_index , tgt_index_labeled ))]
253
+ ys = y [np .concatenate ((src_index , tgt_index_labeled ))]
254
+ Xt = X [tgt_index ]
255
+
236
256
if self .verbose :
237
257
print ("Fitting weights..." )
238
258
self .fit_weights (Xs , Xt )
@@ -318,7 +338,7 @@ def predict(self, X):
318
338
y_pred: array
319
339
prediction of estimator.
320
340
"""
321
- return self .estimator .predict (X )
341
+ return self .estimator_ .predict (X )
322
342
323
343
324
344
def predict_weights (self , X = None ):
0 commit comments