indices

Can anybody explain me the numpy.indices()?

Can anybody explain me the numpy.indices()? Question: I’ve read documentation several times about np.indices() but I can’t seem to grasp what is it about. I’ve used it numerous times on things to see what it does, but I still can’t really get it. Maybe the thing is I’m a beginner in programming so I can’t …

Total answers: 3

Python – what is this syntax? my_2d_list[0:20,1]

Python – what is this syntax? my_2d_list[0:20,1] Question: Having a tough time googling or fiddling around with the interpreter to find out what this thing is inside the brackets. Here’s the code in context: from matplotlib.mlab import PCA as mlabPCA mlab_pca = mlabPCA(all_samples.T) print(‘PC axes in terms of the measurement axes’ ‘ scaled by the …

Total answers: 1

Find the indices of elements greater than x

Find the indices of elements greater than x Question: Given the following vector, a = [1, 2, 3, 4, 5, 6, 7, 8, 9] I need to identify the indices of “a” whose elements are >= than 4, like this: idx = [3, 4, 5, 6, 7, 8] The info in “idx” will be used …

Total answers: 7

NumPy k-th diagonal indices

NumPy k-th diagonal indices Question: I’d like to do arithmetics with k-th diagonal of a numpy.array. I need those indices. For example, something like: >>> a = numpy.eye(2) >>> a[numpy.diag_indices(a, k=-1)] = 5 >>> a array([[ 1., 0.], [ 5., 1.]]) Unfortunately, diag_indices only returns the indices comprising the main diagonal, so at the moment …

Total answers: 6

Splitting a string by list of indices

Splitting a string by list of indices Question: I want to split a string by a list of indices, where the split segments begin with one index and end before the next one. Example: s = ‘long string that I want to split up’ indices = [0,5,12,17] parts = [s[index:] for index in indices] for …

Total answers: 4

compare two lists in python and return indices of matched values

compare two lists in python and return indices of matched values Question: For two lists a and b, how can I get the indices of values that appear in both? For example, a = [1, 2, 3, 4, 5] b = [9, 7, 6, 5, 1, 0] return_indices_of_a(a, b) would return [0,4], with (a[0],a[4]) = …

Total answers: 4

How to find indices of non zero elements in large sparse matrix?

How to find indices of non zero elements in large sparse matrix? Question: i have two sq matrix (a, b) of size in order of 100000 X 100000. I have to take difference of these two matrix (c = a-b). Resultant matrix ‘c’ is a sparse matrix. I want to find the indices of all …

Total answers: 6

How to find all occurrences of an element in a list

How to find all occurrences of an element in a list Question: index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list for an element? Asked By: Bruce || Source Answers: You can use a list comprehension with enumerate: indices = …

Total answers: 18

How to get the index of a maximum element in a NumPy array along one axis

How to get the index of a maximum element in a NumPy array along one axis Question: I have a 2 dimensional NumPy array. I know how to get the maximum values over axes: >>> a = array([[1,2,3],[4,3,1]]) >>> amax(a,axis=0) array([4, 3, 3]) How can I get the indices of the maximum elements? I would …

Total answers: 5

Returning random element from a Python array based on search criteria

Returning random element from a Python array based on search criteria Question: Apologies if this is straightforward, but I’ve been looking for a little while now and can’t find a simple, efficient solution. I have a two-dimensional Python list of lists which only consists of 1’s and 0’s. e.g.: a=[[0,1,0],[0,1,1],[1,0,1]] I wish to return, at …

Total answers: 5