查找与中位数和numpy相差最大的索引

时间:2018-11-12 23:04:38

标签: python numpy functional-programming median argmax

我试图找到离群数的索引号。根据与中位数的差异 我能够获得正确的高数字,但是只要低数字是异常值,我就只会得到高数字。

import numpy as np

def findoutlier(lis):

  outliermax = np.absolute(np.max(lis) - np.median(lis))
  outliermin = np.absolute(np.min(lis) - np.median(lis))
  if outliermax > outliermin:
     argmax = np.argmax(lis, axis = 1)
     return argmax
  else:
     argmin = np.argmin(lis, axis = 1)
     return argmin

def main():
  Matx = np.array([[10,3,2],[1,2,6]])   
  print(findoutlier(Matx))

  threeMatx = np.array([[1,10,2,8,5],[2,7,3,9,11],[19,2,1,1,5]])
  print(findoutlier(threeMatx))

main()

1 个答案:

答案 0 :(得分:1)

使用中位数,最大值和最小值时需要指定轴:

import numpy as np


def findoutlier(lis):
    omaxs = np.absolute(np.max(lis, axis=1) - np.median(lis, axis=1))
    omins = np.absolute(np.min(lis, axis=1) - np.median(lis, axis=1))

    return [np.argmax(l) if omax > omin else np.argmin(l)  for omax, omin, l in  zip(omaxs, omins, lis)]


def main():
    mat_x = np.array([[10, 3, 2], [1, 2, 6]])
    print(findoutlier(mat_x))

    three_mat_x = np.array([[1, 10, 2, 8, 5], [2, 7, 3, 9, 11], [19, 2, 1, 1, 5]])
    print(findoutlier(three_mat_x))

输出

[0, 2]
[1, 0, 0]

更新

如@ user3483203所述,您可以使用numpy.where

import numpy as np


def findoutlier(lis):
    omaxs = np.absolute(np.max(lis, axis=1) - np.median(lis, axis=1))
    omins = np.absolute(np.min(lis, axis=1) - np.median(lis, axis=1))

    return np.where(omaxs > omins, np.argmax(lis, axis=1), np.argmin(lis, axis=1))


def main():
    mat_x = np.array([[10, 3, 2], [1, 2, 6]])
    print(findoutlier(mat_x))

    three_mat_x = np.array([[1, 10, 2, 8, 5], [2, 7, 3, 9, 11], [19, 2, 1, 1, 5]])
    print(findoutlier(three_mat_x))

main()

输出

[0 2]
[1 0 0]