计算行的每X数的平均 [英] Calculating an average for every X number of lines

查看:119
本文介绍了计算行的每X数的平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个文本文件中的数据并将计算平均每600行的文件。我加载从文件中的文本,把它变成一个numpy的数组,并列举了。我能得到平均的第600行,但我不知道如何让蟒蛇计算,每600线平均,然后把它变成新的文本文件写一个循环。这里是我的code迄今:

 导入numpy的是NP#loads文件,并将其地方阵列
数据= np.loadtxt('244UTZ10htz.txt',分隔符='\\ T',skiprows = 2)
形状= np.shape(数据)#creates数组ü风力风速值
对于我,在历数(数据)D:
    数据[I] =(D [3])
    如果我== 600:
        minavg = np.mean(数据[我== 600])#finds总计u意味着天
UBAR = np.mean(数据)


解决方案

根据我从你的问题理解,这听起来像你有你想利用每一行的平均值达到第600 1的一些文件,重复多次,直到没有更多的数据。因此,在600线平均你行0 - 600,在线1200你平均线600至1200

模司将一个方法来取平均值,当你打的每第600行,而不必使用一个独立的变量来算你多少行通过循环。此外,我用 numpy的数组分片创建的视图原始数据,只包含了数据组的第4列

这个例子应该做你想做的,但它是完全未经测试...我也并不十分熟悉numpy的,所以有一些更好的方法做这在其他的答案中提到:

 导入numpy的是NP#loads文件,并将其地方阵列
数据= np.loadtxt('244UTZ10htz.txt',分隔符='\\ T',skiprows = 2)
形状= np.shape(数据)
data_you_want =数据[:3]
daily_averages =名单()
#creates数组ü风力风速值
对于I,D在历数(data_you_want):
    如果(ⅰ%600)== 0:
        avg_for_day = np.mean(data_you_want [I - 600:我])
        daily_averages.append(avg_for_day)

您可以修改上面的例子写出来平均到一个新的文件,而不是追加到一个列表,我都做了,或只写daily_averages列表输出到任何你想要的文件。

作为奖励,这里是只使用CSV库一个Python的解决方案。它没有被足够多的测试,但理论上应该行不通,可能是很容易理解新的人到Python。

 导入CSV数据=名单()
daily_average =名单()
NUM_LINES = 600开放('testme.csv','R')为csvfile:
    读卡器= csv.reader(csvfile,分隔符=\\ t的)    对于我,排在历数(读者):
        如果(I%NUM_LINES)== 0,I = 0!
            平均= SUM(数据[我 - NUM_LINES:我])/ NUM_LINES
            daily_average.append(平均)        data.append(中间体(行[3]))

希望这有助于!

I am trying to take data from a text file and calculate an average for every 600 lines of that file. I'm loading the text from the file, putting it into a numpy array and enumerating it. I can get the average for the first 600 lines but I'm not sure how to write a loop so that python calculates an average for every 600 lines and then puts this into a new text file. Here is my code so far:

import numpy as np

#loads file and places it in array
data = np.loadtxt('244UTZ10htz.txt', delimiter = '\t', skiprows = 2)
shape = np.shape(data)

#creates array for u wind values
for i,d in enumerate(data):
    data[i] = (d[3])
    if i == 600:
        minavg = np.mean(data[i == 600])

#finds total u mean for day
ubar = np.mean(data)

解决方案

Based on what I understand from your question, it sounds like you have some file that you want to take the mean of every line up to the 600th one, and repeat that multiple times till there is no more data. So at line 600 you average lines 0 - 600, at line 1200 you average lines 600 to 1200.

Modulus division would be one approach to taking the average when you hit every 600th line, without having to use a separate variable to keep count how many lines you've looped through. Additionally, I used Numpy Array Slicing to create a view of the original data, containing only the 4th column out of the data set.

This example should do what you want, but it is entirely untested... I'm also not terribly familiar with numpy, so there are some better ways do this as mentioned in the other answers:

import numpy as np

#loads file and places it in array
data = np.loadtxt('244UTZ10htz.txt', delimiter = '\t', skiprows = 2)
shape = np.shape(data)
data_you_want = data[:,3]
daily_averages = list()


#creates array for u wind values
for i,d in enumerate(data_you_want):
    if (i % 600) == 0:
        avg_for_day = np.mean(data_you_want[i - 600:i])
        daily_averages.append(avg_for_day)

You can either modify the example above to write the mean out to a new file, instead of appending to a list as I have done, or just write the daily_averages list out to whatever file you want.

As a bonus, here is a Python solution using only the CSV library. It hasn't been tested much, but theoretically should work and might be fairly easy to understand for someone new to Python.

import csv 

data = list()
daily_average = list()
num_lines = 600

with open('testme.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter="\t")

    for i,row in enumerate(reader):
        if (i % num_lines) == 0 and i != 0:
            average = sum(data[i - num_lines:i]) / num_lines
            daily_average.append(average)

        data.append(int(row[3]))

Hope this helps!

这篇关于计算行的每X数的平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆