reshaping numpy array/matrix - python

im trying to reshape the following numpy array.
from this:
array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]], dtype=int64)
to something like this:
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]], dtype=int64)
Tried to use the reshape function, but that didnt work for me.
thanks

Just np.stack along axis 1:
arr = np.array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]])
np.stack(arr, 1)
results in
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]])

This would work:
import numpy as np
x = np.array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]], dtype=np.int64)
np.vstack(map(lambda x: [x], zip(*(x))))
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]], dtype=int64)

You should be able to use use the numpy.reshape function without any error.
Here's how:
a = array([[[ 1, 2, 3],
[ 2, 3, 4],
[ 3, 4, 5]],
[[-1, -2, -3],
[-2, -3, -4],
[-3, -4, -5]]], dtype=int64)
a = a.reshape(3, 2, 3)
Here's what array 'a' will be converted into:
array([[[ 1 2 3]
[ 2 3 4]]
[[ 3 4 5]
[-1 -2 -3]]
[[-2 -3 -4]
[-3 -4 -5]]])

This isn't a reshape problem. It's transpose task - reordering the axes:
In [293]: arr.transpose(1,0,2)
Out[293]:
array([[[ 1, 2, 3],
[-1, -2, -3]],
[[ 2, 3, 4],
[-2, -3, -4]],
[[ 3, 4, 5],
[-3, -4, -5]]])
stack works by iterating on the first dimension, and concatenating on a new middle axis. transpose just makes a view so will be faster.

Related

Facing ValueError : shape mismatch

How to index the arrays into the empty_array matrix using np. ix_?
please don,t get confused by other details, dict_A and dict_B are used just to calculate the index positions.
However, because my code is too long, that's why I just mentioned the value of 'arrays' directly below here( making an np.array), without mentioning it's background and how it came.
import numpy as np
dict_A = { 1:[1,2], 2:[2,3], 3:[3,4], 4:[4,5], 5:[5,6] }
dict_B = {1:[1,2], 2:[3,4], 3:[5,6], 4:[7,8], 5:[9,10], 6:[11,12] } # these are the values used for indexing the arrays in to K matrix
empty_array = np.zeros((40,40))
arrays = np.array([[[[ 1, -7, -1, 7 ], # five matrix obtained from a loop
[ -7, 3, 7, -3],
[-1, 7, 1, -7 ],
[ 7, -3, -7, 3]]],
[[[ 1, -6, -1, 6],
[ -6, 2, 6, -2],
[-1, 6, 1, -6],
[ 6, -2,-6, 2]]],
[[[ 1, -6, -5,-2],
[ 2, 0, 5, 9 ],
[-5, 1, 6, 8 ],
[-12, 1, 4, 5 ]]],
[[[ 2, 5, 4, 2],
[ -4, 5, 1, 7],
[7, -5, -2, 3],
[ 0, 2, 5, 3]]],
[[[ 3, 0, 2, 5],
[ -2, 6, 3, 1],
[-2, 5, 3, 5],
[ 2, 6, 12, 3 ]]]])
# this is just a small process by which i am combining values of 'dict_B' to create an 'index', for positioning arrays.
a_list = []
for i in dict_A:
index= []
a_list.append(index)
for j in dict_A[i]:
index.extend(dict_B[j])
print(index)
empty_array[np.ix_(index,index)] = empty_array[np.ix_(index,index)] + arrays
#ValueError: shape mismatch: value array of shape (5,1,4,4) could not be broadcast to indexing result of shape (4,4)
print(K)

I am trying to create a symmetric matrix from the output I got in python without using the numpy and additional library?

I am using the matrix for the multiple sequence alignment and this is my score matrix which I got by running the alignment algorithm.
My matrix:
[
[0, 24, -5, 3, -3, -5],
[0, -4, 8, 1, 1],
[0, 13, 1, 2],
[0, -2, 5],
[0, 4],
[0]
]
Matrix I want to build:
[
[0, 24, -5, 3, -3, -5],
[24, 0, -4, 8, 1, 1],
[-5, -4, 0, 13, 1, 2],
[3, 8, 13, 0, -2, 5],
[-3, 1, 1, 2, 0, 4],
[-5, 1, 2, 5, 4, 0]
]
I am trying to create a symmetric matrix from the output I got in python without using NumPy and additional library. I have tried to implement using NumPy but I want to implement without using NumPy.
Try the following:
upper = [[0, 24, -5, 3, -3, -5], [0, -4, 8, 1, 1], [0, 13, 1, 2], [0, -2, 5], [0, 4], [0]]
n = len(upper) # 6: num of rows and cols (assuming square)
output = []
for i in range(n): # iterate over rows
row = [(upper[i][j - i] if j >= i else output[j][i]) for j in range(n)]
output.append(row)
print(output)
# [[0, 24, -5, 3, -3, -5], [24, 0, -4, 8, 1, 1], [-5, -4, 0, 13, 1, 2], [3, 8, 13, 0, -2, 5], [-3, 1, 1, -2, 0, 4], [-5, 1, 2, 5, 4, 0]]

Interchanging rows in Numpy produces an embedded array

I'm trying to interchange the rows of np.array A using the following array:
A = np.array([[0,-3,-6,4,9],
[-1,-2,-1,3,1],
[-2,-3,0,3,-1],
[1,4,5,-9,-7]])
When I use the following code:
A = np.array([A[3],A[0],A[1],A[2]])
my array becomes
array([[ 1, 4, 5, -9, -7],
[ 0, -3, -6, 4, 9],
[-1, -2, -1, 3, 1],
[-2, -3, 0, 3, -1]])
like I hoped, wished and dreamed. When I try a broader slice, though (as I would need for larger matrices), it doesn't work quite as well:
A = np.array([A[3], A[0:3]])
A
array([array([-2, -3, 0, 3, -1]),
array([[ 1, 4, 5, -9, -7],
[ 0, -3, -6, 4, 9],
[-1, -2, -1, 3, 1]])], dtype=object)
Why is this happening/how can I correctly perform this slice?
The first expression can be written much more simply as
A = A[[3, 0, 1, 2], :])
The second can therefore be written as
A = A[[3, *range(3)], :]
This is more general than using roll, since you can move an arbitrary row with something like
A = A[[1, *range(1), *range(2, 4)], :]
You could use vstack:
In [5]: np.vstack([A[3], A[0:3]])
Out[5]:
array([[ 1, 4, 5, -9, -7],
[ 0, -3, -6, 4, 9],
[-1, -2, -1, 3, 1],
[-2, -3, 0, 3, -1]])
np.roll as commented is probably the best choice. You could also use np.r_:
A[np.r_[3,0:3]]
Out:
array([[ 1, 4, 5, -9, -7],
[ 0, -3, -6, 4, 9],
[-1, -2, -1, 3, 1],
[-2, -3, 0, 3, -1]])

Fastest Pairwise Difference of Rows

I have an n by 3 numpy array, with each row representing a vector in 3D space.
I'd like to have a 3D n by n by 3 array with the vector at [i, j] representing the differences between each component of vectors i and j.
>>> x = np.random.randint(10, size=(4, 3))
>>> x
array([[4, 0, 8],
[8, 5, 3],
[4, 1, 6],
[2, 2, 4]])
>>> x[:, np.newaxis] - x
array([[[ 0, 0, 0],
[-4, -5, 5],
[ 0, -1, 2],
[ 2, -2, 4]],
[[ 4, 5, -5],
[ 0, 0, 0],
[ 4, 4, -3],
[ 6, 3, -1]],
[[ 0, 1, -2],
[-4, -4, 3],
[ 0, 0, 0],
[ 2, -1, 2]],
[[-2, 2, -4],
[-6, -3, 1],
[-2, 1, -2],
[ 0, 0, 0]]])
This works, but is the slowest line in my program by far...slower even than my Euclidean distance code (which takes advantage of dot products and einsum...)
Are there any clever matrix math methods for doing what I want in a more efficient way?

Numpy Matrix Difference row by row into 3D tensor [duplicate]

I know I can do np.subtract.outer(x, x). If x has shape (n,), then I end up with an array with shape (n, n). However, I have an x with shape (n, 3). I want to output something with shape (n, n, 3). How do I do this? Maybe np.einsum?
You can use broadcasting after extending the dimensions with None/np.newaxis to form a 3D array version of x and subtracting the original 2D array version from it, like so -
x[:, np.newaxis, :] - x
Sample run -
In [6]: x
Out[6]:
array([[6, 5, 3],
[4, 3, 5],
[0, 6, 7],
[8, 4, 1]])
In [7]: x[:,None,:] - x
Out[7]:
array([[[ 0, 0, 0],
[ 2, 2, -2],
[ 6, -1, -4],
[-2, 1, 2]],
[[-2, -2, 2],
[ 0, 0, 0],
[ 4, -3, -2],
[-4, -1, 4]],
[[-6, 1, 4],
[-4, 3, 2],
[ 0, 0, 0],
[-8, 2, 6]],
[[ 2, -1, -2],
[ 4, 1, -4],
[ 8, -2, -6],
[ 0, 0, 0]]])

Categories

Resources