Extract data from numpy array to create another one - python

Assume I have the following numpy array :
a = np.array([[4, 5, 8],
[7, 2, 9],
[1, 5, 3]])
and I want to extract points from the array 'a' to have this array :
b = array([[4, 8],
[1, 3]])
How can I do this ?
PS : In my real case I have 13*13 matrix and I want to create a 3*3 matrix from the first one

You can use np.ix_() for this to create a map of which values you want by location.
>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> b=np.ix_([0,2],[0, 2])
>>> a[b]
array([[1, 3],
[7, 9]])

Related

Forming a 3D np array from a 2D array

Let's say I have a 2D array:
L = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
I would like to make a 3D array from this, using a parameter N, such that (in this example, let's say N=4)
L2 = np.array([[[1,1,1,1],[2,2,2,2],[3,3,3,3]],
[[4,4,4,4],[5,5,5,5],[6,6,6,6]],
[[7,7,7,7],[8,8,8,8],[9,9,9,9]]])
Is there a nice way of doing this?
One option is to add another dimension, then repeat along the new dimension.
N = 4
out = L[..., None].repeat(N, axis=-1)
Output:
array([[[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]],
[[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6]],
[[7, 7, 7, 7],
[8, 8, 8, 8],
[9, 9, 9, 9]]])
You can use a combination of swapaxes and broadcast_to:
N = 4
L2 = np.broadcast_to(L.swapaxes(0, 1), (N, *reversed(L.shape))).swapaxes(0, 2)
Output will be as desired.

Numpy Matrix Remove row that equals to an array

I have a numpy matrix with 2 axis (row and columns) and an array.
I want to remove the row in the matrix that equals to the array.
For example, if the matrix is
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
And the array is [1, 2, 3], then the output should be:
[[4, 5, 6],
[7, 8, 9]]
Use:
a[~(a == b).all(1)]
Example:
a = np.arange(1, 10).reshape((3, 3))
b = np.arange(1, 4)
a[~(a == b).all(1)]
array([[4, 5, 6],
[7, 8, 9]])

how to add one column array to already existing array with shape 10000*17

Please help me here, I am new to python. I am trying to add an array of one column to another array of shape 10000*17. Below are the samples. Please help me to concatenate two arrays:
[[ 0.02061183, -0.90794402, 0.92005926, 0.5405426 , 4.85191978, 3.65479782],
[-0.52203821, .50416184, -0.87750086, -0.625578 , -0.08879011, -0.57718519]]
I need to add a new row of array [1,2,3,4,5,6] to the above array.
You can use numpy.insert(arr, i, the_object_to_be_added, axis):
import numpy as np
my_array = [[1, 2, 3],
4, 5, 6]]
np.insert(my_array, 2, [7, 8, 9], axis=0)
# output my_array = [[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]]
or
my_array = [[1, 2, 3],
[4, 5, 6]]
np.insert(my_array, 1, [7, 8], axis=1)
# output my_array = [[1, 7, 2, 3],
# [4, 8, 5, 6]]

Flattening an array of matrices to a single matrix (python)

I have a list of matrices:
arr = [array([[1, 2, 3], [7, 8, 9]]), array([[4, 5, 6], [0, 0, 1]])]
I want to flatten them in the following way:
[[1, 2, 3], [7, 8, 9], [4, 5, 6], [0, 0, 1]]
numpy.flatten flattens it into a single array of numbers.
I tried this: flattened_list = [y for x in arr for y in x]
It does the job, but all rows of the matrix are numpy arrays.
Is there any way to flatten numpy arrays upto a certain depth?
You should use reshape:
out = arr.reshape((4,3))
What you want is the vstack function from numpy. It takes a tuple of ndarrays and returns a new ndarray which is the result of stacking them vertically with the first ndarray being on top and so on.
For example:
import numpy as np
>>> a = np.array([1, 2])
>>> b = np.array([3, 4])
>>> c = np.array([5, 6])
>>> np.vstack(a, b)
array([[1, 2],
[3, 4],
[5, 6]])
In your case you can easily call the tuple function on your list of ndarrays
>>> arr = [array([[1, 2, 3], [7, 8, 9]]), array([[4, 5, 6], [0, 0, 1]])]
>>> np.vstack(tuple(arr))
array([[1, 2, 3],
[7, 8, 9],
[4, 5, 6],
[0, 0, 1]])
If you want your answer as a python list then just call numpy's ndarray.tolist function on the result like so:
>>> np.ndarray.tolist(np.vstack(arr))
[[1, 2, 3], [7, 8, 9], [4, 5, 6], [0, 0, 1]]

Swap columns of an ndarray

I am trying to swap two columns from a 2d array such that
a = array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
becomes:
b = array([[1, 3, 2],
[4, 6, 5],
[7, 9, 8]])
How can I do that?
This would do it:
b = a[:, [0, 2, 1]]
It works by providing a list of column indices in the second-dimension position. As always in Python, the indices are zero-based, so the first (leftmost) column is 0 and the third (rightmost, last) column is 2.

Categories