From a21957e85ce9978b60b22748beffe84dfddc0b4e Mon Sep 17 00:00:00 2001 From: Jake VanderPlas Date: Mon, 29 Dec 2014 08:36:25 -0800 Subject: [PATCH] rename ensure_symmetric -> test_symmetric --- sklearn/manifold/mds.py | 4 ++-- sklearn/manifold/spectral_embedding_.py | 4 ++-- sklearn/utils/__init__.py | 2 +- sklearn/utils/validation.py | 18 ++++++++++++------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/sklearn/manifold/mds.py b/sklearn/manifold/mds.py index 51c51bb96d2..50523e95fe6 100644 --- a/sklearn/manifold/mds.py +++ b/sklearn/manifold/mds.py @@ -11,7 +11,7 @@ import warnings from ..base import BaseEstimator from ..metrics import euclidean_distances -from ..utils import check_random_state, check_array, ensure_symmetric +from ..utils import check_random_state, check_array, check_symmetric from ..externals.joblib import Parallel from ..externals.joblib import delayed from ..isotonic import IsotonicRegression @@ -65,7 +65,7 @@ def _smacof_single(similarities, metric=True, n_components=2, init=None, Number of iterations run. """ - similarities = ensure_symmetric(similarities, raise_exception=True) + similarities = check_symmetric(similarities, raise_exception=True) n_samples = similarities.shape[0] random_state = check_random_state(random_state) diff --git a/sklearn/manifold/spectral_embedding_.py b/sklearn/manifold/spectral_embedding_.py index 62278d250dd..e1887a9ba80 100644 --- a/sklearn/manifold/spectral_embedding_.py +++ b/sklearn/manifold/spectral_embedding_.py @@ -13,7 +13,7 @@ from scipy.sparse.linalg import lobpcg from ..base import BaseEstimator from ..externals import six -from ..utils import check_random_state, check_array, ensure_symmetric +from ..utils import check_random_state, check_array, check_symmetric from ..utils.graph import graph_laplacian from ..utils.sparsetools import connected_components from ..utils.arpack import eigsh @@ -183,7 +183,7 @@ def spectral_embedding(adjacency, n_components=8, eigen_solver=None, Andrew V. Knyazev http://dx.doi.org/10.1137%2FS1064827500366124 """ - adjacency = ensure_symmetric(adjacency) + adjacency = check_symmetric(adjacency) try: from pyamg import smoothed_aggregation_solver diff --git a/sklearn/utils/__init__.py b/sklearn/utils/__init__.py index 8abad659a55..bb19a365d99 100644 --- a/sklearn/utils/__init__.py +++ b/sklearn/utils/__init__.py @@ -12,7 +12,7 @@ from .validation import (as_float_array, assert_all_finite, warn_if_not_float, check_random_state, column_or_1d, check_array, check_consistent_length, check_X_y, indexable, - ensure_symmetric) + check_symmetric) from .class_weight import compute_class_weight from ..externals.joblib import cpu_count diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index d1d4804cebd..278813dab3e 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -406,16 +406,16 @@ def has_fit_parameter(estimator, parameter): return parameter in getargspec(estimator.fit)[0] -def ensure_symmetric(array, tol=1E-10, raise_warning=True, - raise_exception=False): +def check_symmetric(array, tol=1E-10, raise_warning=True, + raise_exception=False): """ - Ensure that the array is symmetric two-dimensional array or sparse matrix, + Check that the array is symmetric two-dimensional array or sparse matrix, returning a symmetrized version and optionally raising a warning or exception if the input is not symmetric. Parameters ---------- - array : object + array : nd-array or sparse matrix Input object to check / convert tol : float Absolute tolerance for equivalence of arrays. Default = 1E-10. @@ -427,7 +427,9 @@ def ensure_symmetric(array, tol=1E-10, raise_warning=True, Returns ------- array_sym : object - Symmetrized version of the input array + Symmetrized version of the input array, i.e. the average of array + and array.transpose(). If sparse, then duplicate entries are first + summed and zeros are eliminated. """ if (array.ndim != 2) or (array.shape[0] != array.shape[1]): raise ValueError("array must be 2-dimensional and symmetric") @@ -444,6 +446,10 @@ def ensure_symmetric(array, tol=1E-10, raise_warning=True, if raise_warning: warnings.warn("Array is not symmetric, and will be converted " "to symmetric by average with its transpose.") - array = 0.5 * (array + array.T) + if sp.issparse(array): + conversion = 'to' + array.format + array = getattr(0.5 * (array + array.T), conversion)() + else: + array = 0.5 * (array + array.T) return array