MCPcopy Create free account
hub / github.com/PythonOT/POT / transform

Method transform

ot/da.py:2437–2505  ·  view source on GitHub ↗

r"""Transports source samples :math:`\mathbf{X_s}` onto target ones :math:`\mathbf{X_t}` Parameters ---------- Xs : list of K array-like objects, shape K x (nk_source_samples, n_features) A list of the training input samples. ys : list of K array-like obj

(self, Xs=None, ys=None, Xt=None, yt=None, batch_size=128)

Source from the content-addressed store, hash-verified

2435 return self
2436
2437 def transform(self, Xs=None, ys=None, Xt=None, yt=None, batch_size=128):
2438 r"""Transports source samples :math:`\mathbf{X_s}` onto target ones :math:`\mathbf{X_t}`
2439
2440 Parameters
2441 ----------
2442 Xs : list of K array-like objects, shape K x (nk_source_samples, n_features)
2443 A list of the training input samples.
2444 ys : list of K array-like objects, shape K x (nk_source_samples,)
2445 A list of the class labels
2446 Xt : array-like, shape (n_target_samples, n_features)
2447 The training input samples.
2448 yt : array-like, shape (n_target_samples,)
2449 The class labels. If some target samples are unlabelled, fill the
2450 :math:`\mathbf{y_t}`'s elements with -1.
2451
2452 Warning: Note that, due to this convention -1 cannot be used as a
2453 class label
2454 batch_size : int, optional (default=128)
2455 The batch size for out of sample inverse transform
2456 """
2457 nx = self.nx
2458
2459 transp_Xs = []
2460
2461 # check the necessary inputs parameters are here
2462 if check_params(Xs=Xs):
2463 if all([nx.allclose(x, y) for x, y in zip(self.xs_, Xs)]):
2464 # perform standard barycentric mapping for each source domain
2465
2466 for coupling in self.coupling_:
2467 transp = coupling / nx.sum(coupling, 1)[:, None]
2468
2469 # set nans to 0
2470 transp = nx.nan_to_num(transp, nan=0, posinf=0, neginf=0)
2471
2472 # compute transported samples
2473 transp_Xs.append(nx.dot(transp, self.xt_))
2474 else:
2475 # perform out of sample mapping
2476 indices = nx.arange(Xs.shape[0])
2477 batch_ind = [
2478 indices[i : i + batch_size]
2479 for i in range(0, len(indices), batch_size)
2480 ]
2481
2482 transp_Xs = []
2483
2484 for bi in batch_ind:
2485 transp_Xs_ = []
2486
2487 # get the nearest neighbor in the sources domains
2488 xs = nx.concatenate(self.xs_, axis=0)
2489 idx = nx.argmin(dist(Xs[bi], xs), axis=1)
2490
2491 # transport the source samples
2492 for coupling in self.coupling_:
2493 transp = coupling / nx.sum(coupling, 1)[:, None]
2494 transp = nx.nan_to_num(transp, nan=0, posinf=0, neginf=0)

Callers 1

Calls 9

check_paramsFunction · 0.85
distFunction · 0.70
allcloseMethod · 0.45
sumMethod · 0.45
nan_to_numMethod · 0.45
dotMethod · 0.45
arangeMethod · 0.45
concatenateMethod · 0.45
argminMethod · 0.45

Tested by 1