对二维数组中的值进行排名 [英] Rank values in a 2D array

查看:85
本文介绍了对二维数组中的值进行排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想输入一个二维数组,然后创建一个新的排序数组,其中的值从最低到最高.

我使用的数组是转换为 numpyarray 的高程栅格.这个数组有一个高程值.我只想按海拔排序.以下是我使用代码的程度,但它没有生成我想要的!

import arcpy导入 numpy数组 = arcpy.RasterToNumPyArray(r"C:\Current\SedMod\SedModv3\TestModel.gdb\Smallprepfin_Clip")订单 = array.argsort()等级 = order.argsort()打印行列

感谢您的帮助!

补充说明:

我想将数组中包含的所有值从最大值到最小值排序,但要保持数组的索引和形状.

即如果有 100 个值,最高值(海拔)将是 1 级,最低 100 级是!

解决方案

这样的事情怎么样:

<预><代码>>>>一种数组([[ 0.76303184, 0.17748702, 0.89365504, 0.07221609],[ 0.12267359, 0.56999037, 0.42877407, 0.8875015],[ 0.38178661, 0.57648393, 0.47056551, 0.03178402],[ 0.03606595, 0.93597727, 0.02199706, 0.73906879]])>>>a.ravel().argsort().argsort().reshape(a.shape)数组([[12, 5, 14, 3],[ 4, 9, 7, 13],[ 6, 10, 8, 1],[ 2, 15, 0, 11]])

对此不太满意,可能有更好的方法.

Hi basically I want to input a 2D array and then create a new ranked array with the values from lowest to highest.

The array I am using is an elevation raster converted to a numpyarray. This array has a value for elevation. I only want to sort by elevation. Below is how far I got with the code, but it is not generating what I want!

import arcpy
import numpy

array = arcpy.RasterToNumPyArray(r"C:\Current\SedMod\SedModv3\TestModel.gdb\Smallprepfin_Clip")

order = array.argsort()
ranks = order.argsort()

print ranks

Thanks for any help!

Additional clarification:

I want to sort all the values contained within the array from the highest value to the lowest value but maintain the indices and shape of the array.

i.e the highest value(elevation) would be rank 1 and the lowest rank 100 if there are 100 values!

解决方案

How about something like this:

>>> a
array([[ 0.76303184,  0.17748702,  0.89365504,  0.07221609],
       [ 0.12267359,  0.56999037,  0.42877407,  0.8875015 ],
       [ 0.38178661,  0.57648393,  0.47056551,  0.03178402],
       [ 0.03606595,  0.93597727,  0.02199706,  0.73906879]])
>>> a.ravel().argsort().argsort().reshape(a.shape)
array([[12,  5, 14,  3],
       [ 4,  9,  7, 13],
       [ 6, 10,  8,  1],
       [ 2, 15,  0, 11]])

Not super happy with this, there is likely a better way.

这篇关于对二维数组中的值进行排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆