Array2 subset of array1

Hi, I am new to numpy so any help would be greatly appreciated. I have two arrays: array1 = np.arange(1,100+1) array2 = np.arange(1,50+1) How can I calculate/determine if array2 is a subset of array1 (falls within array 1) Something like : array2 in array1 = TRUE for the case above. Thank

On Tue, Aug 5, 2014 at 1:58 PM, Jurgens de Bruin <debruinjj@gmail.com> wrote:
Hi,
I am new to numpy so any help would be greatly appreciated.
I have two arrays:
array1 = np.arange(1,100+1) array2 = np.arange(1,50+1)
How can I calculate/determine if array2 is a subset of array1 (falls within array 1)
Something like : array2 in array1 = TRUE for the case above.
Does this work? np.in1d(array2, array1) See: http://docs.scipy.org/doc/numpy/reference/routines.set.html (Note that while in1d does the best it can, set operations on arrays will usually be slower than if you used a more appropriate data type like 'set' or 'dict'.) -n -- Nathaniel J. Smith Postdoctoral researcher - Informatics - University of Edinburgh http://vorpus.org

np.all(np.in1d(array1,array2)) On Tue, Aug 5, 2014 at 2:58 PM, Jurgens de Bruin <debruinjj@gmail.com> wrote:
Hi,
I am new to numpy so any help would be greatly appreciated.
I have two arrays:
array1 = np.arange(1,100+1) array2 = np.arange(1,50+1)
How can I calculate/determine if array2 is a subset of array1 (falls within array 1)
Something like : array2 in array1 = TRUE for the case above.
Thank
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Di, 2014-08-05 at 14:58 +0200, Jurgens de Bruin wrote:
Hi,
I am new to numpy so any help would be greatly appreciated.
I have two arrays:
array1 = np.arange(1,100+1) array2 = np.arange(1,50+1)
How can I calculate/determine if array2 is a subset of array1 (falls within array 1)
Something like : array2 in array1 = TRUE for the case above.
Just to be clear. You are looking for the whole of array1 (as a block/subarray) as far as I understand. And there is no obvious numpy way to do this. Depending on your array sizes, you could blow up the first array from (N,) to (N-M+1,M) and then check if any row matches completely. There may be better tricks available though, especially if array1 is large. - Sebastian
Thank _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

ah yes, that may indeed be what you want. depending on your datatype, you could access the underlying raw data as a string. b.tostring() in a.tostring() sort of works; but isn't entirely safe, as you may have false positive matches which arnt aligned to your datatype using str.find in combination with dtype.itemsize could solve that problem; though it isn't the most elegant solution id say. also note that you need to check for identical datatypes and memory layout for this to guarantee correct results. On Tue, Aug 5, 2014 at 6:33 PM, Sebastian Berg <sebastian@sipsolutions.net> wrote:
On Di, 2014-08-05 at 14:58 +0200, Jurgens de Bruin wrote:
Hi,
I am new to numpy so any help would be greatly appreciated.
I have two arrays:
array1 = np.arange(1,100+1) array2 = np.arange(1,50+1)
How can I calculate/determine if array2 is a subset of array1 (falls within array 1)
Something like : array2 in array1 = TRUE for the case above.
Just to be clear. You are looking for the whole of array1 (as a block/subarray) as far as I understand. And there is no obvious numpy way to do this. Depending on your array sizes, you could blow up the first array from (N,) to (N-M+1,M) and then check if any row matches completely. There may be better tricks available though, especially if array1 is large.
- Sebastian
Thank _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Eelco Hoogendoorn
-
Jurgens de Bruin
-
Nathaniel Smith
-
Sebastian Berg