Skip to content
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: 5 additions & 9 deletions densratio/RuLSIF.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Journal of Machine Learning Research 10 (2009) 1391-1445.
"""

from numpy import array, asarray, asmatrix, diag, diagflat, empty, exp, inf, log, matrix, multiply, ones, power, sum
from numpy import array, asarray, diag, diagflat, empty, exp, inf, log, multiply, ones, power, sum
from numpy.random import randint
from numpy.linalg import solve
from warnings import warn
Expand All @@ -26,8 +26,8 @@ def RuLSIF(x, y, alpha, sigma_range, lambda_range, kernel_num=100, verbose=True)
p_alpha(x) = alpha * p(x) + (1 - alpha) * q(x)

Arguments:
x (numpy.matrix): Sample from p(x).
y (numpy.matrix): Sample from q(x).
x (numpy.ndarray): Sample from p(x).
y (numpy.ndarray): Sample from q(x).
alpha (float): Mixture parameter.
sigma_range (list<float>): Search range of Gaussian kernel bandwidth.
lambda_range (list<float>): Search range of regularization parameter.
Expand Down Expand Up @@ -191,12 +191,8 @@ def _compute_kernel_Gaussian(x_list, y_row, neg_gamma, res) -> None:

def _target_numpy_wrapper(x_list, y_list, neg_gamma):
res = empty((y_list.shape[0], x_list.shape[0]), np_float)
if isinstance(x_list, matrix) or isinstance(y_list, matrix):
res = asmatrix(res)

for j, y_row in enumerate(y_list):
# `.T` aligns shapes for matrices, does nothing for 1D ndarray.
_compute_kernel_Gaussian(x_list, y_row, neg_gamma, res[j].T)
_compute_kernel_Gaussian(x_list, y_row, neg_gamma, res[j])

return res

Expand All @@ -208,7 +204,7 @@ def _target_numpy_wrapper(x_list, y_list, neg_gamma):
_compute_function = _compute_functions['cpu' if 'cpu' in _compute_functions else 'numpy']


# Returns a 2D numpy matrix of kernel evaluated at the gridpoints with coordinates from x_list and y_list.
# Returns a 2D numpy ndarray of kernel evaluated at the gridpoints with coordinates from x_list and y_list.
def compute_kernel_Gaussian(x_list, y_list, sigma):
return _compute_function(x_list, y_list, -.5 * sigma ** -2).T

Expand Down
4 changes: 2 additions & 2 deletions densratio/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from numpy import array, matrix, ndarray, result_type
from numpy import array, ndarray, result_type


np_float = result_type(float)
Expand Down Expand Up @@ -30,6 +30,6 @@ def to_ndarray(x):
elif str(type(x)) == "<class 'pandas.core.frame.DataFrame'>":
return x.values
elif not x:
raise ValueError("Cannot transform to numpy.matrix.")
raise ValueError("Cannot transform to numpy.ndarray.")
else:
return to_ndarray(array(x))
2 changes: 1 addition & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from numpy import array, matrix
from numpy import array
from numpy.testing import assert_array_equal
from pandas import DataFrame
from .context import helpers
Expand Down