亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單次操作加載數據和平均值

單次操作加載數據和平均值

滄海一幻覺 2021-09-14 21:25:27
我正在嘗試將如下所示表單的數據加載到數據框中。popSize: 1000numSurvivors: 0tournamentSize: 10probMutation: 0.1probCrossover: 0.9numIters: 100Accuracy: 96.84 Error Rate: 3.16 Not Classified: 0.00Total time: 5.367popSize: 1000numSurvivors: 0tournamentSize: 10probMutation: 0.1probCrossover: 0.9numIters: 100Accuracy: 96.84 Error Rate: 3.16 Not Classified: 0.00Total time: 4.472popSize: 1000numSurvivors: 0tournamentSize: 10probMutation: 0.1probCrossover: 0.9numIters: 100Accuracy: 92.11 Error Rate: 7.89 Not Classified: 0.00Total time: 4.46數據代表算法的多次執行。有沒有辦法將這些數據作為單行加載,使用最后 4 個值的平均結果?
查看完整描述

2 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

這是一種使用itertools.groupby()and將數據整理到數據幀中的方法pandas:


from itertools import groupby

import pandas as pd


with open('test.txt', 'r') as f:


    chunks = [list(group) for k, group in groupby(f.readlines(), lambda x: x=='\n') if not k]


chunks = [dict([tuple(i.strip().split(': ')) for i in chunk]) for chunk in chunks]


df = pd.DataFrame(chunks).astype(float)

返回:


  Accuracy Error Rate Not Classified Total time numIters numSurvivors popSize  \

0    96.84       3.16           0.00      5.367      100            0    1000   

1    96.84       3.16           0.00      4.472      100            0    1000   

2    92.11       7.89           0.00       4.46      100            0    1000   


  probCrossover probMutation tournamentSize  

0           0.9          0.1             10  

1           0.9          0.1             10  

2           0.9          0.1             10 

然后,您可以輕松地計算平均值,如下所示:


df[['Accuracy','Error Rate','Not Classified','Total time']].mean()

返回:


Accuracy          95.263333

Error Rate         4.736667

Not Classified     0.000000

Total time         4.766333

dtype: float64


查看完整回答
反對 回復 2021-09-14
  • 2 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號