首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字典中值的平均值

字典中值的平均值
EN

Stack Overflow用户
提问于 2022-04-19 15:14:22
回答 3查看 39关注 0票数 0

我有一本名为model_scores_for_datasets的字典,如下所示:

代码语言:javascript
复制
{'Unprocessed': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '1.000', 'K-Nearest Neighbour': '1.000', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Standardisation': {'Logistic Regression': '0.933', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.967', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Normalisation': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.967', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Rescale': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}}
{'Unprocessed': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '1.000', 'K-Nearest Neighbour': '1.000', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Standardisation': {'Logistic Regression': '0.933', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.967', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Normalisation': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.967', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Rescale': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}}

我想得到字典列表中每本字典的平均值。总共有4个“未处理的标准化规范化逐步加强”和8个如下所示的总度量:

代码语言:javascript
复制
{'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '1.000', 'K-Nearest Neighbour': '1.000', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}

因此,这四个量表中的每一个都有8种不同的ML语言,我想得到一个平均值,例如,平均“标准化”得分最高,所以它将在机器学习过程中使用。

这是代码,但它给了我一个错误:TypeError: can't convert type 'str' to numerator/denominator

代码语言:javascript
复制
avgDict = model_scores_for_datasets
for st,vals in avgDict.items():
    print(st,(vals))
    #print (st)
    for st,vals in avgDict.items():
        print("Average for {} is {}".format(st,mean(vals)))
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-04-19 15:28:08

代码语言:javascript
复制
import numpy as np
for mode in results.keys():
    mean = np.mean([float(value) for value in results[mode].values()])
    print(f"{mode}: {mean}")

退出:

代码语言:javascript
复制
Unprocessed: 0.9624999999999999
Standardisation: 0.9542499999999999
Normalisation: 0.9584999999999999
Rescale: 0.9542499999999999

对于PythonCrazy

代码语言:javascript
复制
print({mode: np.mean([float(value) for value in results[mode].values()]) for mode in results.keys()})
票数 1
EN

Stack Overflow用户

发布于 2022-04-19 15:20:31

首先,您必须转换为正确的类型:

代码语言:javascript
复制
avgDict = model_scores_for_datasets
#conversion
avgDict=dict(zip(avgDict.keys(),list(map(float,avgDict.keys())))

for st,vals in avgDict.items():
    print(st,(vals))
    #print (st)
    for st,vals in avgDict.items():
        print("Average for {} is {}".format(st,mean(vals)))

产出:

代码语言:javascript
复制
Average for Logistic Regression is 0.967
Average for Support Vector Machine is 0.967
Average for Decision Tree is 0.933
Average for Random Forest is 0.933
Average for LinearDiscriminant is 1.0
Average for K-Nearest Neighbour is 1.0
Average for Naive Bayes is 0.967
Average for XGBoost is 0.933
票数 1
EN

Stack Overflow用户

发布于 2022-04-19 15:31:10

一个容易理解的解决方案是:

代码语言:javascript
复制
data = {'Unprocessed': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '1.000', 'K-Nearest Neighbour': '1.000', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Standardisation': {'Logistic Regression': '0.933', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.967', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Normalisation': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.967', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}, 'Rescale': {'Logistic Regression': '0.967', 'Support Vector Machine': '0.967', 'Decision Tree': '0.933', 'Random Forest': '0.933', 'LinearDiscriminant': '0.967', 'K-Nearest Neighbour': '0.967', 'Naive Bayes': '0.967', 'XGBoost': '0.933'}}

dicts = list(data.keys())
keys = list(data['Unprocessed'].keys())

r = {}
for k in keys:
    r[k] = sum([float(data[d][k]) for d in dicts])/len(dicts)
    
print(r)
#{'Logistic Regression': 0.9585, 'Support Vector Machine': 0.967, 'Decision Tree': 0.933, 'Random Forest': 0.95, 'LinearDiscriminant': 0.9752500000000001, 'K-Nearest Neighbour': 0.9752500000000001, 'Naive Bayes': 0.967, 'XGBoost': 0.933}

同样,如果您想按字典进行平均值:

代码语言:javascript
复制
r2 = {}
for d in dicts:
    r2[d] = sum([float(data[d][k]) for k in keys])/len(keys)
    
print(r2)
#{'Unprocessed': 0.9624999999999999, 'Standardisation': 0.9542499999999999, 'Normalisation': 0.9584999999999999, 'Rescale': 0.9542499999999998}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71927295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档