-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into docs
- Loading branch information
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
""" | ||
|
||
from ._fastcan import FastCan | ||
from ._ssc import ssc | ||
|
||
__all__ = [ | ||
"FastCan", | ||
"ssc", | ||
] |
This file contains 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,47 @@ | ||
"""Sum squared of correlation.""" | ||
|
||
import numpy as np | ||
from sklearn.cross_decomposition import CCA | ||
from sklearn.utils import check_X_y | ||
from sklearn.utils._param_validation import validate_params | ||
|
||
|
||
@validate_params( | ||
{ | ||
"X": ["array-like"], | ||
"y": ["array-like"], | ||
}, | ||
prefer_skip_nested_validation=True, | ||
) | ||
def ssc(X, y): | ||
"""Sum of the squared canonical correlation coefficients. | ||
Parameters | ||
---------- | ||
X : array-like of shape (n_samples, n_features) | ||
Feature matrix. | ||
y : array-like of shape (n_samples, n_outputs) | ||
Target matrix. | ||
Returns | ||
------- | ||
ssc : float | ||
Sum of the squared canonical correlation coefficients. | ||
Examples | ||
-------- | ||
>>> from fastcan import ssc | ||
>>> X = [[1], [-1], [0]] | ||
>>> y = [[0], [1], [-1]] | ||
>>> ssc(X, y) | ||
np.float64(0.25) | ||
""" | ||
X, y = check_X_y( | ||
X, y, dtype=float, ensure_2d=True, multi_output=True, ensure_min_samples=2 | ||
) | ||
n_components = min(X.shape[1], y.shape[1]) | ||
cca = CCA(n_components=n_components) | ||
X_c, y_c = cca.fit_transform(X, y) | ||
corrcoef = np.diagonal(np.corrcoef(X_c, y_c, rowvar=False), offset=n_components) | ||
return sum(corrcoef**2) |
This file contains 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# pylint: skip-file | ||
"""Test FastCan""" | ||
|
||
import numpy as np | ||
|
This file contains 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,32 @@ | ||
"Test ssc" | ||
|
||
import numpy as np | ||
from numpy.testing import assert_almost_equal | ||
from sklearn.linear_model import LinearRegression | ||
|
||
from fastcan import ssc | ||
|
||
|
||
def test_pearson_r(): | ||
"""Test Pearson's correlation.""" | ||
rng = np.random.default_rng(12345) | ||
X = rng.random(100) | ||
y = rng.random(100) | ||
r2 = ssc(X.reshape(-1, 1), y.reshape(-1, 1)) | ||
gtruth_r2 = np.corrcoef(X, y)[0, 1]**2 | ||
assert_almost_equal(actual=r2, desired=gtruth_r2) | ||
|
||
def test_multi_r(): | ||
"""Test multiple correlation.""" | ||
rng = np.random.default_rng(12345) | ||
X = rng.random((100, 10)) | ||
y = rng.random(100) | ||
r2 = ssc(X, y.reshape(-1, 1)) | ||
gtruth_r2 = LinearRegression().fit(X, y).score(X, y) | ||
assert_almost_equal(actual=r2, desired=gtruth_r2) | ||
|
||
X = rng.random(100) | ||
y = rng.random((100, 10)) | ||
r2 = ssc(X.reshape(-1, 1), y) | ||
gtruth_r2 = LinearRegression().fit(y, X).score(y, X) | ||
assert_almost_equal(actual=r2, desired=gtruth_r2) |