簡體   English   中英

Python/Pandas:在 dataframe 或字典中查找最小值

[英]Python/Pandas: Find min value in dataframe or dictionary

我在 for 循環中創建了一個字典,它給了我以下 192 個結果:

dic_aic = {0: 16.83024400288158,
1: 10.580792750644934,
2: 10.460203246761916,
3: 10.44309674334913,
4: 10.425859422774248,
        ...
191: 10.273789550619007,
192: 10.272853618268071}

當我將 plot 與 pandas 一起輸出時,我得到以下信息:

aic_df = pd.DataFrame(aic_dic.items(), columns=['Order', 'AIC'])
aic_df = aic_df.set_index('Order')
aic_df[1:].plot()

在此處輸入圖像描述

 min(aic_dic, key=aic_dic.get)

我的 dic 中的最小值為 192。但正如您在圖片中看到的那樣,AIC 在 192 和例如 96 之間的差異非常小,只有 10.272853618268071 - 10.28435108717606 = 0.01149 ... 我正在嘗試找到優化值大約 96 歲。有人知道如何用 python 解決這個問題嗎? 也許有積分?

您可以像剛才一樣先設置最小值。

然后你需要設置一個公差值,比如 0.1。

然后您可以返回滿足 min +- 容差的最小鍵值

minimum = min(aic_dic, key=aic_dic.get)  #your code here
tolerance = 0.1 #could be higher lower up to you
possible_answers = []
for key,value in your_dict.items():
    if minimum + tolerance > value:
          possible_answers(key)
print(min(possible_answers)) # you can adjust here as according to your need

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM