2

After reading this Converting NumPy array into Python List structure?, I have:

import numpy as 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 I am doing:

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

I am getting this Error:

return old_centroids == np.array(centroids).tolist()
ValueError: The truth value of an array with more than one element is ambiguous.

How to fix this?

The type of centroids is <type 'numpy.ndarray'> and they are computed like this:

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

Note, that without PCA, I would just do:

return old_centroids == centroids

EDIT_0:

Check if two unordered lists are equal suggests set(), thus I did:

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

and got:

TypeError: unhashable type: 'list'
Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305

1 Answers1

1

Since you are comparing floating-point values it is better to use numpy.allclose() and, consequently, keep your values in the numpy array form:

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

(notice that I transformed a list of 1D arrays to a 2D array; technically, you can apply allclose() separately for each pair of elements from old_centroids and centroids, if you wish.)

Edit (based on the comment): if old_centroids and centroids may have different shapes, check it before allclose():

old = np.array(old_centroids)
new = np.array(centroids)
return old.shape == new.shape and np.allclose(old, new)
gsamaras
  • 71,951
  • 46
  • 188
  • 305
fjarri
  • 9,546
  • 39
  • 49
  • `r = all(less_equal(abs(x-y), atol + rtol * abs(y))) ValueError: operands could not be broadcast together with shapes (25,0) (25,3)`. Is it maybe because at the very first time, `old_centroids` is just a list of 25 empty lists? – gsamaras Feb 06 '16 at 03:42
  • Well, it is certainly _not_ filled with empty lists in the printout you have in your question. Please add an executable example if there are other variants of `old_centroids` contents. If that is the only other variant, just check for array size to be greater than 0 before comparing. – fjarri Feb 06 '16 at 03:45
  • fjarri, it will be too long and my question is already big. `old_centroids` consists of 25 empty lists at the very first iteration **only** and then gets as the one you see in my question, can't we bypass this? With a dummy check maybe before calling `allclose()`? – gsamaras Feb 06 '16 at 03:46