Python中带数集的组合数学及相关计算

2024-09-30 08:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个组合数学的问题,我甚至很难用itertools概念化我将如何去做。我的数据集如下所示:

GoldMeasure Measure1 Measure2 Measure3 Measure4 Measure5 3+4-5

  30.501       -1      -1       -1        -1       -1     -1
  30.658       -1      -1        0        -1       -1      0
  31.281       -1               -1        -1        1     -3
  31.506        7      -1       -1         1       -1      1
  31.554       -1      -1       -1        -1       -1     -1
  31.613       -1      -1       -1         1               0
  31.838       -1      -1       -1        -1              -2
  31.954       -1      -1       -1         1       -1      1
  33.073        1       1                  1       -1      2
  33.592       -2      -2        2         0       -2      4

Coefficient: -0.054    0.119   0.690     0.474   -0.441  0.723

我试图找到1-5的最佳组合,它与黄金指标的皮尔逊相关系数最高。在上面的数据集中,最好的单个度量是度量3,其系数为0.69。但是,如果将度量值3与度量值4相加,然后减去度量值5,则得到一个新的组合度量值,其系数为0.72。本质上,我想确定这五个度量的所有可能组合的相关系数。这些组合可以是度量值的2/5、度量值的3/5(如上面的示例中所示)、度量值的4/5或所有五个度量值。你知道吗

秩序有点重要。确实,度量3、4和5的加法组合与度量5、3和4的加法组合是相同的;但是,我也尝试将算术运算符作为另一个选项(加法或减法)包含进来。因此,3+4+5与5+3+4相同,但3+4-5与5+3-4不同。你知道吗

任何指导或帮助都将不胜感激。实际数据集有46个度量值,如上面的示例数据集所示,有些度量值没有特定样本的值。测度的值既有正的也有负的,但没有界。你知道吗

事先谢谢你的帮助。你知道吗


Tags: 数据示例度量数学itertools系数黄金coefficient
2条回答
from collections import defaultdict
import statistics
import itertools
import scipy.stats
import heapq

#Please add a filename here and use a .csv format.
F=open('filename','r').readlines();

F[0].strip().split(',');
HEADER=[r.strip() for r in F[0].strip().split(',')[2:]];

DDF={};
DDP={};
DDN={};
DDG={};
#Please replace missing values using Excel into "NAA". I have replaced the "NAA"/missing values with average of the column. You may also consider ignoring missing rows before doing correlation between 2 columns.
for f in F[1:]:
    DATA=f.strip().split(',');
    ATAD=[[i.strip(),j.strip()] for i,j in zip(HEADER,DATA[2:])];
    for atad in ATAD:
        if atad[0].strip() in DDF.keys():
            DDF[atad[0].strip()].append(atad[1].strip());
        else:
            DDF[atad[0].strip()]=[atad[1].strip()];

    if F[0].strip().split(',')[1].strip() in DDG.keys():
        DDG[F[0].strip().split(',')[1].strip()].append(float(DATA[1].strip()));
    else:
        DDG[F[0].strip().split(',')[1].strip()]=[float(DATA[1].strip())];

for ke in DDF.keys():
    AVGP=statistics.mean([float(u) for u in DDF[ke.strip()] if u.strip()!='NAA']);
    NEWP=[float(nr.strip()) if nr.strip()!='NAA' else AVGP for nr in DDF[ke.strip()]];
    if ke in DDP.keys():
        DDP[ke.strip()]=NEWP;
    else:
        DDP[ke.strip()]=NEWP;
    AVGN=statistics.mean([-1*float(e) for e in DDF[ke.strip()] if e.strip()!='NAA']);
    NEWN=[-1*float(ne.strip()) if ne.strip()!='NAA' else AVGN for ne in DDF[ke.strip()]];
    if 'minus_'+ke.strip() in DDN.keys():
        DDN['minus_'+ke.strip()]=NEWN;
    else:
        DDN['minus_'+ke.strip()]=NEWN;
U=0;
L1=DDP.keys()+DDN.keys();
N=range(1,len(L1));
U=[0,0,-1];

for n in N:
    DDU=[];
    for subset in itertools.combinations(L1,n):
        S=[];
        SSET=[sset.strip().replace('minus_','') if sset.strip().startswith('minus_') else sset.strip() for sset in list(subset)];
        if len(set(SSET))>=len(subset):
            TMP=[DDN[p.strip()] if p.strip().startswith('minus_') else DDP[p.strip()] for p in subset];
            for y in range(0,len(TMP[0])):
                for x in TMP:
                    S.append(x[y]);
            SUM=[sum(S[w:w + n]) for w in range(0, len(S),n)];
            K=['+'.join(list(subset)).strip()]+[' vs Cq TREC']+list(scipy.stats.pearsonr(SUM,DDG['Cq TREC']));
            if str(K[-2])!='nan':
                DDU.append([K[-2],K]);
    DDU.sort(key=lambda x: x[0],reverse=True);
    for ea in DDU[0:3]:
        print repr(n).strip()+','+ea[1][0].strip().replace('+minus_','-').replace('minus_','-')+' '+ea[1][1].strip()+','+repr(ea[1][-2]).strip();

用五种方法来做这件事是可行的。你知道吗

from itertools import product
from numpy import negative, array_equal

choices = [1, 0, -1]
measures = 5
limit = len(choices)**measures//2+1
count = 0
measure_combinations = []
for p in product(*([choices]*5)):
    measure_combinations.append(list(p))
    count += 1
    if count == limit:
        break

print (len(measure_combinations))

例如,一个度量组合的内积,例如[1,1,0,-1,-1],与每一行度量列的内积将提供一系列值,GoldMeasure可以在这些值上进行回归以获得相关系数,这可以针对122个唯一的可能性进行。你知道吗

然而,根据{-1,0,1}的41次重复的部分乘积,41个度量将有18236498188585393202个独特的可能性。你知道吗

相关问题 更多 >

    热门问题