如何找到特定列的值最大的行?
df.max() 会给我每列的最大值,我不知道如何得到相应的行.
我是Keras的初学者,需要帮助才能理解keras.argmax(a,axis = -1)和keras.max(a,axis = -1).当a.shape =(19,19,5,80)时,axis = -1的含义是什么?
如果你能回答keras.argmax(a,axis = -1)和keras.max(a,axis = -1)的输出,我将不胜感激.
提前致谢
- 年轻
我无法理解的输出argmax,并argmin在与轴参数使用.例如:
>>> a = np.array([[1,2,4,7], [9,88,6,45], [9,76,3,4]])
>>> a
array([[ 1, 2, 4, 7],
[ 9, 88, 6, 45],
[ 9, 76, 3, 4]])
>>> a.shape
(3, 4)
>>> a.size
12
>>> np.argmax(a)
5
>>> np.argmax(a,axis=0)
array([1, 1, 1, 1])
>>> np.argmax(a,axis=1)
array([3, 1, 1])
>>> np.argmin(a)
0
>>> np.argmin(a,axis=0)
array([0, 0, 2, 2])
>>> np.argmin(a,axis=1)
array([0, 2, 2])
Run Code Online (Sandbox Code Playgroud)
如您所见,最大值是点(1,1),最小值是点(0,0).所以在我运行的逻辑中:
np.argmin(a,axis=0) 我期望 array([0,0,0,0]) np.argmin(a,axis=1) 我期望 array([0,0,0]) np.argmax(a,axis=0) 我期望 array([1,1,1,1]) np.argmax(a,axis=1) 我期望 array([1,1,1]) 我对事物的理解有什么问题?
我想按列找到矩阵中值的argmax,例如:
1 2 3 2 3 3
4 5 6 ->
3 7 8
Run Code Online (Sandbox Code Playgroud)
我觉得我应该能够在列上映射argmax/posmax函数,但我没有看到在Octave中执行此操作的特别直观的方法.
所以我想制作一个数据帧片,然后设置该片中第一个项的值而不复制数据帧.例如:
df = pandas.DataFrame(numpy.random.rand(3,1))
df[df[0]>0][0] = 0
Run Code Online (Sandbox Code Playgroud)
这里的切片是无关紧要的,仅用于示例,并将再次返回整个数据帧.重点是,通过这样做,就像在示例中,您获得了带有复制警告的设置(可以理解).我还尝试先切片,然后使用ILOC/IX/LOC并使用ILOC两次,例如:
df.iloc[df[0]>0,:][0] = 0
df[df[0]>0,:].iloc[0] = 0
Run Code Online (Sandbox Code Playgroud)
这些都不起作用.再次 - 我不想复制数据框,即使它只是切片版本.
编辑:似乎有两种方法,使用掩码或IdxMax.如果索引是唯一的,IdxMax方法似乎有效,如果不是,则掩码方法.在我的情况下,索引不是唯一的,我在最初的帖子中忘了提到.
我正在argmax研究 PyTorch 的功能,其定义为:
torch.argmax(input, dim=None, keepdim=False)
Run Code Online (Sandbox Code Playgroud)
考虑一个例子
a = torch.randn(4, 4)
print(a)
print(torch.argmax(a, dim=1))
Run Code Online (Sandbox Code Playgroud)
在这里,当我使用 dim=1 而不是搜索列向量时,该函数会搜索行向量,如下所示。
print(a) :
tensor([[-1.7739, 0.8073, 0.0472, -0.4084],
[ 0.6378, 0.6575, -1.2970, -0.0625],
[ 1.7970, -1.3463, 0.9011, -0.8704],
[ 1.5639, 0.7123, 0.0385, 1.8410]])
print(torch.argmax(a, dim=1))
tensor([1, 1, 0, 3])
Run Code Online (Sandbox Code Playgroud)
就我的假设而言,dim = 0 代表行,dim = 1 代表列。
我经常需要根据产生double或int值的标准的最大化来收集集合的最大元素.Streams有max()函数,需要我实现一个比较器,我觉得很麻烦.是否有更简洁的语法,例如names.stream().argmax(String::length)在以下示例中?
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ArgMax
{
public static void main(String[] args)
{
List<String> names = Arrays.asList("John","Joe","Marilyn");
String longestName = names.stream().max((String s,String t)->(Integer.compare(s.length(),t.length()))).get();
System.out.println(longestName);
}
}
Run Code Online (Sandbox Code Playgroud) 在Python中,有numpy.argmax:
In [7]: a = np.random.rand(5,3)
In [8]: a
Out[8]:
array([[ 0.00108039, 0.16885304, 0.18129883],
[ 0.42661574, 0.78217538, 0.43942868],
[ 0.34321459, 0.53835544, 0.72364813],
[ 0.97914267, 0.40773394, 0.36358753],
[ 0.59639274, 0.67640815, 0.28126232]])
In [10]: np.argmax(a,axis=1)
Out[10]: array([2, 1, 2, 0, 1])
Run Code Online (Sandbox Code Playgroud)
是否有朱莉娅类似于Numpy的argmax?我只找到了一个indmax,它只接受一个向量,而不是二维数组np.argmax.
我有DataFrame考虑Name并Date具有单元中的权重值:
Name Jan17 Jun18 Dec18 Apr19 count
Nick 0 1.7 3.7 0 2
Jack 0 0 2.8 3.5 2
Fox 0 1.7 0 0 1
Rex 1.0 0 3.0 4.2 3
Snack 0 0 2.8 4.4 2
Yosee 0 0 0 4.3 1
Petty 0.5 1.3 2.8 3.5 4
Run Code Online (Sandbox Code Playgroud)
Start并Finish应参考下一个定义添加到dataFrame中:
Start行中的第一个非零值从Jan17
列开始Apr19 Finish序列中的第一个非零值,Apr19直到Jan17同样,如果row在行中只有一个非零值,则Start和Finish 是相同的。
为了找到行中的第一个非零元素,我尝试了data[col].keys, np.argmax()它,并按预期工作。
date_col_list = ['Jan17','Jun18','Dec18', 'Apr19'] …
该base哈斯克尔库具有以下类型同义词在Data.Semigroup:
type ArgMin a b = Min (Arg a b)
type ArgMax a b = Max (Arg a b)
Run Code Online (Sandbox Code Playgroud)
这两种类型的同义词的目的是什么?在哪里可以有效地使用它们?
解释 argmin 和 argmax 函数在数学中的作用以及它们与这些类型同义词的关系可能会有所帮助。
这里有一些额外的信息,因此您不必跳到 Hackage。
这是 的定义Arg:
-- | 'Arg' isn't itself a 'Semigroup' in its own right, but it can be
-- placed inside 'Min' and 'Max' to compute an arg min or arg max.
data Arg a b = Arg a b
Run Code Online (Sandbox Code Playgroud)
它的文档字符串表明ArgMinandArgMax …