New to python but ran into something I don't understand. The following line of code:
diff = features[0:] - test[0] # <- this is what I don't get
is used thusly:
x = diff[i]
to return the element-wise difference between features[i] and test[0]. Can anyone point to a language reference for this or explain it? I understand how the result can be developed using "def" or "lambda" but I don't understand the construction.
thanks!
the answer depends on what features[0:] and test[0] evaluate to.
if test[0] is a number and features[0:] is a numpy array, then you may be using numpy to subtract a number from each element in a list:
>>> import numpy
>>> array = numpy.array([49, 51, 53, 56])
>>> array - 13
array([36, 38, 40, 43])
feature appears to be a Numpy array. Numpy arrays 'broadcast' scalar operations to the whole array.
import numpy as np
asd = np.full([10,10], 10, np.int64)
asd /= 5
print asd # prints a 10x10 array of 2s
Related
I just learn about numpy and array today and I am quite confused about something. I don’t get line 3 as I thought np.array() should have a list in the (). Can someone explain that line to me? And for line 5, I know it is comparing array x and y. But can someone explain to me how it works? And what does x[y] mean? Thank you so much.
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array(x<4)
print(y)
print(x[y])
What this code snippet does is masking the x array with a condition. x[y] is the masked array, which shows only the elements of x where y is True (in that case where x < 4).
y = np.array(x < 4) has an useless np.array call, as x < 4 is already a numpy array. That being said, you can give many objects to np.array() such as lists, tuples, other arrays...
The whole thing should be simply:
import numpy as np
x = np.array([1, 2, 3, 4, 5])
print(x[x < 4])
# [1 2 3]
Line three tells you the places in x where the value is less than four. You don't need the "np.array" constructor out front though - y = x<4 will return you the same array.
Line five filters the x array, using the values in y that are 'True'. This is often referred to as a 'mask'.
I have a generator that returns numpy arrays.
For example sake, let it be:
import numpy as np
a = np.arange(9).reshape(3,3)
gen = (x for x in a)
Calling:
np.sum(gen)
On numpy 1.17.4:
DeprecationWarning: Calling np.sum(generator) is deprecated, and in
the future will give a different result. Use
np.sum(np.fromiter(generator)) or the python sum builtin instead.
Trying to refactor the above:
np.sum(np.fromiter(gen, dtype=np.ndarray))
I get:
ValueError: cannot create object arrays from iterator
What is wrong in the above statement?
The problem is the second argument, np.ndarray in the fromiter(). Numpy fromiter expected a 1D and returns a 1D array:
Create a new 1-dimensional array from an iterable object.
Therefore, you cannot create object arrays from iterator. Furthermore the .reshape() will also raise an error, because of what I stated in the first line. All in all, this works:
import numpy as np
a = np.arange(9)
gen = (x for x in a)
print(np.sum(np.fromiter(gen,float)))
Output:
36
Since you're summing instances of arrays you can just use the built-in sum:
result = sum(gen)
What about simply converting your generator to a list and then passing it to the np.sum?
a = np.arange(9).reshape(3,3)
gen = (x for x in a)
Summing all the elements:
>>> np.sum(list(gen))
36
Summing column-wise:
>>> np.sum(list(gen), axis=0)
array([ 9, 12, 15])
Summing row-wise:
>>> np.sum(list(gen), axis=1)
array([ 3, 12, 21])
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])
I want to get the tangent inverse of a set of array
import numpy as np
import math
For example (this is an array)
x_value=[1 2 3 4 5 6]
a= abs(x_value-125)
This still works fine, but when I get the tangent inverse of a:
b=math.atan(a)
I got this error: TypeError: only length-1 arrays can be converted to Python scalars
How should I solve this error where I can get the tangent inverse of the elements of array a?
Just use np.arctan:
>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6])
>>> a = abs(a - 125) # could use np.abs. It does the same thing, but might be more clear that you expect to get an ndarray instance as a result.
>>> a
array([124, 123, 122, 121, 120, 119])
>>> np.arctan(a)
array([ 1.56273199, 1.56266642, 1.56259979, 1.56253205, 1.56246319,
1.56239316])
you could use a list comprehension to apply the atan function to each element of the array:
a = np.abs(np.array([1,2,3,4,5,6]) - 125)
b = [np.math.atan(x) for x in a]
You can use a list comprehension:
b = [math.atan(ele) for ele in a]
I want to do something like this:
a = # multi-dimensional numpy array
ares = # multi-dim array, same shape as a
a.shape
>>> (45, 72, 37, 24) # the relevant point is that all dimension are different
v = # 1D numpy array, i.e. a vector
v.shape
>>> (37) # note that v has the same length as the 3rd dimension of a
for i in range(37):
ares[:,:,i,:] = a[:,:,i,:]*v[i]
I'm thinking there has to be a more compact way to do this with numpy, but I haven't figured it out. I guess I could replicate v and then calculate a*v, but I am guessing there is something better than that too. So I need to do element wise multiplication "over a given axis", so to speak. Anyone know how I can do this? Thanks. (BTW, I did find a close duplicate question, but because of the nature of the OP's particular problem there, the discussion was very short and got tracked into other issues.)
Here is one more:
b = a * v.reshape(-1, 1)
IMHO, this is more readable than transpose, einsum and maybe even v[:, None], but pick the one that suits your style.
You can automatically broadcast the vector against the outermost axis of an array. So, you can transpose the array to swap the axis you want to the outside, multiply, then transpose it back:
ares = (a.transpose(0,1,3,2) * v).transpose(0,1,3,2)
You can do this with Einstein summation notation using numpy's einsum function:
ares = np.einsum('ijkl,k->ijkl', a, v)
I tend to do something like
b = a * v[None, None, :, None]
where I think I'm officially supposed to write np.newaxis instead of None.
For example:
>>> import numpy as np
>>> a0 = np.random.random((45,72,37,24))
>>> a = a0.copy()
>>> v = np.random.random(37)
>>> for i in range(len(v)):
... a[:,:,i,:] *= v[i]
...
>>> b = a0 * v[None,None,:,None]
>>>
>>> np.allclose(a,b)
True