function for multiplication of two 2d-array row by row [duplicate] - python

This question already has an answer here:
how to calculate the dot product of two arrays of vectors in python? [duplicate]
(1 answer)
Closed 4 years ago.
I need a simple and fast function to multiply each row of numpy array 'a' to array 'b'
a , b have same 2d dimention
like the result of this is example(c):
but I want a numpy function insted of this loop
a=np.arange(6).reshape(3,2)
b=np.arange(6,12).reshape(3,2)
c=np.array([[a[i,:]#b[i,:]]for i in range(a.shape[0])])

An easy way would be to write it yourself with vectorized numpy methods:
np.sum(a*b,axis=1,keepdims=True)
array([[ 8],
[ 48],
[104]])

Related

What is right method for 1-d array/row vector transposition? [duplicate]

This question already has answers here:
Transposing a 1D NumPy array
(15 answers)
numpy's transpose method can't convert 1D row ndarray to a column one [duplicate]
(2 answers)
Numpy transpose of 1D array not giving expected result
(4 answers)
Closed last month.
I know the simple/worked solution to this question is reshape (-1, 1) for turning row vector (numpy.array) into a column vector (numpy.array).
Specifically, I want to understand why numpy.transpose(a) won't work.
Say,
vector_of_1 = np.transpose(np.ones(N)) # statement 1
And if I define a column vector b, and use the following statement:
V = b + vector_of_1
I would get a weird matrix V.
My fix is to use
vector_of_1 = np.ones(N).reshape(-1,1)
And it works as expected (V being a column vector).
But I want to understand why the transpose method (i.e., statement 1) won't work. Detailed explanation is appreciated.

Extracting elements from a NumPy array in Python [duplicate]

This question already has answers here:
Python: slicing a multi-dimensional array
(1 answer)
Slicing/Indexing with multidimensional arrays using Numpy
(2 answers)
Indexing numpy multidimensional arrays depends on a slicing method
(2 answers)
Numpy - slicing 2d row or column vector from array
(3 answers)
Closed 10 months ago.
I have an array that looks as follows:
array([
[[[0.08467145],
[0.0846905 ]]],
[[[0.08470057],
[0.08483638]]],
[[[0.0846846 ],
[0.08471105]]],
[[[0.08469571],
[0.08472978]]]], dtype=float32)
I want to extract the first element from each pair and store in a list and also extract the second element and store it in another list. How can I do this?
You can use array indexing with np.ndarray.flatten:
print(a[:,:,0].flatten())
print(a[:,:,1].flatten())
This outputs:
[0.08467145 0.08470057 0.0846846 0.08469571]
[0.0846905 0.08483638 0.08471105 0.08472978]

Sort rows of a 2d numpy array in ascending order [duplicate]

This question already has answers here:
Python Numpy Sort rows [duplicate]
(2 answers)
Closed 2 years ago.
I have an numpy array arr with shape (1500,10) where each element is a digit from 0 to 9. I'd like to sort the array as each row's elements are concatenated to form a single number and then sort these numbers in ascending order.Let a simple array be like:
arr = ([[3,4,1,5,1,2,3,4,5,6],
[1,2,3,5,6,2,9,2,1,2],
[0,3,1,4,2,1,6,8,2,1],
[0,1,3,5,1,2,9,2,1,7],
[2,3,5,7,1,2,5,7,1,5]])
it should return
arr = ([[0,1,3,5,1,2,9,2,1,7],
[0,3,1,4,2,1,6,8,2,1],
[1,2,3,5,6,2,9,2,1,2],
[2,3,5,7,1,2,5,7,1,5],
[3,4,1,5,1,2,3,4,5,6]])
You can do the following:
arr[np.lexsort(np.flip(arr.transpose(), axis=0))]

how to combine two numpy arrays and form a new array of a new size [duplicate]

This question already has answers here:
Concatenating two one-dimensional NumPy arrays
(6 answers)
Closed 6 years ago.
I have two numpy arrays that have the same shape(4,1,2).
How can I combine them and get a new array of size(8,1,2) with minimum lines of python code? Not changing values just put them together with A on the top B at the bottom.
A=numpy.array([[[1,1]],
[[2,2]],
[[3,3]],
[[4,4]]]);
B=numpy.array([[[5,5]],
[[6,6]],
[[7,7]],
[[8,8]]]);
numpy.concatenate() should do what you want:
numpy.concatenate((A, B))
Use numpy.vstack()
numpy.vstack([A,B])

How to divide matrix elements by corresponding matrix row sum in numpy? [duplicate]

This question already has answers here:
numpy divide row by row sum
(3 answers)
Closed 7 years ago.
For example,
M= [[1,2], [7,8]]
then I want
[[1/3, 2/3], [7/15, 8/15]]
I'm trying to do this vectorized. One idea I have is to write s = np.sum(M, axis=1); this gives us the corresponding row sums. Then I could maybe transpose s, and copy it along the columns, then do an elementwise division of M/s, but even this seems too hacky. What's the right way?
Use tile to repeat it along the dimension on which sum operated.
M / np.tile(np.sum(M, 1), (1, M.shape[1]))

Categories

Resources