Skip to content
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

Use only lower half of the kernel matrices #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pyHSICLasso/hsic_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

standard_library.install_aliases()


def hsic_lasso(X, Y, y_kernel, x_kernel='Gaussian', n_jobs=-1, discarded=0, B=0, M=1):
"""
Input:
Expand Down Expand Up @@ -50,6 +51,7 @@ def hsic_lasso(X, Y, y_kernel, x_kernel='Gaussian', n_jobs=-1, discarded=0, B=0,

return K, KtL, L


def compute_kernel(x, kernel, B = 0, M = 1, discarded = 0):

d,n = x.shape
Expand All @@ -61,8 +63,9 @@ def compute_kernel(x, kernel, B = 0, M = 1, discarded = 0):
if kernel == "Gaussian":
x = (x / (x.std() + 10e-20)).astype(np.float32)

num_elements = int((B * (B + 1)) / 2)
st = 0
ed = B ** 2
ed = num_elements
index = np.arange(n)
for m in range(M):
np.random.seed(m)
Expand All @@ -75,17 +78,20 @@ def compute_kernel(x, kernel, B = 0, M = 1, discarded = 0):
k = kernel_gaussian(x[:,index[i:j]], x[:,index[i:j]], np.sqrt(d))
elif kernel == 'Delta':
k = kernel_delta_norm(x[:,index[i:j]], x[:, index[i:j]])
else:
raise ValueError("invalid kernel selected")

k = np.dot(np.dot(H, k), H)

# Normalize HSIC tr(k*k) = 1
k = k / (np.linalg.norm(k, 'fro') + 10e-10)
K[st:ed] = k.flatten()
st += B ** 2
ed += B ** 2
K[st:ed] = k[np.tril_indices(k.shape[0])]
st += num_elements
ed += num_elements

return K


def parallel_compute_kernel(x, kernel, feature_idx, B, M, n, discarded):

return (feature_idx, compute_kernel(x, kernel, B, M, discarded))
12 changes: 6 additions & 6 deletions tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ def test_classification(self):

self.hsic_lasso.input("./tests/test_data/csv_data.csv")
self.hsic_lasso.classification(10, B = 0, discrete_x = True, n_jobs = 1)
self.assertEqual(self.hsic_lasso.A, [764, 1422, 512, 248, 1581,
1670, 1771, 896, 779, 266])
self.assertEqual(self.hsic_lasso.A, [764, 1422, 512, 248, 1581,
1670, 896, 1771, 779, 266])

# Blocks
self.hsic_lasso.input("./tests/test_data/csv_data.csv")
B = int(self.hsic_lasso.X_in.shape[1]/2)
self.hsic_lasso.classification(5, B, 10, discrete_x = True)
self.assertEqual(self.hsic_lasso.A, [764, 1422, 512, 248, 266])
self.assertEqual(self.hsic_lasso.A, [764, 1422, 248, 512, 266])

self.hsic_lasso.input("./tests/test_data/csv_data.csv")
B = int(self.hsic_lasso.X_in.shape[1]/2)
self.hsic_lasso.classification(10, B, 10, discrete_x = True)
self.assertEqual(self.hsic_lasso.A, [764, 1422, 512, 248, 1670,
1581, 266, 896, 1771, 779])
self.assertEqual(self.hsic_lasso.A, [764, 1422, 248, 512, 1670,
1581, 896, 266, 1771, 779])

# use non-divisor as block size
with warnings.catch_warnings(record=True) as w:
Expand All @@ -66,7 +66,7 @@ def test_classification(self):
self.hsic_lasso.input("./tests/test_data/matlab_data.mat")
covars = self.hsic_lasso.X_in[[1422, 512],:].T
self.hsic_lasso.classification(5, B = 0, n_jobs = 1, covars = covars)
self.assertEqual(self.hsic_lasso.A, [622, 841, 1636, 1891, 116])
self.assertEqual(self.hsic_lasso.A, [622, 841, 1636, 694, 1891])

if __name__ == "__main__":
unittest.main()
18 changes: 9 additions & 9 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ def test_regression(self):

self.hsic_lasso.input("./tests/test_data/matlab_data.mat")
self.hsic_lasso.regression(5, B = 0, n_jobs = 1)
self.assertEqual(self.hsic_lasso.A, [1099, 99, 199, 1299, 299])
self.assertEqual(self.hsic_lasso.A, [1099, 99, 199, 1299, 1477])

self.hsic_lasso.input("./tests/test_data/matlab_data.mat")
self.hsic_lasso.regression(10, B = 0, n_jobs = 1)
self.assertEqual(self.hsic_lasso.A, [1099, 99, 199, 1299, 1477,
1405, 1073, 299,1596, 358])
self.assertEqual(self.hsic_lasso.A, [1099, 199, 99, 1299, 1477,
1073, 1405, 1596, 375, 358])

# Blocks
self.hsic_lasso.input("./tests/test_data/matlab_data.mat")
B = int(self.hsic_lasso.X_in.shape[1]/2)
self.hsic_lasso.regression(5, B, 10)
self.assertEqual(self.hsic_lasso.A, [1099, 99, 199, 299, 1299])
self.assertEqual(self.hsic_lasso.A, [1099, 99, 199, 1299, 1477])

self.hsic_lasso.input("./tests/test_data/matlab_data.mat")
B = int(self.hsic_lasso.X_in.shape[1]/2)
self.hsic_lasso.regression(10, B, 10)
self.assertEqual(self.hsic_lasso.A, [1099, 99, 199, 1477, 299,
1299, 1073, 1405, 358, 1596])
self.assertEqual(self.hsic_lasso.A, [1099, 199, 99, 1299, 1477,
1073, 1405, 1363, 375, 492])

# use non-divisor as block size
with warnings.catch_warnings(record=True) as w:
Expand All @@ -56,8 +56,8 @@ def test_regression(self):
numblocks = n / B

self.hsic_lasso.regression(10, B, 10)
self.assertEqual(self.hsic_lasso.A, [1422, 248, 512, 1581, 1670,
764, 1771, 896, 779, 398])
self.assertEqual(self.hsic_lasso.A, [1422, 248, 512, 1670, 1581,
764, 896, 1771, 779, 1472])
self.assertEqual(len(w), 1)
self.assertEqual(w[-1].category, RuntimeWarning)
self.assertEqual(str(w[-1].message), "B {} must be an exact divisor of the \
Expand All @@ -67,7 +67,7 @@ def test_regression(self):
self.hsic_lasso.input("./tests/test_data/matlab_data.mat")
covars = self.hsic_lasso.X_in[[99,299],:].T
self.hsic_lasso.regression(5, B = 0, n_jobs = 1, covars = covars)
self.assertEqual(self.hsic_lasso.A, [199, 1477, 1405, 1073, 1596])
self.assertEqual(self.hsic_lasso.A, [199, 1477, 1073, 1405, 1596])

if __name__ == "__main__":
unittest.main()