Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a Python set from a Numpy matrix

Tags:

I'm trying to execute the following

>> from numpy import *
>> x = array([[3,2,3],[4,4,4]])
>> y = set(x)
TypeError: unhashable type: 'numpy.ndarray'

How can I easily and efficiently create a set with all the elements from the Numpy array?

like image 997
D R Avatar asked Dec 21 '09 10:12

D R


People also ask

Does NumPy have set?

NumPy Set OperationsSets are used for operations involving frequent intersection, union and difference operations.


2 Answers

If you want a set of the elements, here is another, probably faster way:

y = set(x.flatten())

PS: after performing comparisons between x.flat, x.flatten(), and x.ravel() on a 10x100 array, I found out that they all perform at about the same speed. For a 3x3 array, the fastest version is the iterator version:

y = set(x.flat)

which I would recommend because it is the less memory expensive version (it scales up well with the size of the array).

PPS: There is also a NumPy function that does something similar:

y = numpy.unique(x)

This does produce a NumPy array with the same element as set(x.flat), but as a NumPy array. This is very fast (almost 10 times faster), but if you need a set, then doing set(numpy.unique(x)) is a bit slower than the other procedures (building a set comes with a large overhead).

like image 85
Eric O Lebigot Avatar answered Sep 20 '22 17:09

Eric O Lebigot


The immutable counterpart to an array is the tuple, hence, try convert the array of arrays into an array of tuples:

>> from numpy import *
>> x = array([[3,2,3],[4,4,4]])

>> x_hashable = map(tuple, x)

>> y = set(x_hashable)
set([(3, 2, 3), (4, 4, 4)])
like image 44
miku Avatar answered Sep 20 '22 17:09

miku