代码之家  ›  专栏  ›  技术社区  ›  Chris T.

将概率估计转化为索引序列

  •  1
  • Chris T.  · 技术社区  · 9 年前

    reference_id
       184932    0.531842
       185362    0.401958
       185361    0.105928
       185338    0.018572
                 ...
       276499    0.208567
       276500    0.818759
       269851    0.018528
       Name: response, dtype: float32

    我实现了以下Python代码:

    y_score_lr = LogisticRegression(C=10).fit(X_train, y_train).predict_proba(X_test)[:,1]
    y_proba = y_score_lr

    array([ 0.05225495,  0.00522493,  0.07369773, ...,  0.06994582, 0.06995239,  0.12659022])

    这是一个numpy数组。

    但我不确定这个数组是否真的与原始X\u测试数据帧中的相应reference\u id匹配,我还没有想出如何将其转换为我在本文开头提到的索引“序列”。

    我也试过使用

    y_score_lr = LogisticRegression(C=10).fit(X_train, y_train).predict_proba(X_test)[:,1]
    y_proba = y_score_lr.tolist()

    非常感谢。

    真诚地

    1 回复  |  直到 9 年前
        1
  •  1
  •   E.Z    9 年前

    首先,是的,它与 X_test :第一行对应于 y_proba 大堆

    其次,有几种方法可以解决这个问题。

    假设您愿意,可能的解决方案之一可能是: dtype=pandas.Series

    import pandas as pd
    import numpy as np
    
    y_proba_indexed = pd.Series(
        data=y_proba, index=X_test['reference_id'], name='response', dtype=np.float32)
    
    print(y_proba_indexed)
    

    这会给你这样的东西:

    84932     0.531842
    185362    0.401958
    185361    0.105928
    185338    0.018572
          ....
    276499    0.208567
    276500    0.818759
    269851    0.018528
    Name: response, dtype: float32
    

    reference_id = 185338 您可以键入: y_proba_indexed.loc[[185338]] ,输出为:

    185338    0.018572
    Name: respone, dtype: float32
    

    推荐文章