-
Notifications
You must be signed in to change notification settings - Fork 23
[TO-REVIEW] Add multi-domain Monge alignment and JCPOT Target shift method #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9e236aa
add per domaij split
rflamary 17f2d7d
better tets multi_domains
rflamary d6a3d9c
first shit multimonge alignment
rflamary 52ad549
add test
rflamary f7e17b4
Merge branch 'main' into multisource
rflamary 8878338
add exmaple
rflamary 4a81b79
Merge branch 'main' into multisource
rflamary 797e0a8
Merge branch 'main' into multisource
rflamary d74e511
update maping to new API
rflamary 1df15ee
Merge branch 'main' into multisource
rflamary 262bf49
Merge branch 'main' into multisource
rflamary 0440cc0
add proper references
rflamary 1f06667
add JCPOT
rflamary 949698b
add stuff
rflamary 8d6399e
Merge branch 'main' into multisource
rflamary b0e2963
Merge branch 'main' into multisource
rflamary 5c4f802
Merge branch 'multisource' of https://github.com/scikit-adaptation/sk…
rflamary d025dfb
add jcpot tests
rflamary 6881e32
upate doc and add JCPOT
rflamary 36dcac1
exmale in gallery of jcpot
rflamary df77630
typo in exmaple
rflamary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
""" | ||
Multi-domain Linear Monge Alignment | ||
=================================== | ||
|
||
This example illustrates the use of the MultiLinearMongeAlignmentAdapter | ||
|
||
""" | ||
|
||
# Author: Remi Flamary | ||
# | ||
# License: BSD 3-Clause | ||
# sphinx_gallery_thumbnail_number = 4 | ||
|
||
# %% Imports | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from sklearn.linear_model import LogisticRegression | ||
|
||
from skada import ( | ||
MultiLinearMongeAlignmentAdapter, | ||
make_da_pipeline, | ||
source_target_split, | ||
) | ||
from skada.datasets import make_shifted_datasets | ||
|
||
# %% | ||
# Generate concept drift classification dataset and plot it | ||
# ----------------------------------------------------- | ||
# | ||
# We generate a simple 2D concept drift dataset. | ||
|
||
X, y, sample_domain = make_shifted_datasets( | ||
n_samples_source=20, | ||
n_samples_target=20, | ||
shift="concept_drift", | ||
noise=0.2, | ||
label="multiclass", | ||
random_state=42, | ||
) | ||
|
||
|
||
Xs, Xt, ys, yt = source_target_split(X, y, sample_domain=sample_domain) | ||
|
||
|
||
plt.figure(5, (10, 5)) | ||
plt.subplot(1, 2, 1) | ||
plt.scatter(Xs[:, 0], Xs[:, 1], c=ys, cmap="tab10", vmax=9, label="Source") | ||
plt.title("Source data") | ||
ax = plt.axis() | ||
|
||
plt.subplot(1, 2, 2) | ||
plt.scatter(Xt[:, 0], Xt[:, 1], c=yt, cmap="tab10", vmax=9, label="Target") | ||
plt.title("Target data") | ||
plt.axis(ax) | ||
|
||
# %% | ||
# Train a classifier on source data | ||
# -------------------------------- | ||
# | ||
# We train a simple SVC classifier on the source domain and evaluate its | ||
# performance on the source and target domain. Performance is much lower on | ||
# the target domain due to the shift. We also plot the decision boundary | ||
|
||
|
||
clf = MultiLinearMongeAlignmentAdapter() | ||
clf.fit(X, sample_domain=sample_domain) | ||
|
||
X_adapt = clf.transform(X, sample_domain=sample_domain, allow_source=True) | ||
|
||
|
||
plt.figure(5, (10, 3)) | ||
plt.subplot(1, 3, 1) | ||
plt.scatter(Xs[:, 0], Xs[:, 1], c=ys, cmap="tab10", vmax=9, label="Source") | ||
plt.title("Source data") | ||
ax = plt.axis() | ||
|
||
plt.subplot(1, 3, 2) | ||
plt.scatter(Xt[:, 0], Xt[:, 1], c=yt, cmap="tab10", vmax=9, label="Target") | ||
plt.title("Target data") | ||
plt.axis(ax) | ||
|
||
plt.subplot(1, 3, 3) | ||
plt.scatter( | ||
X_adapt[sample_domain >= 0, 0], | ||
X_adapt[sample_domain >= 0, 1], | ||
c=y[sample_domain >= 0], | ||
marker="o", | ||
cmap="tab10", | ||
vmax=9, | ||
label="Source", | ||
alpha=0.5, | ||
) | ||
plt.scatter( | ||
X_adapt[sample_domain < 0, 0], | ||
X_adapt[sample_domain < 0, 1], | ||
c=y[sample_domain < 0], | ||
marker="x", | ||
cmap="tab10", | ||
vmax=9, | ||
label="Target", | ||
alpha=1, | ||
) | ||
plt.legend() | ||
plt.title("Adapted data") | ||
|
||
|
||
# %% | ||
# Train a classifier on adapted data | ||
# ---------------------------------- | ||
|
||
clf = make_da_pipeline( | ||
MultiLinearMongeAlignmentAdapter(), | ||
LogisticRegression(), | ||
) | ||
|
||
clf.fit(X, y, sample_domain=sample_domain) | ||
|
||
print( | ||
"Average accuracy on all domains:", | ||
clf.score(X, y, sample_domain=sample_domain, allow_source=True), | ||
) | ||
|
||
# %% Multisource and taregt data | ||
|
||
|
||
def get_multidomain_data( | ||
n_samples_source=100, | ||
n_samples_target=100, | ||
noise=0.1, | ||
random_state=None, | ||
n_sources=3, | ||
n_targets=2, | ||
): | ||
np.random.seed(random_state) | ||
X, y, sample_domain = make_shifted_datasets( | ||
n_samples_source=n_samples_source, | ||
n_samples_target=n_samples_target, | ||
noise=noise, | ||
shift="concept_drift", | ||
label="multiclass", | ||
random_state=random_state, | ||
) | ||
for ns in range(n_sources - 1): | ||
Xi, yi, sample_domaini = make_shifted_datasets( | ||
n_samples_source=n_samples_source, | ||
n_samples_target=n_samples_target, | ||
noise=noise, | ||
shift="concept_drift", | ||
label="multiclass", | ||
random_state=random_state + ns, | ||
mean=np.random.randn(2), | ||
sigma=np.random.rand(2) * 0.5 + 0.5, | ||
) | ||
Xs, Xt, ys, yt = source_target_split(Xi, yi, sample_domain=sample_domaini) | ||
X = np.vstack([X, Xt]) | ||
y = np.hstack([y, yt]) | ||
sample_domain = np.hstack([sample_domain, np.ones(Xt.shape[0]) * (ns + 2)]) | ||
|
||
for nt in range(n_targets - 1): | ||
Xi, yi, sample_domaini = make_shifted_datasets( | ||
n_samples_source=n_samples_source, | ||
n_samples_target=n_samples_target, | ||
noise=noise, | ||
shift="concept_drift", | ||
label="multiclass", | ||
random_state=random_state + nt + 42, | ||
mean=np.random.randn(2), | ||
sigma=np.random.rand(2) * 0.5 + 0.5, | ||
) | ||
Xs, Xt, ys, yt = source_target_split(Xi, yi, sample_domain=sample_domaini) | ||
X = np.vstack([X, Xt]) | ||
y = np.hstack([y, yt]) | ||
sample_domain = np.hstack([sample_domain, -np.ones(Xt.shape[0]) * (nt + 1)]) | ||
|
||
return X, y, sample_domain | ||
|
||
|
||
X, y, sample_domain = get_multidomain_data( | ||
n_samples_source=50, | ||
n_samples_target=50, | ||
noise=0.1, | ||
random_state=43, | ||
n_sources=3, | ||
n_targets=2, | ||
) | ||
|
||
Xs, Xt, ys, yt = source_target_split(X, y, sample_domain=sample_domain) | ||
|
||
|
||
plt.figure(5, (10, 5)) | ||
plt.subplot(1, 2, 1) | ||
plt.scatter(Xs[:, 0], Xs[:, 1], c=ys, cmap="tab10", vmax=9, label="Source") | ||
plt.title("Source data") | ||
ax = plt.axis() | ||
|
||
plt.subplot(1, 2, 2) | ||
plt.scatter(Xt[:, 0], Xt[:, 1], c=yt, cmap="tab10", vmax=9, label="Target") | ||
plt.title("Target domains") | ||
plt.axis(ax) | ||
|
||
|
||
# %% | ||
clf = MultiLinearMongeAlignmentAdapter() | ||
clf.fit(X, sample_domain=sample_domain) | ||
|
||
X_adapt = clf.transform(X, sample_domain=sample_domain, allow_source=True) | ||
|
||
|
||
plt.figure(5, (10, 3)) | ||
plt.subplot(1, 3, 1) | ||
plt.scatter(Xs[:, 0], Xs[:, 1], c=ys, cmap="tab10", vmax=9, label="Source") | ||
plt.title("Source data") | ||
ax = plt.axis() | ||
|
||
plt.subplot(1, 3, 2) | ||
plt.scatter(Xt[:, 0], Xt[:, 1], c=yt, cmap="tab10", vmax=9, label="Target") | ||
plt.title("Target data") | ||
plt.axis(ax) | ||
|
||
plt.subplot(1, 3, 3) | ||
plt.scatter( | ||
X_adapt[sample_domain >= 0, 0], | ||
X_adapt[sample_domain >= 0, 1], | ||
c=y[sample_domain >= 0], | ||
marker="o", | ||
cmap="tab10", | ||
vmax=9, | ||
label="Source", | ||
alpha=0.5, | ||
) | ||
plt.scatter( | ||
X_adapt[sample_domain < 0, 0], | ||
X_adapt[sample_domain < 0, 1], | ||
c=y[sample_domain < 0], | ||
marker="x", | ||
cmap="tab10", | ||
vmax=9, | ||
label="Target", | ||
alpha=1, | ||
) | ||
plt.legend() | ||
plt.axis(ax) | ||
plt.title("Adapted data") | ||
|
||
# %% | ||
# Train a classifier on adapted data | ||
# ---------------------------------- | ||
|
||
clf = make_da_pipeline( | ||
MultiLinearMongeAlignmentAdapter(), | ||
LogisticRegression(), | ||
) | ||
|
||
clf.fit(X, y, sample_domain=sample_domain) | ||
|
||
print( | ||
"Average accuracy on all domains:", | ||
clf.score(X, y, sample_domain=sample_domain, allow_source=True), | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go to toy datasets, right?