python - Check for equal lists -


after reading converting numpy array python list structure?, have:

import numpy np print np.array(centroids).tolist() print "here\n" print old_centroids  print type(np.array(centroids).tolist()) print type(old_centroids) 

which gives:

[[-0.30485176069166947, -0.2874083792427779, 0.0677763505876472], ...,[0.09384637511656496, -0.015282322735474268, -0.05854574606104108]] here [array([-0.30485176, -0.28740838,  0.06777635]), ..., array([-0.03415291, -0.10915068,  0.07733185]), array([ 0.09384638, -0.01528232, -0.05854575])] <type 'list'> <type 'list'> 

however, when doing:

return old_centroids == np.array(centroids).tolist() 

i getting error:

return old_centroids == np.array(centroids).tolist() valueerror: truth value of array more 1 element ambiguous. 

how fix this?

the type of centroids <type 'numpy.ndarray'> , computed this:

from sklearn import decomposition centroids = pca.transform(mean_centroids) 

note, without pca, do:

return old_centroids == centroids 

edit_0:

check if 2 unordered lists equal suggests set(), did:

return set(old_centroids) == set(np.array(centroids).tolist()) # or set(centroids) 

and got:

typeerror: unhashable type: 'list' 

since comparing floating-point values better use numpy.allclose() and, consequently, keep values in numpy array form:

return np.allclose(np.array(old_centroids), np.array(centroids)) 

(notice transformed list of 1d arrays 2d array; technically, can apply allclose() separately each pair of elements old_centroids , centroids, if wish.)

edit (based on comment): if old_centroids , centroids may have different shapes, check before allclose():

old = np.array(old_centroids) new = np.array(centroids) return old.shape == new.shape , np.allclose(old, new) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -