找出numpy array数组的最值及其索引
在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引
但在numpy中的array没有index方法,取而代之的是where,其又是list没有的
首先我们可以得到array在全局和每行每列的最大值(最小值同理)
a = np.arange(9).reshape((3,3))
a
array([[0, 1, 2],
[9, 4, 5],
[6, 7, 8]]) print(np.max(a)) #全局最大
8
print(np.max(a,axis=0)) #每列最大
[6 7 8]
print(np.max(a,axis=1)) #每行最大
[2 5 8]
然后用where得到最大值的索引,返回值中,前面的array对应行数,后者对应列数
print(np.where(a==np.max(a)))
(array([2], dtype=int64), array([2], dtype=int64))
print(np.where(a==np.max(a,axis=0)))
(array([2, 2, 2], dtype=int64), array([0, 1, 2], dtype=int64))
如果array中有相同的最大值,where会将其位置全部给出
a[1,0]=8
a
array([[0, 1, 2],
[8, 4, 5],
[6, 7, 8]])
print(np.where(a==np.max(a)))
(array([1, 2], dtype=int64), array([0, 2], dtype=int64))
参考文献: python中找出numpy array数组的最值及其索引
找出numpy array数组的最值及其索引的更多相关文章
- Class 找出一个整形数组中的元素的最大值
目的:找出一个整形数组中的元素的最大值 以下,我们用类和对象的方法来做. #include<iostream> using namespace std; class Array_m ...
- FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)
题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...
- 找出旋转有序数列的中间值python实现
题目给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2].假定数列中无重复元素,且数列长度为奇数.求出旋转数列的中间值.如数列 ...
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 对已有的2个一维数组,譬如说A[],B[],经过最少循环找出2个数组重复的元素。
import java.util.Arrays; /** * Created by ccc on 16-4-27. */ public class Test { public static void ...
- [小米OJ] 5. 找出旋转有序数列的中间值
排序,输出 #include <bits/stdc++.h> using namespace std; int main() { string input; while (cin > ...
- numpy ndarray求其最值的索引
import numpy as np a = np.array([1,2,3,4]) np.where(a== np.max(a)) >>>3 但假设其最值不止一个,如下 a = n ...
- 找出数组中求和等于y的所有子数组
算法记录: 给定一个数组x,每个元素都是正整数,找出其中满足条件"求和等于y"的所有子数组.(简化问题,每个元素都不相等) x=[x1,...,xn],暴力搜索,复杂度O(2^n) ...
- JavaScript的json和Array及Array数组的使用方法
1.关于json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集.也可以称为数据集和数组类似,能够存数据! //Ar ...
随机推荐
- 【大数据系列】HDFS初识
一.HDFS介绍 HDFS为了做到可靠性(reliability)创建了多分数据块(data blocks)的复制(replicas),并将它们放置在服务集群的计算节点中(compute nodes) ...
- jQuery缓存机制(四)
Data封装的方法的后面四个方法 和 dataAttr方法阅读. Data.prototype = { key: function( owner ) {}, set: function( owner, ...
- 题目1042:Coincidence(最长公共子序列 dp题目)
题目链接:http://ac.jobdu.com/problem.php?pid=1042 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- [原]git的使用(四)---撤销修改
8.撤销修改 $ cat readme.txt Git is a distributed version control system. Git is free software distribute ...
- GlusterFS六大卷模式說明
GlusterFS六大卷說明 第一,分佈卷 在分布式卷文件被随机地分布在整个砖的体积.使用分布式卷,你需要扩展存储,冗余是重要或提供其他硬件/软件层.(簡介:分布式卷,文件通过hash算法随机的分 ...
- 【转】RTMP/RTP/RTSP/RTCP协议对比与区别介绍
用一句简单的话总结:RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步. 之所以以前对这几个有点分不清,是因为CTC标准里没有对RTCP进行要求,因此在标准RTSP的代码 ...
- Unity3D笔记 英保通七 物理引擎
给球体添加刚体RigidBody和球体碰撞器Sphere Collider 效果: OnTriggerEnter() 代码 using UnityEngine; using System.Collec ...
- was cached in the local repository, resolution will not be reattempted until the update interval of localhost-repository has elapsed or updates are forced
ailed to collect dependencies at com.eshore:common:jar:0.0.1-SNAPSHOT: Failed to read artifact descr ...
- 【Win】使用L2TP出现809错误
1.环境:win7/10 2.解决: a.修改注册表(新建一个文本文件,复制以下内容,保存后文件扩展名改为.reg,双击运行文件.) Windows Registry Editor Version 5 ...
- Oracle创建测试表
试中文排序的数据库版本: SQL> select * from v$version; BANNER ----------------------------------------------- ...