如何将此ECDF表示函数从MATLAB转换为Python?

时间:2017-06-21 16:15:40

标签: python arrays matlab indexing tuples

我必须转换以下MATLAB代码:

% calculate ECDF from D at n points
function X = ECDF_representation(D, n)
  m = mean(D); X = [];
  for d=1:size(D,2),
      [f, x] = ecdf(D(:,d)+
          randn(size(D(:,d)))*0.01);
       ll=interp1(f,x,linspace(0,1,n),'cubic');
       X=[X ll];
   end
   X= [X m];
end

进入Python。这是我到目前为止所尝试的内容:

import numpy as np
from scipy import interpolate
from statsmodels.distributions.empirical_distribution import ECDF

def ecdf_representation(D, n):
    """calculate ECDF from D at n points"""
    m = np.mean(D)
    X = []
    for d in xrange(D.shape[1] + 1):
        f, x = ECDF([D[:, d] + np.random.randn(np.shape(D[:, d])) * 0.01])
        ll = interpolate.interp1d(f, x, np.linspace(0, 1, n), 'cubic')
        X = [X, ll]
    X = [X, m]
    return X

我收到错误:

in ecdf_representation
for d in xrange(D.shape[1] + 1):
IndexError: tuple index out of range

如果我将行for d in xrange(D.shape[1] + 1):切换为for d in xrange(D.shape[0] + 1):

我解决了原始错误但获得了一个新错误:

line 25, in ecdf_representation
f, x = ECDF([D[:, d] + np.random.randn(np.shape(D[:, d])) * 0.01])
IndexError: too many indices for array

编辑:

D是1-d数组,因为在data是pandas数据帧的下一个函数中返回了值。

我想我应该在ecdf_representation函数中返回其他内容,但我不确定是什么。

def segment_energy(data, th):
    mag = np.linalg.norm(data.loc[:, ['x', 'y', 'z']], axis=1)
    mag = np.array(mag)
    mag -= np.mean(mag)

    above = np.where(mag >= th * np.std(mag))
    indicator = np.zeros(mag.shape)
    indicator[above] = 1
    plt.plot(mag)
    plt.plot(indicator * 1000, 'r')
    plt.show()
    return indicator

0 个答案:

没有答案