From 3a332fb27e34e2db4b96611e6aac48e5f3f99250 Mon Sep 17 00:00:00 2001 From: Krzysztof Mierzejewski Date: Wed, 23 Aug 2023 17:51:52 +0200 Subject: [PATCH] Deleting leftovers of `numpy.matrix` type. --- densratio/RuLSIF.py | 14 +++++--------- densratio/helpers.py | 4 ++-- tests/test_helpers.py | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/densratio/RuLSIF.py b/densratio/RuLSIF.py index f80f2b8..749afcc 100644 --- a/densratio/RuLSIF.py +++ b/densratio/RuLSIF.py @@ -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 @@ -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): Search range of Gaussian kernel bandwidth. lambda_range (list): Search range of regularization parameter. @@ -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 @@ -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 diff --git a/densratio/helpers.py b/densratio/helpers.py index 311d79b..38a346d 100644 --- a/densratio/helpers.py +++ b/densratio/helpers.py @@ -1,4 +1,4 @@ -from numpy import array, matrix, ndarray, result_type +from numpy import array, ndarray, result_type np_float = result_type(float) @@ -30,6 +30,6 @@ def to_ndarray(x): elif str(type(x)) == "": 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)) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index b3bfcd2..5c69c91 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -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