Given a numpy array a, is there any alternative to
a[-1]
to get the last element?
The idea is to have some aggregating numpy method as
np.last(a)
that could be passed to a function to operate on a numpy array:
import numpy as np
def operate_on_array(a: np.array, np_method_name: str):
method = getattr(np, np_method_name)
return method(a)
This would work for methods such as np.mean, np.sum but I have not been able to find if there is some numpy method name that would return the last or first element of the array.
What about a lambda?
lambda x: x[-1]
Related
suppose we have two dictionaries
d1={"a":1,"b":2}
d2={"x":4,"y":2}
Now I want to compare only the values of both dictionaries(no matters what keys they are having).
How can I do this operation, pls suggest...
You can transform the values to numpy array and then broadcast the == operator as follows:
import numpy as np
d1={"a":1,"b":2}
d2={"x":4,"y":2}
np.array(list(d1.values())) == np.array(list(d2.values()))
output:
array([False, True])
I would like to create a subclass of numpy.ndarray which is an array of complex number. To that purpose, I'm trying to make the constructor of my sublass such that it returns an array of (0+0j). I'm unsuccessful for the moment...
Here is my code so far :
import numpy as np
class ComplexArray(np.ndarray):
def __init__(self, args):
np.ndarray.__init__(args, dtype=complex)
self.fill(0)
a = ComplexArray(3)
a[0] = 1j
When I run the above code, I get the error TypeError: can't convert complex to float.
I specify that the reason why I want to create such a subclass is that I want to implement several methods in it afterwards.
Thank you in advance for your advice !
I have found a solution :
import numpy as np
class ComplexArray(np.ndarray):
def __new__(cls, n):
ret = np.zeros(n, dtype=complex)
return ret.view(cls)
I am looking for a single vector with values [(0:400) (-400:-1)]
Can anyone help me on how to write this in python.
Using Numpy .array to create the vector and .arange to generate the range:
import numpy as np
arr = np.array([[np.arange(401)], [np.arange(-400, 0)]], dtype=object)
I have a numpy ndarray in this form:
inputs = np.array([[1],[2],[3]])
How can I convert this ndarray to a deque (collections.deque) so that the structure get preserved (array of arrays) and I could apply normal deque methods such as popleft() and append()? for example:
inputs.popleft()
->>> [[2],[3]]
inputs.append([4])
->>> [[2],[3], [4]]
I think you could pass inputs directly to deque
from collections import deque
i = deque(inputs)
In [1050]: i
Out[1050]: deque([array([1]), array([2]), array([3])])
In [1051]: i.popleft()
Out[1051]: array([1])
In [1052]: i
Out[1052]: deque([array([2]), array([3])])
In [1053]: i.append([4])
In [1054]: i
Out[1054]: deque([array([2]), array([3]), [4]])
Later on, when you want numpy.array back, just pass deque back to numpy
np.array(i)
Out[1062]:
array([[2],
[3],
[4]])
Hmm I think that you can do:
inputs = np.array([[1],[2],[3]])
inputs = collections.deque([list(i) for i in inputs])
inputs.append([4])
inputs.popleft()
EDIT.
I edited code
I'm using a camera to store raw data in a numpy array, but I don't know What does mean a colon before a number in numpy array?
import numpy as np
import picamera
camera = picamera.PiCamera()
camera.resolution = (128, 112)
data = np.empty((128, 112, 3), dtype=np.uint8)
camera.capture(data, 'rgb')
data = data[:128, :112]
numpy array indexing is explained in the doc.
this example shows what is selected:
import numpy as np
data = np.arange(64).reshape(8, 8)
print(data)
data = data[:3, :5]
print(data)
the result will be the first 5 elements of the first 3 rows of the array.
as in standard python lst[:3] means everything up to the third element (i.e. the element with index < 3). in numpy you can do the same for every dimension with the syntax given in your question.