Make sure that array is 2D, square and symmetric.
If the array is not symmetric, then a symmetrized version is returned. Optionally, a warning or exception is raised if the matrix is not symmetric.
Input object to check / convert. Must be two-dimensional and square, otherwise a ValueError will be raised.
Absolute tolerance for equivalence of arrays. Default = 1E-10.
If True then raise a warning if conversion is required.
If True then raise an exception if array is not symmetric.
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.
>>> import numpy as np
>>> from sklearn.utils.validation import check_symmetric
>>> symmetric_array = np.array([[0, 1, 2], [1, 0, 1], [2, 1, 0]])
>>> check_symmetric(symmetric_array)
array([[0, 1, 2],
[1, 0, 1],
[2, 1, 0]])
>>> from scipy.sparse import csr_matrix
>>> sparse_symmetric_array = csr_matrix(symmetric_array)
>>> check_symmetric(sparse_symmetric_array)
<Compressed Sparse Row sparse matrix of dtype 'int64'
with 6 stored elements and shape (3, 3)>
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.utils.validation.check_symmetric.html