numpy - Kotlin 是否有类似于 argmax 方法的东西?
问题描述
所以假设我有一个像这样的numpy数组:
import numpy as np
mat = np.array([[4, 8, 1], [5, 10, 6]])
print(np.argmax(mat)) # prints 4
print(np.argmax(mat, axis=1)) # prints [1 1], index of maximum values along the rows
Kotlin 是否有类似的(内置)功能?我找到了NumPy 的 Kotlin 绑定,但我没有找到实现的功能。
提前致谢!
解决方案
使用withIndex()
和maxByOrNull()
:
fun <T : Comparable<T>> Iterable<T>.argmax(): Int? {
return withIndex().maxByOrNull { it.value }?.index
}
推荐阅读
- linux - 在 bash 文件中为 mosquitto 代理使用密码文件
- hibernate - 在Spring中以相同的形式上传具有另一个对象的文件
- game-physics - 在与线碰撞后求出球的 X 和 Y 速度
- wordpress - WooCommerce 中的“无效电子邮件”
- google-drive-api - Google Drive API -- 根 ID
- wordpress - 如何获得正确的 docker-compose Multiline 环境变量格式?
- c++ - 相同的程序在 C 中有效,但在 C++ 中无效(使用 linux 系统调用)
- typescript - 类型“ProxyTargetUrl”错误中不存在属性“主机”。即使我在 JS 文件中获得 IntelliSense
- sql-server - 拒绝对存储过程中的表的选择权限 - SQL Server
- arrays - MIPS将字符放入字符串中的位置正在删除字符串的堆地址