从数据帧中提取子集

2024-05-29 11:36:34 发布

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

我有一个熊猫数据框,如下所示:

 index  Validation_Set  Topics   Alpha       Beta  Coherence
 0      75% Corpus      14         0.5        0.5   0.501483
 1      75% Corpus      14         0.5  symmetric   0.481676
 2     100% Corpus      14  asymmetric        0.5   0.500620
 3     100% Corpus      14         0.5  symmetric   0.492288
 4      75% Corpus      12         0.5        0.5   0.511823
 5      75% Corpus      12         0.5  symmetric   0.477614
 6     100% Corpus      12  asymmetric        0.5   0.489424
 7     100% Corpus      12         0.5  symmetric   0.541270
 8      75% Corpus       4         0.5        0.5   0.515683
 9      75% Corpus       4         0.5  symmetric   0.430614
10     100% Corpus       4  asymmetric        0.5   0.489324
11     100% Corpus       4         0.5  symmetric   0.473570

等等。。。这些是一些参数调整测试的结果

现在,我只想提取关于最佳模型的所有信息(所有参数测试),即在完整验证集(100%语料库)上达到最高“一致性”值的模型(或者可能不止一个)

在本例中,我将得到[错误,请参见编辑]

 index  Validation_Set  Topics   Alpha       Beta  Coherence
 7     100% Corpus      12         0.5  symmetric   0.541270

我以这种方式设法检索到“一致性”值最高的行(df是完整的数据帧):

corpus_100 = df[df['Validation_Set']=='100% Corpus']
topics_num = df.iloc[[corpus_100['Coherence'].idxmax()]]['Topics'].values[0]
opt_model = corpus_100[corpus_100['Topics']==topics_num]

它正在发挥作用,但确实是一团糟,然后我正在寻找一种更清晰的方法来实现这一点

谢谢大家!

编辑:我真的很抱歉,但所需输出中有一个输入错误,实际上是:

 4      75% Corpus      12         0.5        0.5   0.511823
 5      75% Corpus      12         0.5  symmetric   0.477614
 6     100% Corpus      12  asymmetric        0.5   0.489424
 7     100% Corpus      12         0.5  symmetric   0.541270

Tags: 数据模型alphadf参数indexcorpusbeta
2条回答

试试这个

df[df['Coherence']==df['Coherence'].max()]

df[df['column']==value]过滤数据帧以查找您要查找的内容

df['column']max()返回“列”中的最大值

将它们放在一起将返回一致性最大值的数据帧行

看起来nlargest()正是您需要的

df[df['Validation_Set']=='100% Corpus'].nlargest(1,'Coherence')

    index   Validation_Set  Topics  Alpha   Beta        Coherence
    7       100%Corpus      12      0.5     symmetric   0.54127

相关问题 更多 >

    热门问题