Python 基于groupby的数据帧排序精度

Python 基于groupby的数据帧排序精度,python,Python,我想做的是根据算法对所有数据集进行排序,特别是根据每个算法的准确度得分 现在我有: 我的预期行为是: 对于银行数据集,第一行应该是随机森林,因为其精度最好为0.842,然后是0.8384的叠加,然后是0.836的xgboost,依此类推 然后我想对第二个数据集得到相同的结果 以下数据集作为数据帧提供: report_df_scoring = pd.DataFrame({'Dataset': {0: 'Bank', 1: 'Bank', 2: 'Bank', 3: 'Bank',

我想做的是根据算法对所有数据集进行排序,特别是根据每个算法的准确度得分

现在我有:

我的预期行为是: 对于银行数据集,第一行应该是随机森林,因为其精度最好为0.842,然后是0.8384的叠加,然后是0.836的xgboost,依此类推

然后我想对第二个数据集得到相同的结果

以下数据集作为数据帧提供:

report_df_scoring = pd.DataFrame({'Dataset': {0: 'Bank',
  1: 'Bank',
  2: 'Bank',
  3: 'Bank',
  4: 'Bank',
  5: 'Bank',
  6: 'Bank',
  7: 'Bank',
  8: 'Cell2Cell',
  9: 'Cell2Cell',
  10: 'Cell2Cell',
  11: 'Cell2Cell',
  12: 'Cell2Cell',
  13: 'Cell2Cell',
  14: 'Cell2Cell',
  15: 'Cell2Cell'},
 'Algorithm': {0: 'LogisticRegressionNoSMOTE',
  1: 'Logistic Regression',
  2: 'SVM-linear',
  3: 'SVM-rbf',
  4: 'xgboost',
  5: 'GaussianNB',
  6: 'RandomForest',
  7: 'stacked',
  8: 'LogisticRegressionNoSMOTE',
  9: 'Logistic Regression',
  10: 'SVM-linear',
  11: 'SVM-rbf',
  12: 'xgboost',
  13: 'GaussianNB',
  14: 'RandomForest',
  15: 'stacked'},
 'W-Precision': {0: 0.8159638339642141,
  1: 0.8229500536388679,
  2: 0.8243426658647828,
  3: 0.7956512785333915,
  4: 0.8288351219512194,
  5: 0.8302513223140496,
  6: 0.8318761238761239,
  7: 0.8357318199248408,
  8: 0.6453332232903468,
  9: 0.657153039784181,
  10: 0.5641184628317989,
  11: 0.6207332779376712,
  12: 0.6466095048295256,
  13: 0.6274024966096714,
  14: 0.6350781137558213,
  15: 0.6490390100373438},
 'W-Recall': {0: 0.8324,
  1: 0.7636,
  2: 0.7628,
  3: 0.8056,
  4: 0.836,
  5: 0.8176,
  6: 0.8420000000000001,
  7: 0.8384,
  8: 0.7119573734524369,
  9: 0.5691114245416079,
  10: 0.4761009246199655,
  11: 0.7105469362168939,
  12: 0.6676069581570286,
  13: 0.7012223789374706,
  14: 0.6806143237737032,
  15: 0.7036514652875725},
 'W-F1-score': {0: 0.810103868755423,
  1: 0.7811452562742854,
  2: 0.7807117770916884,
  3: 0.7997335148514852,
  4: 0.831622605929424,
  5: 0.7598757585104978,
  6: 0.8346719018404907,
  7: 0.8369700532427321,
  8: 0.6120020301499476,
  9: 0.5897535881716193,
  10: 0.5012036339285898,
  11: 0.6019956967503604,
  12: 0.6547804871944608,
  13: 0.6235533708965149,
  14: 0.6456762711012908,
  15: 0.646126294149303},
 '0-Precision': {0: 0.8493518104604381,
  1: 0.9187236604455148,
  2: 0.9206541490006056,
  3: 0.8634596695821186,
  4: 0.8834146341463415,
  5: 0.8152892561983471,
  6: 0.8791208791208791,
  7: 0.8933200398803589,
  8: 0.7178499799438428,
  9: 0.7789978348283328,
  10: 0.6867191115224434,
  11: 0.7149551266777857,
  12: 0.7462792345854005,
  13: 0.7204730967849409,
  14: 0.7326366410801812,
  15: 0.7301806864355495},
 '0-Recall': {0: 0.958627648839556,
  1: 0.7699293642785066,
  2: 0.7669021190716448,
  3: 0.8965691220988901,
  4: 0.9137235116044399,
  5: 0.9954591321897074,
  6: 0.9283551967709384,
  7: 0.9041372351160444,
  8: 0.9825409026023938,
  9: 0.5530910288788844,
  10: 0.4888547271329746,
  11: 0.9884704073789392,
  12: 0.8093774019984628,
  13: 0.9498188206873832,
  14: 0.8698803118480289,
  15: 0.9274184693093224},
 '0-F1-score': {0: 0.9006873666745674,
  1: 0.8377710678012626,
  2: 0.8367740159647675,
  3: 0.8797029702970298,
  4: 0.8983134920634921,
  5: 0.8964107223989097,
  6: 0.9030674846625768,
  7: 0.8986960882647943,
  8: 0.8295939180419062,
  9: 0.6468888460797535,
  10: 0.5711353431686979,
  11: 0.8297538943681445,
  12: 0.7765486725663717,
  13: 0.8194003694406291,
  14: 0.7953815261044176,
  15: 0.8170649124504208},
 '1-Precision': {0: 0.6882129277566539,
  1: 0.4564958283671037,
  2: 0.4558303886925795,
  3: 0.5361990950226244,
  4: 0.62,
  5: 0.8875,
  6: 0.6511056511056511,
  7: 0.6153846153846154,
  8: 0.4646464646464646,
  9: 0.35355781448538753,
  10: 0.2586399108138239,
  11: 0.3859649122807017,
  12: 0.3982668977469671,
  13: 0.3955026455026455,
  14: 0.3919958953309389,
  15: 0.4468619246861925},
 '1-Recall': {0: 0.34942084942084944,
  1: 0.7393822393822393,
  2: 0.747104247104247,
  3: 0.4575289575289575,
  4: 0.5386100386100386,
  5: 0.13706563706563707,
  6: 0.5115830115830116,
  7: 0.5868725868725869,
  8: 0.037756497948016415,
  9: 0.6090287277701778,
  10: 0.4443228454172367,
  11: 0.01805745554035568,
  12: 0.3143638850889193,
  13: 0.08180574555403558,
  14: 0.20902872777017786,
  15: 0.14610123119015048},
 '1-F1-score': {0: 0.4635083226632522,
  1: 0.5644804716285925,
  2: 0.5662033650329188,
  3: 0.49375,
  4: 0.5764462809917356,
  5: 0.2374581939799331,
  6: 0.5729729729729729,
  7: 0.6007905138339921,
  8: 0.06983805668016195,
  9: 0.4473922218872475,
  10: 0.3269579222870949,
  11: 0.03450078410872975,
  12: 0.3513761467889908,
  13: 0.13557016549535253,
  14: 0.2726623840114205,
  15: 0.22020618556701027},
 'Accuracy': {0: 0.8324,
  1: 0.7636,
  2: 0.7628,
  3: 0.8056,
  4: 0.836,
  5: 0.8176,
  6: 0.8420000000000001,
  7: 0.8384,
  8: 0.7119573734524369,
  9: 0.5691114245416079,
  10: 0.4761009246199655,
  11: 0.7105469362168939,
  12: 0.6676069581570286,
  13: 0.7012223789374706,
  14: 0.6806143237737032,
  15: 0.7036514652875725},
 'AUC': {0: 0.6540242491302027,
  1: 0.754655801830373,
  2: 0.7570031830879459,
  3: 0.6770490398139237,
  4: 0.7261667751072393,
  5: 0.5662623846276723,
  6: 0.719969104176975,
  7: 0.0,
  8: 0.5101487002752051,
  9: 0.5810598783245311,
  10: 0.4665887862751056,
  11: 0.5032639314596475,
  12: 0.561870643543691,
  13: 0.5158122831207095,
  14: 0.5394545198091034,
  15: 0.0},
 'SMOTE': {0: 'No',
  1: 'Yes',
  2: 'Yes',
  3: 'Yes',
  4: 'Yes',
  5: 'Yes',
  6: 'Yes',
  7: 'Yes',
  8: 'No',
  9: 'Yes',
  10: 'Yes',
  11: 'Yes',
  12: 'Yes',
  13: 'Yes',
  14: 'Yes',
  15: 'Yes'},
 'top3var': {0: "['numofproducts_4', 'numofproducts_3', 'geography_germany']",
  1: "['numofproducts_4', 'numofproducts_3', 'geography_germany']",
  2: "['numofproducts_4', 'numofproducts_3', 'age']",
  3: "['empty']",
  4: "['numofproducts_2', 'numofproducts_1', 'isactivemember']",
  5: "['empty']",
  6: "['age', 'balance', 'numofproducts_2']",
  7: 'empty',
  8: "['retentioncalls_4', 'madecalltoretentionteam', 'currentequipmentdays']",
  9: "['retentioncalls_4', 'madecalltoretentionteam', 'currentequipmentdays']",
  10: "['receivedcalls', 'retentioncalls_0', 'retentionoffersaccepted_0']",
  11: "['empty']",
  12: "['newcellphoneuser', 'notnewcellphoneuser', 'handsetrefurbished']",
  13: "['empty']",
  14: "['currentequipmentdays', 'monthsinservice', 'directorassistedcalls']",
  15: 'empty'}})
我所尝试的:

report_df_scoring.sort_valuaes(["Accuracy"], ascending=True).groupby(["Dataset", "Algorithm"]).first()
你可以做:

report_df_scoring.reset_index().sort_values(['Dataset', 'Accuracy'], ascending=[True, False]).set_index(['Dataset', 'Algorithm'])