argmax

np.argmax on multidimensional arrays, keeping some indexes fixed

雨燕双飞 提交于 2019-12-11 08:56:58
问题 I have a collection of 2D narrays, depending on two integer indexes, say p1 and p2, with each matrix of the same shape. Then I need to find, for each pair (p1,p2), the maximum value of the matrix and the indexes of these maxima. A trivial, albeit slow, way to do this would would be to do something like this import numpy as np import itertools range1=range(1,10) range2=range(1,20) for p1,p2 in itertools.product(range1,range1): mat=np.random.rand(10,10) index=np.unravel_index(mat.argmax(), mat

Get indices of numpy.argmax elements over an axis

泄露秘密 提交于 2019-12-10 03:48:58
问题 I have N-dimensional matrix which contains the values for a function with N parameters. Each parameter has a discrete number of values. I need to maximize the function over all parameters but one, resulting in a one-dimensional vector of size equal to the number of values of the non-maximized parameter. I also need to save which values are taken by the other parameters. To do so I wanted to iteratively apply numpy.max over different axes to reduce the dimensionality of the matrix to find what

tf.argmax()解析

可紊 提交于 2019-12-09 23:41:50
tf.argmax(input,axis)根据axis取值的不同返回每行或者每列最大值的索引。代码如下: import tensorflow as tfimport numpy as npsess=tf.Session()a = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])a0=tf.argmax(a,axis=0)a1=tf.argmax(a,axis=1)a0=sess.run(a0)a1=sess.run(a1)b = np.array([[[1, 2, 3,5], [2, 3, 4,8], [5, 1,4, 3]], [[13 ,4, 3,5], [2,13, 4,8],[8, 4, 7, 32]] ])b0=tf.argmax(b,axis=0)b1=tf.argmax(b,axis=1)b2=tf.argmax(b,axis=2)b0=sess.run(b0)b1=sess.run(b1)b2=sess.run(b2)print('矩阵为二维度状况:\n')print('first dimension=',a0)print('second dimension=',a1)print('矩阵为三个维度状况:\n')print('first dimension=',b0)print('second

Argmax on a tensor and ceiling in Tensorflow

帅比萌擦擦* 提交于 2019-12-08 12:20:45
问题 Suppose I have a tensor in Tensorflow that its values are like: A = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]] How can I change this tensor into the following: B = [[1, 0, 0],[0, 0, 1]] In other words I want to just keep the maximum and replace it with 1. Any help would be appreciated. 回答1: I think that you can solve it with a one-liner: import tensorflow as tf import numpy as np x_data = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]] # I am using hard-coded dimensions for simplicity x = tf.placeholder(dtype=tf

How to get rows in pandas data frame, with maximal values in a column and keep the original index?

爱⌒轻易说出口 提交于 2019-12-06 07:10:44
问题 I have a pandas data frame. In the first column it can have the same value several times (in other words, the values in the first column are not unique). Whenever I have several rows that contain the same value in the first column, I would like to leave only those that have maximal value in the third column. I almost found a solution: import pandas ls = [] ls.append({'c1':'a', 'c2':'a', 'c3':1}) ls.append({'c1':'a', 'c2':'c', 'c3':3}) ls.append({'c1':'a', 'c2':'b', 'c3':2}) ls.append({'c1':'b

cumulative argmax of a numpy array

故事扮演 提交于 2019-12-06 00:32:00
Consider the array a np.random.seed([3,1415]) a = np.random.randint(0, 10, (10, 2)) a array([[0, 2], [7, 3], [8, 7], [0, 6], [8, 6], [0, 2], [0, 4], [9, 7], [3, 2], [4, 3]]) What is a vectorized way to get the cumulative argmax? array([[0, 0], <-- both start off as max position [1, 1], <-- 7 > 0 so 1st col = 1, 3 > 2 2nd col = 1 [2, 2], <-- 8 > 7 1st col = 2, 7 > 3 2nd col = 2 [2, 2], <-- 0 < 8 1st col stays the same, 6 < 7 2nd col stays the same [2, 2], [2, 2], [2, 2], [7, 2], <-- 9 is new max of 2nd col, argmax is now 7 [7, 2], [7, 2]]) Here is a non-vectorized way to do it. Notice that as

MNIST手写数字识别程序

匆匆过客 提交于 2019-12-05 09:06:16
实验环境: python3.6.3 pip 9.0.1 tensorflow 1.10.0 window 10 oracle vm virtualbox ubuntu 16.0.1 1.基于tensorflow对mnist预测,需要连接外网 。 下面代码可以直接复制去调试,识别率高达98%,最低也在91%。python对代码格式有非常高的要求。行头不能同时存在tab和空格。函数内行头对齐。大概有3/40分钟左右.不过我在8月20号训练结果不是这样,最高也就97%,最低89%。不清楚其中原因 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = input_data.read_data_sets("/dataset",one_hot=True) #每一批数据大小 batch_size = 100 #计算多少批数据 n_batch = mnist.train.num_examples #定义两个placeholder,None=100,28*28=784,即100行784列 x = tf.placeholder(tf.float32,[None,784]) #0-9个输出标签 y = tf.placeholder(tf.float32,[None

Get indices of numpy.argmax elements over an axis

ぐ巨炮叔叔 提交于 2019-12-05 04:42:31
I have N-dimensional matrix which contains the values for a function with N parameters. Each parameter has a discrete number of values. I need to maximize the function over all parameters but one, resulting in a one-dimensional vector of size equal to the number of values of the non-maximized parameter. I also need to save which values are taken by the other parameters. To do so I wanted to iteratively apply numpy.max over different axes to reduce the dimensionality of the matrix to find what I need. The final vector will then depend on just the parameter I left out. I'm however having trouble

Pandas: Resample dataframe column, get discrete feature that corresponds to max value

故事扮演 提交于 2019-12-04 14:04:18
Sample data: import pandas as pd import numpy as np import datetime data = {'value': [1,2,4,3], 'names': ['joe', 'bob', 'joe', 'bob']} start, end = datetime.datetime(2015, 1, 1), datetime.datetime(2015, 1, 4) test = pd.DataFrame(data=data, index=pd.DatetimeIndex(start=start, end=end, freq="D"), columns=["value", "names"]) gives: value names 2015-01-01 1 joe 2015-01-02 2 bob 2015-01-03 4 joe 2015-01-04 3 bob I want to resample by '2D' and get the max value, something like: df.resample('2D') The expected result should be: value names 2015-01-01 2 bob 2015-01-03 4 joe Can anyone help me? You can

How to get rows in pandas data frame, with maximal values in a column and keep the original index?

女生的网名这么多〃 提交于 2019-12-04 12:53:40
I have a pandas data frame. In the first column it can have the same value several times (in other words, the values in the first column are not unique). Whenever I have several rows that contain the same value in the first column, I would like to leave only those that have maximal value in the third column. I almost found a solution: import pandas ls = [] ls.append({'c1':'a', 'c2':'a', 'c3':1}) ls.append({'c1':'a', 'c2':'c', 'c3':3}) ls.append({'c1':'a', 'c2':'b', 'c3':2}) ls.append({'c1':'b', 'c2':'b', 'c3':10}) ls.append({'c1':'b', 'c2':'c', 'c3':12}) ls.append({'c1':'b', 'c2':'a', 'c3':7})