Expand python matrix along extra dimension - python

I have a 2 dimensional 3x3 array e.g.:
(4,5,6
8, 10, 12
12,15,18 )
I would like to multiply this by a vector (1,2,3) so that I end up with a 3x3x3 array where along the third dimension all the elements of the original array are multiplied by 1, 2 or 3 respectively. How do it do this in python?

This is the shortest code i could come up with (not the most optimized):
a = [[1,2,3],[4,5,6],[7,8,9]]
b = [1,2,3]
mult = [[[z*x for z in y] for y in a] for x in b]

Related

Merge two arrays from a given index - PYTHON

I am wondering if there is an easy way to 'append' new elements to an array, but not at the end
Imagine I have a vector
a = np.array([1,2,3,4,5,6])
I want to append a new vector
b = np.array([1,1,1,1])
to a, starting from element 3 so that the new array would be
c = np.array([1,2,3,5,6,7,1])
that is the last 3 elements of array a are resulting from a+b while the new element just belong to C
Any ideas?
THX
I tried just append!
Using numpy with pad:
a = np.array([1,2,3,4,5,6])
b = np.array([1,1,1,1])
# or
# a = [1,2,3,4,5,6]
# b = [1,1,1,1]
n = 3
extra = len(b)-len(a)+n
c = np.pad(a, (0, extra))
c[n:] += b
Output:
array([1, 2, 3, 5, 6, 7, 1])

How can I multiply selected elements of a Numpy 2D array by corresponding elements of a 1D array?

Given the numpy arrays a of shape (m,n), b of shape (n,), and mask of the same shape as a, how can I multiply the elements of a selected by 'mask' by the corresponding elements of 'b'? This works:
>>> import numpy as np
>>>
>>> a = np.array([[1,1,1,1],[1,1,1,1]])
>>> mask = np.array([[0,1,0,1],[1,0,1,0]])
>>> b=np.array([2,3,4,5])
>>>
>>> a=np.select([mask==0, mask==1], [a, a*b])
>>> print(a)
[[1 3 1 5]
[2 1 4 1]]
Is there a more elegant way?
This can be another way, but not sure if more elegant than the method you already have!
a = a * (1 - mask) + a * mask * b

NumPy dot product of matrix and array

From numpy's documentation: https://numpy.org/doc/stable/reference/generated/numpy.dot.html#numpy.dot
numpy.dot(a, b, out=None)
Dot product of two arrays. Specifically,
If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a # b is preferred.
If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:
According to the 4th point, if we take the dot product of a 2-D array A and a 1-D array B, we should be taking the sum product over the columns of A and row of B since the last axis of A is the column, correct?
Yet, when I tried this in the Python IDLE, here is my output:
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> b
[1, 2]
>>> a.dot(b)
array([ 5, 11, 17])
I expected this to throw an error since the dimension of a's columns is greater than the dimension of b's row.
If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
The last axis of a has size 2:
>>> a.shape
(3, 2)
which matches bs size.
Remember: the first axis for multi-dimensional arrays in numpy is 'downward'. 1D arrays are displayed horizontally in numpy but I think it's usually better to think of them as vertical vectors instead. This is what's being computed:

Pad list of arrays with zeros in order all arrays to have the same size

I have created this array(or I think its a list) that consist of many arrays that are different size and that is the reason I put dtype = object .
m = [data[a:b] for a, b in zip(z[0:-1:2], z[1:-1:2])]
array = np.array(m, dtype=object)
I need to pad each array with zero so that they have the same size (lets say size=smax) and become a "proper" array. My definitions are a little off and I am sorry in advance
You can do this using np.pad on each row. For example:
import numpy as np
data = np.arange(10)
z = [0, 2, 1, 4, 6, 10, 8, 9]
m = [data[a:b] for a, b in zip(z[0:-1:2], z[1:-1:2])]
max_length = max(len(row) for row in m)
result = np.array([np.pad(row, (0, max_length-len(row))) for row in m])
print(result)
# [[0 1 0 0]
# [1 2 3 0]
# [6 7 8 9]]

which numpy command could I use to subtract vectors with different dimensions many times?

i have to write this function:
in which x is a vector with dimensions [150,2] and c is [N,2] (lets suppose N=20). From each component xi (i=1,2) I have to subtract the components of c in this way ([x11-c11,x12-c12])...([x11-cN1, x12-cN2])for all the 150 sample.
I've trasformed them in a way I have the same dimensions and I can subtract them, but the result of the function should be a vector. Maybe How can I write this in numpy?
Thank you
Ok, lets suppose x=(5,2) and c=(3,2)
this is what I have obtained transforming dimensions of the two arrays. the problem is that, I have to do this but with a iteration "for loop" because the exp function should give me as a result a vector. so I have to obtain a sort of matrix divided in N blocks.
From what I understand of the issue, the problem seems to be in the way you are calculating the vector norm, not in the subtraction. Using your example, but calculating exp(-||x-c||), try:
x = np.linspace(8,17,10).reshape((5,2))
c = np.linspace(1,6,6).reshape((3,2))
sub = np.linalg.norm(x[:,None] - c, axis=-1)
np.exp(-sub)
array([[ 5.02000299e-05, 8.49325705e-04, 1.43695961e-02],
[ 2.96711024e-06, 5.02000299e-05, 8.49325705e-04],
[ 1.75373266e-07, 2.96711024e-06, 5.02000299e-05],
[ 1.03655678e-08, 1.75373266e-07, 2.96711024e-06],
[ 6.12664624e-10, 1.03655678e-08, 1.75373266e-07]])
np.exp(-sub).shape
(5, 3)
numpy.linalg.norm will try to return some kind of matrix norm across all the dimensions of its input unless you tell it explicitly which axis represents the vector components.
I I understand, try if this give the expected result, but there is still the problem that the result has the same shape of x:
import numpy as np
x = np.arange(10).reshape(5,2)
c = np.arange(6).reshape(3,2)
c_col_sum = np.sum(c, axis=0)
for (h,k), value in np.ndenumerate(x):
x[h,k] = c.shape[0] * x[h,k] - c_col_sum[k]
Initially x is:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
And c is:
[[0 1]
[2 3]
[4 5]]
After the function x becomes:
[[-6 -6]
[ 0 0]
[ 6 6]
[12 12]
[18 18]]

Categories