Deleting Elements from an array - python

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]

Related

How to slice numpy array with rows of varying length

I have a numpy array arr of numpy arrays each with varying length. I can get the shape of arr:
arr.shape
>>> (9,)
I can get the shape of one of the elements of arr:
arr[0].shape
>>> (6, 1, 2)
And I know that all such elements have shape (n, 1, 2).
I want to slice arr to get a 1 dimensional result as follows:
arr[:,:,:,0]
But I get the following error:
IndexError: too many indices for array
EDIT: My original question was misleading. I want to do this slice so that I can assign values to the slice. So getting the slice in a new variable is not useful for my case. Essentially I want to do something like this in a simple one liner:
arr[:,:,:,0] = arr[:,:,:,0] - np.min(arr[:,:,:,0])
You can fix your first (in fact all varying ones) dimension, and apply your transformation per static-shaped elements of arr
import numpy as np
from random import randint
arr=np.array([np.random.randint(3,15, size=(randint(3,9),randint(3,7),randint(6,19))) for el in range(9)])
print(arr.shape)
print(arr[0].shape)
for i in range(arr.shape[0]):
arr[i][:,:,0]-=arr[i][:,:,0].min()
print(arr[i][:,:,0])
You could use list comprehension version of your solution.
desired_result = np.array([el[:,:,0] for el in arr])

Numpy: two dimensional arrays, delete the odd indexes and keep the same array format

My array looks like this:
a = ([1,2],[2,3],[4,5],[3,8])
I did the following to delete odd indexes :
a = [v for i, v in enumerate(a) if i % 2 == 0]
but it dives me now two different arrays instead of one two dimensional:
a= [array([1, 2]), array([4, 5])]
How can I keep the same format as the beginning? thank you!
That is as simple as
a[::2]
which yields the lines with even index.
Use numpy array indexing, not comprehensions:
c = a[list(range(0,len(a),2)),:]
If you define c as the output of a list comprehension, it will return a list of one-dimensional numpy arrays. Instead, using the proper indexing maintains the result a numpy array.
Note than instead of "deleting" the odd indices, what we do is specify what to keep: take all lines with an even index (the list(range(0,len(a),2)) part) and for each line take all elements (the : part)

Numpy slice from beginning and from end

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

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.

Categories