在python中使用numpy.argsort对列表的每一列进行排序

我正在尝试使用numpy.argsort对列表的每一列进行排序。但是,我得到的输出与正确的排序不匹配。我要排序的列表名称是“ CD”。 下面,我提供了到目前为止使用的代码和错误的输出。

  

print(numpy.argsort(CD,axis=0))

CD列出了下面的一张。

[[90,85,71,48],[28,75,2,71],[5,93,15,56],[59,91,29,43],24,82,35],[13,102,77,21],[85,33,64],[80,66,64,30],[91,78,41,1],[77,30,50]]

,输出为

[[2 4 1 8]
 [5 9 2 5]
 [1 7 3 7]
 [4 1 9 4]
 [3 8 6 3]
 [9 0 8 0]
 [7 3 7 9]
 [6 2 0 2]
 [0 5 5 6]
 [8 6 4 1]]

如果您能给出可能出现问题的提示,我将不胜感激。

编辑

我想获取已排序列的索引。不是实际数字。抱歉没有首先清除掉

a53932991 回答:在python中使用numpy.argsort对列表的每一列进行排序

一种有效(但很慢)的方法来获取您想要的东西:

import { Component,ElementRef,Renderer,ViewChild,Renderer2 } from '@angular/core';

@Component({
  selector: 'my-app',templateUrl: './app.component.html',styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  left = 0;
  @ViewChild('parentTag',{static: false})
  parentTag: ElementRef; 

  @ViewChild('target',{static: false})
  target: ElementRef;

  constructor(private el:ElementRef,private renderer: Renderer2){

  } 

  move(){
    console.log(this.parentTag.nativeElement.clientWidth); 
    console.log(this.target.nativeElement.scrollWidth);
    let left = this.target.nativeElement.scrollWidth - 
    this.parentTag.nativeElement.clientWidth; 
    this.renderer.setStyle(this.target.nativeElement,'margin-left','-'+left+'px');
  }

  stop(){
    this.renderer.setStyle(this.target.nativeElement,'0px');
  }

}

您实际上想要的是一个逆argsort,您可以在here上找到很多信息

,

numpy.argsort返回已排序数组的索引

我认为您想要的是numpy.sort

print(numpy.sort(CD,axis=0))
# [[  5  24   2   1]
#  [ 13  33  15  21]
#  [ 28  66  29  30]
#  [ 28  75  30  35]
#  [ 59  78  33  43]
#  [ 77  85  41  48]
#  [ 80  91  64  50]
#  [ 85  93  71  56]
#  [ 90 102  77  64]
#  [ 91 102  82  71]]
,

也许您只想sort(返回数组的排序副本)而不是argsort(返回将对数组排序的索引)?

np.sort(cd,axis=1)
#array([[ 48,71,85,90],#       [  2,28,75],#       [  5,15,56,93],#       [ 29,43,59,91],#       [ 24,35,82],#       [ 13,21,77,102],#       [ 33,64,#       [ 30,66,80],#       [  1,41,78,33,50,77]])
np.sort(cd,axis=0)
#array([[  5,24,2,1],21],#       [ 28,29,30],75,30,35],#       [ 59,43],#       [ 77,48],#       [ 80,91,50],#       [ 85,93,56],#       [ 90,102,64],#       [ 91,82,71]])
本文链接:https://www.f2er.com/3144720.html

大家都在问