Simplfy row AND column extraction, numpy [duplicate] - python

This question already has answers here:
Subsetting a 2D numpy array
(5 answers)
Closed 7 years ago.
I wish to extract rows and columns from a matrix using a single "fancy" slice, is this possible?
m = matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
My target is
matrix([[1, 3],
[7, 9]])
Where I have a list of the items I want
d = [0,2]
I can achieve the functionality by
m[d][:,d]
But is there a simpler expression?

You can do this using numpy.ix_:
m = matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = [0,2]
print m[ix_(d,d)]
which will emit:
[[1 3]
[7 9]]

Related

Per line numpy roll [duplicate]

This question already has answers here:
Roll rows of a matrix independently
(6 answers)
Closed 4 months ago.
Is there an elegant way to perform a per-line roll of a numpy array?
Example:
>>> arr
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> desired_func(arr, shift=np.array([1, 2]))
array([[4, 1, 2, 3],
[7, 8, 5, 6]])
The function np.roll doesn't seem to allow that.
You can use fancy indexing:
# define the roll per row
roll = np.array([1, 2])
# compute the rolled indices
idx = (np.arange(arr.shape[1]) - roll[:,None]) % arr.shape[1]
# index as 2D
out = arr[np.arange(arr.shape[0])[:,None], idx]
output:
array([[4, 1, 2, 3],
[7, 8, 5, 6]])

Identifying unique index numbers from a list [duplicate]

This question already has answers here:
Get unique values in List of Lists
(6 answers)
Find unique rows in numpy.array
(20 answers)
Count Distinct Values in a List of Lists
(5 answers)
Closed 6 months ago.
I have a list A containing an array with indices. I want to print all the unique index numbers by scanning each [i,j]. In [0,3], i=0,j=3. I present the expected output.
import numpy as np
A=[np.array([[[0, 1],
[0, 3],
[1, 3],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 7],
[6, 4]]])]
The expected output is
A=[0,1,3,4,5,6,7]
numpy has this very cool function np.unique.
Simply to get the output A=[0,1,3,4,5,6,7]
B = np.unique(A[0])
Output :
[0 1 3 4 5 6 7]
A=[[0, 1],[0, 3],[1, 3],[3, 4],[3, 6],[4, 5],[4, 7],[5, 7],[6, 4]]
K = []
for _ in range(len(A)):
K.extend(A[_])
print(set(K))
OUTPUT:
{0, 1, 3, 4, 5, 6, 7}
A is basically a list and within list you make an array.
Do this:
def unique(A):
x = np.array(A)
print(np.unique(x))
unique(A)
if A is an array we can simply do this:
np.unique(A)

How to split an array into chunks of a given length in python? [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 1 year ago.
What is the fastest and shortest method to turn this:
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for example into this:
ids = [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
by giving the input 2 as the fixed length.
Of course there are some easy ways to make this but none of them occur to me as fast or pretty.
Why don't you try out a list comprehension?
Example:
[ids[i:i+2] for i in range(0,len(ids),2)]
Output:
[[1, 2], [3, 4], [5, 6], [7, 8], [9]]
bad but effective:
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9]
new_ids = []
function = lambda x, y: [x, y]
for i in range(0, len(ids), 2):
try:
new_ids.append(function(ids[i], ids[i + 1]))
except IndexError:
new_ids.append(ids[i])
ids = new_ids
print(ids)

How to slice a multi-dimensional array with another multi-dimensional array? [duplicate]

This question already has answers here:
Indexing one array by another in numpy
(4 answers)
Closed 3 years ago.
I have, say, a 2D-array:
x = np.array([[4, 5, 6],
[7, 8, 9]])
and another one with indexes:
a = np.array([[0, 1],
[1, 2]])
How do I slice each row of x using the indexes in each respective row in a without using a loop in order to obtain:
[[4, 5]
[8, 9]]
Try this :
import numpy as np
x = np.array([[4, 5, 6],
[7, 8, 9]])
a = np.array([[0, 1],
[1, 2]])
print(np.take_along_axis(x,a,1))
You can use numpy.take_along_axis
np.take_along_axis(x,a,1)
# array([[4, 5],
# [8, 9]])
or manually add the first coordinate (broadcasting applies)
x[np.c_[:2],a]
# array([[4, 5],
# [8, 9]])
I know it is technically a loop, but you can do it in one line with a list comprehension.
print(np.array([x[i][a[i]] for i in range(0, x.shape[0])]))

Double elements in Python Numpy Array [duplicate]

This question already has answers here:
Repeating each element of a numpy array 5 times
(2 answers)
Closed 4 years ago.
I'm trying to copy every element into a 2d array in Python such that it doubles the size of the array and adds the element directly after the element that it intends to copy.
For example:
[[1,2,3],
[4,5,6],
[7,8,9]]
becomes
[[1,1,2,2,3,3],
[4,4,5,5,6,6],
[7,7,8,8,9,9]]
Can anyone help with this problem? Thanks!
You can use np.repeat(..) [numpy-doc] for this:
>>> import numpy as np
>>> np.repeat(a, 2, axis=1)
array([[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6],
[7, 7, 8, 8, 9, 9]])
We thus repeat, for the second axis (axis=1) the elements two times.
We can also use list-comprehension, but given that the data has the same time, using numpy is faster, and more declarative:
times2 = [[xi for x in row for xi in [x, x]] for row in a]
This the produces:
>>> [[xi for x in row for xi in [x, x]] for row in a]
[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6], [7, 7, 8, 8, 9, 9]]

Categories