Numpy slice from beginning and from end - python

array = numpy.array([1,2,3,4,5,6,7,8,9,10])
array[-1:3:1]
>> []
I want this array indexing to return something like this:
[10,1,2,3]

Use np.roll to:
Roll array elements along a given axis. Elements that roll beyond the last position are re-introduced at the first.
>>> np.roll(x, 1)[:4]
array([10, 1, 2, 3])

np.roll lets you wrap an array which might be useful
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.roll(a,1)[0:4]
results in
>>> b
array([10 1 2 3])

As one of the answers mentioned, rolling the array makes a copy of the whole array which can be memory consuming for large arrays. So just another way of doing this without converting to list is:
np.concatenate([array[-1:],array[:3]])

Use np.r_:
import numpy as np
>>>
>>> arr = np.arange(1, 11)
>>> arr[np.r_[-1:3]]
array([10, 1, 2, 3])

Simplest solution would be to convert first to list, and then join and return to array.
As such:
>>> numpy.array(list(array[-1:]) + list(array[:3]))
array([10, 1, 2, 3])
This way you can choose which indices to start and end, without creating a duplicate of the entire array

Related

Reverse part of an array using NumPy

I am trying to use array slicing to reverse part of a NumPy array. If my array is, for example,
a = np.array([1,2,3,4,5,6])
then I can get a slice b
b = a[::-1]
Which is a view on the original array. What I would like is a view that is partially reversed, for example
1,4,3,2,5,6
I have encountered performance problems with NumPy if you don't play along exactly with how it is designed, so I would like to avoid "fancy" indexing if it is possible.
If you don't like the off by one indices
>>> a = np.array([1,2,3,4,5,6])
>>> a[1:4] = a[1:4][::-1]
>>> a
array([1, 4, 3, 2, 5, 6])
>>> a = np.array([1,2,3,4,5,6])
>>> a[1:4] = a[3:0:-1]
>>> a
array([1, 4, 3, 2, 5, 6])
You can use the permutation matrices (that's the numpiest way to partially reverse an array).
a = np.array([1,2,3,4,5,6])
new_order_for_index = [1,4,3,2,5,6] # Careful: index from 1 to n !
# Permutation matrix
m = np.zeros( (len(a),len(a)) )
for index , new_index in enumerate(new_order_for_index ):
m[index ,new_index -1] = 1
print np.dot(m,a)
# np.array([1,4,3,2,5,6])

Converting list of 2-D arrays into a 3-D array, adding elements along "fast" axes

I have a list of 2d numpy arrays. As a test, consider the following list:
lst = [np.arange(10).reshape(5,2)]*10
Now I can get at a particular data element by:
lst[k][j,i]
I would like to convert this to a numpy array so that I can index it:
array[k,j,i]
i.e., the shape should be (10, 5, 2).
This seems to work, but seems completely unnecessary:
z = np.empty((10,5,2))
for i,x in enumerate(z):
x[:,:] = lst[i]
These don't work:
np.hstack(lst)
np.vstack(lst)
np.dstack(lst) #this is closest, but gives wrong shape (5, 2, 10)
I suppose I could pair a np.dstack with a np.rollaxis, but again, that doesn't seem quite right ...
Is there a good way to do this with numpy?
I've looked at this very related post, but I can't quite seem to work it out.
This should work simply by calling the array constructor, i.e. np.array(lst).
>>> l = [np.arange(10).reshape((5,2)) for i in range(10)]
>>> np.array(l).shape
(10, 5, 2)
Do you mean like
>>> lst = [np.arange(10).reshape(5,2)]*10
>>> arr = np.array(lst)
>>> arr.shape
(10, 5, 2)
?

Python: Select items from a 2D array based on indices of another 2D array

I'm trying to do a simple task, but can't figure out a quick solution.
I have two 2D arrays of the same size. One array is a mask array that simply contains either 0s or 1s; I want to retain values in another array wherever the mask array index is 1.
The mask array could look like:
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
And say I have a second array of:
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
I want to extract the values [2,2,3,4] from testarr.
Thanks for your help!
If you convert your mask to bools, you can use numpy to do this. First convert your arrays to numpy arrays using maskl = np.array(mask, dtype=bool) and estarray = np.array(estarray). Then:
>>> estarr[mask]
array([2, 2, 3, 4])
You could use zip.
for mask_list,val_list in zip(arr1, arr2):
for m,v in zip(mask_list, val_list):
if m:
# put in results
You could flatten the lists with itertools.chain and then simply iterate over the zipped lists, e.g. like the following:
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
from itertools import chain, izip
print [x for m,x in izip(chain(*mask), chain(*testarr)) if m]
# output:
# [2, 2, 3, 4]
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
n = len(mask)
m = len(mask[0])
filtered = []
for i in xrange(n):
for j in xrange(m):
if mask[i][j]:
filtered.append(testarr[i][j])
This is how I'd do it. maybe you can write this as a separate function, and pass in the two lists.
There might be more "elegant" ways, but I don' think there's more "efficient" ways to do it. Personally I like the readability.
You might also check out the numpy.ma module. http://docs.scipy.org/doc/numpy/reference/maskedarray.html
import numpy as np
mask = np.array([[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]],dtype=np.bool)
testarr = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])
ma = np.ma.array(testarr, mask=np.invert(mask))
print ma[~ma.mask]
output
[2 2 3 4]

How to convert a python set to a numpy array?

I am using a set operation in python to perform a symmetric difference between two numpy arrays. The result, however, is a set and I need to convert it back to a numpy array to move forward. Is there a way to do this? Here's what I tried:
a = numpy.array([1,2,3,4,5,6])
b = numpy.array([2,3,5])
c = set(a) ^ set(b)
The results is a set:
In [27]: c
Out[27]: set([1, 4, 6])
If I convert to a numpy array, it places the entire set in the first array element.
In [28]: numpy.array(c)
Out[28]: array(set([1, 4, 6]), dtype=object)
What I need, however, would be this:
array([1,4,6],dtype=int)
I could loop over the elements to convert one by one, but I will have 100,000 elements and hoped for a built-in function to save the loop. Thanks!
Do:
>>> numpy.array(list(c))
array([1, 4, 6])
And dtype is int (int64 on my side.)
Don't convert the numpy array to a set to perform exclusive-or. Use setxor1d directly.
>>> import numpy
>>> a = numpy.array([1,2,3,4,5,6])
>>> b = numpy.array([2,3,5])
>>> numpy.setxor1d(a, b)
array([1, 4, 6])
Try:
numpy.fromiter(c, int, len(c))
This is twice as fast as the solution with list as a middle product.
Try this.
numpy.array(list(c))
Converting to list before initializing numpy array would set the individual elements to integer rather than the first element as the object.

Deleting Elements from an array

I have a numpy array and I want to delete the first 3 elements of the array. I tried this solution:
a = np.arange(0,10)
i=0
while(i<3):
del a[0]
i=i+1
This gives me an error that "ValueError: cannot delete array elements". I do not understand why this is the case. i'd appreciate the help thanks!
Numpy arrays have a fixed size, hence you cannot simply delete an element from them. The simplest way to achieve what you want is to use slicing:
a = a[3:]
This will create a new array starting with the 4th element of the original array.
For certain scenarios, slicing is just not enough. If you want to create a subarray consisting of specific elements from the original array, you can use another array to select the indices:
>>> a = arange(10, 20)
>>> a[[1, 4, 5]]
array([11, 14, 15])
So basically, a[[1,4,5]] will return an array that consists of the elements 1,4 and 5 of the original array.
It works for me:
import numpy as np
a = np.delete(a, k)
where "a" is your numpy arrays and k is the index position you want delete.
Hope it helps.
numpy arrays don't support element deletion. Why don't you just use slicing to achieve what you want?
a = a[3:]
You can convert it into a list and then try regular delete commands like pop, del, eg.
a = np.array([1,2,3,4,5])
l = list(a)
l.pop(3)
l
>>[1, 2, 3, 5]

Categories