indices

In TensorFlow, how can I get nonzero values and their indices from a tensor with python?

帅比萌擦擦* 提交于 2019-12-03 08:27:21
问题 I want to do something like this. Let's say we have a tensor A. A = [[1,0],[0,4]] And I want to get nonzero values and their indices from it. Nonzero values: [1,4] Nonzero indices: [[0,0],[1,1]] There are similar operations in Numpy. np.flatnonzero(A) return indices that are non-zero in the flattened A. x.ravel()[np.flatnonzero(x)] extract elements according to non-zero indices. Here's a link for these operations. How can I do somthing like above Numpy operations in Tensorflow with python?

Cumulative summation of a numpy array by index

≡放荡痞女 提交于 2019-12-03 06:13:08
Assume you have an array of values that will need to be summed together d = [1,1,1,1,1] and a second array specifying which elements need to be summed together i = [0,0,1,2,2] The result will be stored in a new array of size max(i)+1 . So for example i=[0,0,0,0,0] would be equivalent to summing all the elements of d and storing the result at position 0 of a new array of size 1 . I tried to implement this using c = zeros(max(i)+1) c[i] += d However, the += operation adds each element only once, thus giving the unexpected result of [1,1,1] instead of [2,1,2] How would one correctly implement

In TensorFlow, how can I get nonzero values and their indices from a tensor with python?

时光毁灭记忆、已成空白 提交于 2019-12-02 22:15:12
I want to do something like this. Let's say we have a tensor A. A = [[1,0],[0,4]] And I want to get nonzero values and their indices from it. Nonzero values: [1,4] Nonzero indices: [[0,0],[1,1]] There are similar operations in Numpy. np.flatnonzero(A) return indices that are non-zero in the flattened A. x.ravel()[np.flatnonzero(x)] extract elements according to non-zero indices. Here's a link for these operations. How can I do somthing like above Numpy operations in Tensorflow with python? (Whether a matrix is flattened or not doesn't really matter.) You can achieve same result in Tensorflow

Simpler way to create a matrix/list of indices?

跟風遠走 提交于 2019-12-02 19:45:54
问题 I wonder what could be the easiest way to create a bi-dimensional array, that has for each row the indices to another multi-dimensional array. For example, let's say I have a cube 4x4, the "indices matrix" would be the following: np.concatenate([ np.expand_dims(curr.ravel(),axis=0).T for curr in np.meshgrid( np.arange(4), np.arange(4), np.arange(4) ) ],axis=1) with the following result: array([[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [1, 0, 0], [1, 0, 1], ... [2, 3, 2], [2, 3, 3], [3, 3, 0

How to stack indices and given values

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 16:59:30
问题 seq1_values = [5, 40, 180, 13, 30, 20, 25, 24, 29, 31, 54, 46, 42, 50, 67, 17, 76, 33, 84, 35, 100, 37, 110, 32, 112, 15, 123, 3, 130, 42] def get_num_values(): global seq1_values return len( seq1_values ) def get_values(): global seq1_values for value in seq1_values: yield value yield None def next_value(): return next( next_value.values ) next_value.values = get_values() def main(): numbers = [] value = next_value() while value is not None: numbers.append(value) input_size = len(numbers)-1

Search parallel rows of a structures for common items in matlab

大城市里の小女人 提交于 2019-12-02 08:56:10
问题 I've stored (row,col,val) information of key1: (1,1) (1,2) (1,3) (4,2) (3,4) attribute1: 2 3 4 2 5 as follows: Structure A1: key row1: 1 1 1 4 3 key col1: 1 2 3 2 4 attribute1: 2 3 4 2 5 Similarly, for structure A2 Structure A2: key row2: 2 2 1 3 key col2: 1 2 3 4 attribute2: 1 0 1 5 Now, I'd like to be able to search for common entries in both row and column key items between structure A1 and A2 simultaneously. That is conceptually find [common_item, index]=intersect([row2,col2],[row1,col1])

Simpler way to create a matrix/list of indices?

霸气de小男生 提交于 2019-12-02 08:29:00
I wonder what could be the easiest way to create a bi-dimensional array, that has for each row the indices to another multi-dimensional array. For example, let's say I have a cube 4x4, the "indices matrix" would be the following: np.concatenate([ np.expand_dims(curr.ravel(),axis=0).T for curr in np.meshgrid( np.arange(4), np.arange(4), np.arange(4) ) ],axis=1) with the following result: array([[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [1, 0, 0], [1, 0, 1], ... [2, 3, 2], [2, 3, 3], [3, 3, 0], [3, 3, 1], [3, 3, 2], [3, 3, 3]]) Besides the fact that it seems that the second column should be in

How to stack indices and given values

╄→尐↘猪︶ㄣ 提交于 2019-12-02 08:20:30
seq1_values = [5, 40, 180, 13, 30, 20, 25, 24, 29, 31, 54, 46, 42, 50, 67, 17, 76, 33, 84, 35, 100, 37, 110, 32, 112, 15, 123, 3, 130, 42] def get_num_values(): global seq1_values return len( seq1_values ) def get_values(): global seq1_values for value in seq1_values: yield value yield None def next_value(): return next( next_value.values ) next_value.values = get_values() def main(): numbers = [] value = next_value() while value is not None: numbers.append(value) input_size = len(numbers)-1 for i in range(input_size): print("|", str(input_size).rjust(4), end="") for j in range(input_size):

Generate NumPy array containing the indices of another NumPy array

和自甴很熟 提交于 2019-12-02 05:31:37
I'd like to generate a np.ndarray NumPy array for a given shape of another NumPy array. The former array should contain the corresponding indices for each cell of the latter array. Example 1 Let's say we have a = np.ones((3,)) which has a shape of (3,) . I'd expect [[0] [1] [2]] since there is a[0] , a[1] and a[2] in a which can be accessed by their indices 0 , 1 and 2 . Example 2 For a shape of (3, 2) like b = np.ones((3, 2)) there is already very much to write. I'd expect [[[0 0] [0 1]] [[1 0] [1 1]] [[2 0] [2 1]]] since there are 6 cells in b which can be accessed by the corresponding

Python: Finding corresponding indices for an intersection of two lists

若如初见. 提交于 2019-12-01 20:26:23
问题 This is somewhat related to a question I asked not too long ago today. I am taking the intersection of two lists as follows: inter = set(NNSRCfile['datetimenew']).intersection(catdate) The two components that I am taking the intersection of belong to two lengthy lists. Is it possible to get the indices of the intersected values? (The indices of the original lists that is). I'm not quite sure where to start with this one. Any help is greatly appreciated! 回答1: I would create a dictionary to