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.
Related
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]]
I have two matrices of the same size, A, B. I want to use the columns of B to acsses the columns of A, on a per column basis. For example,
A = np.array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
and
B = np.array([[0, 0, 2],
[1, 2, 1],
[2, 1, 0]])
I want something like:
A[B] = [[1, 4, 9],
[2, 6, 8],
[3, 5, 7]]
I.e., I've used the j'th column of B as indices to the j'th column of A.
Is there any effiecnt way of doing so?
Thanks!
You can use advanced indexing:
A[B, np.arange(A.shape[0])]
array([[1, 4, 9],
[2, 6, 8],
[3, 5, 7]])
Or with np.take_along_axis:
np.take_along_axis(A, B, axis=0)
array([[1, 4, 9],
[2, 6, 8],
[3, 5, 7]])
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]])
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]]
This question already has answers here:
using an numpy array as indices of the 2nd dim of another array? [duplicate]
(2 answers)
Closed 4 years ago.
Given a 2D numpy array A and a 1D numpy array col with the same number of rows and values ranging the columns in A, how can I get a new 1D array that selects the corresponding column from each row in A?
Example:
np.random.seed(0)
A = np.random.randint(10, size=(5, 3))
A
# array([[5, 0, 3],
# [3, 7, 9],
# [3, 5, 2],
# [4, 7, 6],
# [8, 8, 1]])
col = np.random.randint(A.shape[1], size=A.shape[0])
col
# array([2, 2, 0, 1, 1])
Based on col, I'd like to get element 2 from rows 0 and 1, element 0 from row 2, and 1 from rows 3 and 4: [3, 9, 3, 7, 8].
The normal indexing doesn't work:
A[col]
# array([[3, 5, 2],
# [3, 5, 2],
# [5, 0, 3],
# [3, 7, 9],
# [3, 7, 9]])
A[:, col]
# array([[3, 3, 5, 0, 0],
# [9, 9, 3, 7, 7],
# [2, 2, 3, 5, 5],
# [6, 6, 4, 7, 7],
# [1, 1, 8, 8, 8]])
This works but is very slow for large arrays:
A[:, col].diagonal()
# array([3, 9, 3, 7, 8])