numpy vectorized assignment of sequences [duplicate] - python

This question already has answers here:
Vectorized NumPy linspace for multiple start and stop values
(4 answers)
Closed 6 years ago.
Is there a vectorized assigment of elements to sequences in numpy like in this discussion?
for instance:
xx = np.array([1,2], dtype=object)
expanded = np.arange(xx, xx+2)
instead of loops:
xx = np.array([1,2], dtype=object)
expanded = np.array([np.arange(x, x+2) for x in xx]).flatten()
This would be for mapping a scalar heuristic to the neighboring cells in a matrix that determined it (e.g. the range of cells that had the peak overlap from a correlation() operation).

Like this?
>>> xx = np.array([3,8,19])
>>> (xx[:,None]+np.arange(2)[None,:]).flatten()
array([ 3, 4, 8, 9, 19, 20])
The xx[:,None] operation turns the length n vector into an nx1 matrix, and the np.arange(2)[None,:]) operation creates a length 1x2 matrix containing [0., 1.]. Added together using array broadcasting gives an nx2 matrix, which is then flattened into a length 2n vector.

Related

Adding new feature (column) to given numpy array [duplicate]

This question already has answers here:
How do I add an extra column to a NumPy array?
(17 answers)
Closed 2 years ago.
lets say I have a numpy array
Y =
array([[4.96473614, 6.02336215],
[2.56213959, 2.74621531],
[4.36170066, 5.60413956],
...,
[5.93540184, 3.62864816],
[2.34483661, 2.37333593],
[6.3250522 , 7.09102362]])
with
Y.shape
(1000,2)
(for example 2dim datapoints with x and y)
How can I easily add a third column to this given array (a z-value) such that
Y.shape
(1000,3)
?
Assume that you have:
Y array of shape (3,2) (3 rows instead of 1000):
array([[4.96473614, 6.02336215],
[2.56213959, 2.74621531],
[4.36170066, 5.60413956]])
Y2 array (1-D) of shape (3,):
array([10, 20, 30])
To get your result, you should:
first convert Y2 to (3,1) shape (3 rows, 1 column),
hstack them.
The code to do it is:
Y = np.hstack([Y, Y2[:, np.newaxis]])
The result is:
array([[ 4.96473614, 6.02336215, 10. ],
[ 2.56213959, 2.74621531, 20. ],
[ 4.36170066, 5.60413956, 30. ]])
I hope this helps:
Create the desired column in a (1000,1) array.
Call ArrayName.append(DesiredColumn, axis = 1)

python vector apply mean across axis in chunks of size 5 [duplicate]

This question already has answers here:
Average every x numbers in NumPy array
(4 answers)
Closed 3 years ago.
I have a long vector (50,000 datapoints)
and I want to reduce it by applying mean in chunks of 5. (So I will get vector at the size of 10,000).
For example,
if the vector is
[1,8,-1,0,2 , 6,8,11,4,6]
the output will be
[2,7]
What is the most efficient way to do so?
Thanks
When you know that the vector is always divisible by 5:
import numpy as np
vec = np.array([1,8,-1,0,2 , 6,8,11,4,6])
averaged = vec.reshape(-1, 5).mean(axis=1).flatten()
print(averaged)
Output
array([2., 7.])

matrix product of the two tensors [duplicate]

This question already has answers here:
How do I multiply matrices in PyTorch?
(4 answers)
Closed 3 years ago.
How would you calculate matrix product of the two tensors in PyTorch?
x = torch.Tensor([[1, 2, 3], [1, 2, 3]]).view(-1, 2)
y = torch.Tensor([[2, 1]]).view(2, -1)
I am confused between these options.
You can use one of the options from the code below:
In [188]: torch.einsum("ij, jk -> ik", x, y)
Out[188]:
tensor([[4.],
[7.],
[7.]])
In [189]: x.mm(y)
Out[189]:
tensor([[4.],
[7.],
[7.]])
In [193]: x # y
Out[193]:
tensor([[4.],
[7.],
[7.]])
In [194]: torch.matmul(x, y)
Out[194]:
tensor([[4.],
[7.],
[7.]])
As you can see, all these approaches would yield us the same result.
x*y is a hadamard product (element-wise multiplication) and will not work in this case. Also, torch.dot() would fail as well because it expects 1D tensors. torch.sum(x*y) would just give a single scalar value and that is also wrong since you wish to do matrix multiplication, not an inner product.

Numpy two matrices, pairwise dot product of rows [duplicate]

This question already has answers here:
Vectorized way of calculating row-wise dot product two matrices with Scipy
(5 answers)
Closed 6 years ago.
We are currently working on a python project and have to vectorize a lot due to performance constraints. We end up with the following calculation: We have two numpy arrays of shape (20,6) and want to calculate the pairwise dot product of the rows, i.e. we should obtain a (20,1) matrix in the end, where each row is the scalar obtained by the respective vector dot multiplication.
You can multiply the two arrays element wise and then do sum by rows, and then you have an array where each element is a dot product from rows of the two original arrays:
a = np.array([[1,2], [3,4]])
b = np.array([[3,4], [2,1]])
(a * b).sum(axis=1)
# array([11, 10])

How to normalize one dimension of a 2-dimensional array in python numpy? [duplicate]

This question already has answers here:
How to normalize a 2-dimensional numpy array in python less verbose?
(12 answers)
Closed 8 years ago.
For example, to normalize each row in a 2-dimensional vector such that the magnitude of a row is one:
import numpy as np
a = np.arange(0,27,3).reshape(3,3)
result = a / norm_of_rows( a )
Such that:
np.sum( result**2, axis=-1 )
# array([ 1., 1., 1.])
The original question, How to normalize a 2-dimensional numpy array in python less verbose?, which people feel my question is a duplicate of, the author actually asks how to make the elements of each row sum to one. This is different than normalizing each row such that its magnitude is one (the sum of the square of each element equals one).
np.max(a[0,:]) will give you the maximum of the 1st row,
np.max(a[1,:]) will give you the maximum of the 2nd row
To normalize the whole matrix just loop through your rows and divide each element by the corresponding max_row

Categories