如何识别值列表的较低和较高中位数邻居

时间:2016-02-24 02:55:32

标签: python numpy

我有以下值列表:

$sql_param = array();
    $params = array();

    $query = "SELECT campname,url,views,leads,subs,country,affid FROM camp WHERE";

if (isset($_POST['fromdatetime']) && isset($_POST['todatetime'])) {
    $datefrom = $_POST['fromdatetime'].":00";
    $dateto = $_POST['todatetime'].":59";

    $sql_param[] = $datefrom;
    $sql_param[] = $dateto;
    $bindparam = "ss";
    $query .= "datevisit between (?) AND (?)";
}

if (isset($_POST['affidcode'])) {
    $affidcode = $_POST['affidcode'];
    $sql_param[] = $affidcode;
    $bindparam .= "s";
    $query .= " AND affid = (?)";
}

$sql_param_count = count($sql_param);
$params[] = &$bindparam;

for($i = 0; $i < $sql_param_count; $i++) {
      $params[] = &$sql_param[$i];
    }

    /* create a prepared statement */
    $stmt = mysqli_prepare($mysqli, $query);

    call_user_func_array(array($stmt, 'mysqli_stmt_bind_param'), $params); 

    /* execute query */
    $status = mysqli_stmt_execute($stmt);

它有中位数:

 my_vals_sorted =    
 [0.04701098816483717,
 0.047837747941765346,
 0.048203210075420284,
 0.050858702201435485,
 0.05517778062513831, # lower median neighbor
 0.06254807568026952, # higher median neighbor
 0.08835915310130199,
 0.09745027658063653,
 0.10855616681703671,
 0.3939978988121587]

如上所述,我想确定较低和较高的中位数邻居。屈服

  import numpy as np
  np.median(my_vals_sorted)
  #0.058862928152703914

我该怎么做?

2 个答案:

答案 0 :(得分:2)

从Python 3.4开始,有一个statistics模块:

import statistics
statistics.median_low(my_vals_sorted)
statistics.median_high(my_vals_sorted)

median_lowmedian_high可以处理未排序的数据(首先对它们进行排序)。如果数据已经排序,正如您的变量名所暗示的那样,或者如果您正在使用早期的python,那么您可能比复制statistics的{​​{3}}更糟糕。下面的代码主要是从那里复制的,经过一些修改(删除排序,并将StatisticsError替换为通用ValueError):

def median_low(data):
    n = len(data)
    if n == 0:
        raise ValueError("no median for empty data")
    if n % 2 == 1:
        return data[n//2]
    else:
        return data[n//2 - 1]


def median_high(data):
    n = len(data)
    if n == 0:
        raise ValueError("no median for empty data")
    return data[n//2]

答案 1 :(得分:-3)

我认为您可以为此使用常规索引,即

my_vals_sorted[int(len(my_vals_sorted)/2)]
my_vals_sorted[int(len(my_vals_sorted)/2 + 1)]