1

在尝试使用 Python Pandas 中的拆分-应用-组合范例时,我注意到一些奇怪的行为 - 变量x不会采用正确的长度!

我要完成的工作的概述:

  1. 使用 groupby() 操作将包含多个公司数据的数据框分成组(每个公司一个)
  2. 计算每组的信息盈余
  3. 返回带有结果的新数据框

对于第一组,计算是完美的!但是,在第二组上,切片数据帧时似乎继承了前一个变量的长度 - 这是为什么呢?正如您从代码和提供的调试中看到的那样,我试图找出问题但看不到它,任何帮助将不胜感激!

函数调用:

data_nasdaq_top_100_preprocessed_mi_res = sentisignal.split_apply_combine(data_nasdaq_top_100_preprocessed_merge, 'SYMBOL', sentisignal.information_surplus, 10, 'PCA_SENTIMENT', 'PCA_FINANCE', -1, True)

调试(和问题):

第一组:

['AAL'] exante  True
len(df.index) 928
shift_x length 928
len(x.index) 927 len(x) 927 end_index 927
len(x) 927
doane 927
success

第二组:

['AAPL'] exante  True
len(df.index) 1006
shift_x length 1006
len(x.index) 78 len(x) 78 end_index 1005
len(x) 78
doane 78
success

这就是问题所在 - 由于某种原因,长度x减少到 78(应该是 1005!),神秘的是 1006 - 928 = 78(第一组的长度 - 第二组的长度)

编码:

拆分应用组合方法

def split_apply_combine(df, key, func, *args):
# print "args:", args
return df.groupby(key).apply(func, *args)

信息盈余法(带调试)

def information_surplus(df, time_shift, varx, vary, bins, exante):
print df.SYMBOL.unique(), "exante ", exante

output = []

if exante:
    shift_range = range(0, -(time_shift+1), -1)
else:
    shift_range = range(0, time_shift+1)

print "len(df.index)", len(df.index)

for i in shift_range:
    if abs(i) > len(df.index):
        break

    shift_x = df[varx].shift(i)
    print "shift_x length", len(shift_x)

    if exante:
        end_index = (len(shift_x.index) - 1 - abs(i))
        x = shift_x.ix[1:end_index]
        y = df[vary].ix[1:end_index]
        print "len(x.index)", len(x.index), "len(x)", len(x), "end_index", end_index
    else:
        print "exec"
        x = shift_x.ix[1+abs(i):]
        y = df[vary].ix[1+abs(i):]

    mi = calc_mutual_information(x, y, bins)

    if i == 0:
        mi_origin = mi

    if mi_origin == 0: 
        inf_surp_pct = 0
    else:
        inf_surp_pct = (mi - mi_origin) / mi_origin * 100

    output.append({'SHIFT': i, 'MUTUAL_INFORMATION': mi, 'INFORMATION_SURPLUS_DIFF': mi - mi_origin, 'INFORMATION_SURPLUS_PCT': inf_surp_pct})

output_frame = pd.DataFrame(output)
return output_frame

互信息法

def calc_mutual_information(x, y, bins):
print "len(x)", len(x)

try:
    if bins == -1:
        bins = doane_bin(x)
    if bins == np.inf:
        bins = sturges_bin(x)
except ValueError:
    bins = 10.0
# print "bins", bins
try:
    c_xy = np.histogram2d(x, y, bins)[0]
    mi = metrics.mutual_info_score(None, None, contingency=c_xy)
    print "success"
except Exception,e: 
    print "error with mi calc", str(e)
    mi = 0
return mi
4

1 回答 1

0

这有效:

更新了要使用的剩余信息.ix,我已经更新了问题标题以尝试反映这一点,并很乐意接受带有解释的答案!

更新的方法:

def information_surplus(df, time_shift, varx, vary, bins, exante):
print df.SYMBOL.unique(), "exante ", exante

output = []

if exante:
    shift_range = range(0, -(time_shift+1), -1)
else:
    shift_range = range(0, time_shift+1)

# print "len(df.index)", len(df.index)

for i in shift_range:
    if abs(i) > len(df.index):
        break

    shift_x = df[varx].shift(i)
    # print "shift_x length", len(shift_x)

    mi = 0.0

    if exante:
        end_index = (len(shift_x.index) - 1 - abs(i))
        x = shift_x[1:end_index]
        y = df[vary][1:end_index]
        # print "len(x.index)", len(x.index), "len(x)", len(x), "end_index", end_index
    else:
        # print "exec"
        x = shift_x.ix[1+abs(i):]
        y = df[vary].ix[1+abs(i):]

    mi = calc_mutual_information(x, y, bins)

    if i == 0:
        mi_origin = mi

    if mi_origin == 0: 
        inf_surp_pct = 0
    else:
        inf_surp_pct = (mi - mi_origin) / mi_origin * 100

    output.append({'SHIFT': i, 'MUTUAL_INFORMATION': mi, 'INFORMATION_SURPLUS_DIFF': mi - mi_origin, 'INFORMATION_SURPLUS_PCT': inf_surp_pct})

output_frame = pd.DataFrame(output)
return output_frame
于 2016-03-15T21:46:03.827 回答