第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

計(jì)算文本文件的標(biāo)準(zhǔn)偏差

計(jì)算文本文件的標(biāo)準(zhǔn)偏差

慕妹3146593 2021-10-10 19:00:52
我正在嘗試計(jì)算“ClosePrices”列中所有數(shù)據(jù)的標(biāo)準(zhǔn)偏差,請(qǐng)參閱 pastebin https://pastebin.com/JtGr672m我們需要計(jì)算所有 1029 個(gè)浮點(diǎn)數(shù)的一個(gè)標(biāo)準(zhǔn)差。這是我的代碼:ins1 = open("bijlage.txt", "r")for line in ins1:        numbers = [(n) for n in number_strings]         i = i + 1        ClosePriceSD = []        ClosePrice = float(data[0][5].replace(',', '.'))        ClosePriceSD.append(ClosePrice)def sd_calc(data):    n = 1029    if n <= 1:        return 0.0    mean, sd = avg_calc(data), 0.0    # calculate stan. dev.    for el in data:        sd += (float(el) - mean)**2    sd = math.sqrt(sd / float(n-1))    return sddef avg_calc(ls):    n, mean = len(ls), 0.0    if n <= 1:        return ls[0]    # calculate average    for el in ls:        mean = mean + float(el)    mean = mean / float(n)    return meanprint("Standard Deviation:")print(sd_calc(ClosePriceSD))print()所以我要計(jì)算的是“收盤價(jià)”部分下所有浮動(dòng)的標(biāo)準(zhǔn)偏差。好吧,我有這個(gè)“ClosePrice = float(data[0][5].replace(',', '.'))”這應(yīng)該計(jì)算ClosePrice下所有浮點(diǎn)數(shù)的標(biāo)準(zhǔn)偏差,但它只從數(shù)據(jù)中計(jì)算出來(lái)[0][5]。但我希望它計(jì)算 ClosePrice 下所有 1029 個(gè)浮點(diǎn)數(shù)的一個(gè)標(biāo)準(zhǔn)差
查看完整描述

2 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

我認(rèn)為您的錯(cuò)誤在開頭的 for 循環(huán)中。你有,for line in ins1但你永遠(yuǎn)不會(huì)line在循環(huán)內(nèi)使用。在您的循環(huán)中,您還使用了之前未定義的number_string和data。


以下是從 txt 文件中提取數(shù)據(jù)的方法。


with open("bijlage.txt", "r") as ff:

    ll = ff.readlines() #extract a list, each element is a line of the file


data = []

for line in ll[1:]: #excluding the first line wich is an header

    d = line.split(';')[5] #split each line in a list using semicolon as a separator and keep the element with index 5

    data.append(float(d.replace(',', '.'))) #substituting the comma with the dot in the string and convert it to a float


print data #data is a list with all the numbers you want

您應(yīng)該能夠從這里計(jì)算均值和標(biāo)準(zhǔn)差。


查看完整回答
反對(duì) 回復(fù) 2021-10-10
  • 2 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)