I am trying to take out the dot product of each row against itself in a nx3 vector. Let me explain a little better: what I need is to go from a nx3 to a nx3x3 array.
If i have the following:
A = np.array([[1, 2, 2],
[4, 2, 3])
I would like to get what it would be:
First element:
np.dot(A[0].reshape(3,1), A[0].reshape(1,3)) = array([[1, 2, 2], [2, 4, 4], [2, 4, 4]])
Second element:
np.dot(A[1].reshape(3,1), A[1].reshape(1,3)) = array([[16, 8, 12], [8, 4, 6], [12, 6, 9]])
So my final array would be:
result = array([[[ 1, 2, 2],
[ 2, 4, 4],
[ 2, 4, 4]],
[[16, 8, 12],
[ 8, 4, 6],
[12, 6, 9]])
result.shape = (2, 3, 3)
I know I can do this with a for loop but I guess there must be a way to do it faster and more directly. Speed is vital for what I need.
Hope I explained myself correctly enough. Thank you in advance.
In [301]: A = np.array([[1, 2, 2],
...: [4, 2, 3]])
...:
...:
This isn't a dot product; there's no summing of products. Rather it's more like an outer product, increasing the number of dimensions. numpy with broadcasting does this nicely:
In [302]: A[:,:,None]*A[:,None,:]
Out[302]:
array([[[ 1, 2, 2],
[ 2, 4, 4],
[ 2, 4, 4]],
[[16, 8, 12],
[ 8, 4, 6],
[12, 6, 9]]])
Related
Let's say I have data structured in a 2D array like this:
[[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
The first column denotes a third dimension, so I want to convert this to the following 3D array:
[[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]]
Is there a built-in numpy function to do this?
You can try code below:
import numpy as np
array = np.array([[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)
Output
array([[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]])
However, this code can be used when you are aware of the array's shape. But if you are sure that the number of columns in the array is a multiple of 3, you can simply use code below to show the array in the desired format.
array.reshape(array.shape[0]//3,3,-3)
Use numpy array slicing with reshape function.
import numpy as np
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# convert the list to numpy array
arr = np.array(arr)
# remove first column from numpy array
arr = arr[:,1:]
# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)
print(arr)
Output:
[[[3 4 6]
[4 8 2]
[3 2 9]]
[[2 4 8]
[4 9 1]
[2 9 3]]]
You list a non numpy array. I am unsure if you are just suggesting numpy as a means to get a non numpy result, or you are actually looking for a numpy array as result. If you don't actually need numpy, you could do something like this:
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# Length of the 3rd and 2nd dimension.
nz = arr[-1][0] + (arr[0][0]==0)
ny = int(len(arr)/nz)
res = [[arr[ny*z_idx+y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]
OUTPUT:
[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]
Note that the calculation of nz takes into account that the 3rd dimension index in your array is either 0-based (as python is per default) or 1-based (as you show in your example).
This question already has answers here:
How to copy a 2D array into a 3rd dimension, N times?
(7 answers)
Closed 1 year ago.
I'll use a simple 2D array with shape (4,4) as an example:
array([[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]])
And to visualize it:
I want to convert this to a 3D array, so that the values a duplicated along the z-axis, as such:
So that the resulting array has a shape (4,4,3)
It seems really simple, but I can't seem to think of any way to do this.
Edit: I tried np.tile from the answers below, however I would like the output to be this:
array([[[0, 0, 0],
[2, 2, 2],
[6, 6, 6],
[3, 3, 3]],
[[3, 3, 3],
[7, 7, 7],
[3, 3, 3],
[9, 9, 9]],
[[0, 0, 0],
[8, 8, 8],
[3, 3, 3],
[4, 4, 4]],
[[4, 4, 4],
[6, 6, 6],
[2, 2, 2],
[1, 1, 1]]])
I tried changing which axis is duplicated and reshaping, although it doesn't work.
You can use numpy.tile for this
>>> import numpy as np
>>> data = np.array([[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]])
>>> np.tile(data, (3,1,1))
array([[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]],
[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]],
[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]]])
Let's say I have this numpy matrix:
>>> mat = np.matrix([[3,4,5,2,1], [1,2,7,6,5], [8,9,4,5,2]])
>>> mat
matrix([[3, 4, 5, 2, 1],
[1, 2, 7, 6, 5],
[8, 9, 4, 5, 2]])
Now let's say I have some indexes in this form:
>>> ind = np.matrix([[0,2,3], [0,4,2], [3,1,2]])
>>> ind
matrix([[0, 2, 3],
[0, 4, 2],
[3, 1, 2]])
What I would like to do is to get three values from each row of the matrix, specifically values at columns 0, 2, and 3 for the first row, values at columns 0, 4 and 2 for the second row, etc. This is the expected output:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
I've tried using np.take but it doesn't seem to work. Any suggestion?
This is take_along_axis.
>>> np.take_along_axis(mat, ind, axis=1)
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
This will do it: mat[np.arange(3).reshape(-1, 1), ind]
In [245]: mat[np.arange(3).reshape(-1, 1), ind]
Out[245]:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
(but take_along_axis in #user3483203's answer is simpler).
I have 2d and 1d numpy and I want to join them with Cartesian product.
For example the numpy's are:
td = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
od = np.array([1,2,3])
The expected result should be:
[[1,2,3,1],
[1,2,3,2],
[1,2,3,3],
[4,5,6,1],
[4,5,6,2],
[4,5,6,3],
[7,8,9,1],
[7,8,9,2],
[7,8,9,3]]
The following code does not achieve the desired result:
import numpy as np
rslt = np.transpose([np.tile(td, len(od)), np.repeat(od , len(td))])
What needs to change?
Give this a try:
np.c_[np.repeat(td,3,axis=0),np.tile(od,3).reshape((-1,1))]
output:
array([[1, 2, 3, 1],
[1, 2, 3, 2],
[1, 2, 3, 3],
[4, 5, 6, 1],
[4, 5, 6, 2],
[4, 5, 6, 3],
[7, 8, 9, 1],
[7, 8, 9, 2],
[7, 8, 9, 3]])
Quite straightforward question, I have the following array:
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
I want to repeat this array over columns, having something like this:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8]])
So, in order to do so I have been trying:
repeat_x = np.repeat(x, 3, axis = 1)
However, I get the following error:
AxisError: axis 1 is out of bounds for array of dimension 1
So, is there a way/trick to achieve my goal without having to use any sort of reshape?
Try this code:
np.array([x] * 3).T
Here 3 is the number of times you want to repeat those values
To do it purely in numpy without resorting back to python lists you need to use expand_dims followed by a transpose or use reshape to convert the vector into a matrix before using repeat.
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# array([1, 2, 3, 4, 5, 6, 7, 8])
x = x.reshape(-1, 1)
# array([[1],
# [2],
# [3],
# [4],
# [5],
# [6],
# [7],
# [8]])
np.repeat(x.reshape(-1, 1), 3, 1)
# array([[1, 1, 1],
# [2, 2, 2],
# [3, 3, 3],
# [4, 4, 4],
# [5, 5, 5],
# [6, 6, 6],
# [7, 7, 7],
# [8, 8, 8]])
Using expand dims and a transpose will be like
np.repeat(np.expand_dims(x, 0).T, 3, 1)
Same result.