Python集成,然后保存文本文件 [英] Python-Integrate then save text file

查看:59
本文介绍了Python集成,然后保存文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部:

这个问题与我正在做的一些MS研究有关.

This question pertains to some MS research I am doing.

我想做的事情似乎很简单.我有一个值(时间,... values)的文本文件.然后,我想一直都从0到那些值进行积分,然后将该值保存到文本文件中.

What I want to do is seemingly simple. I have a text file of values( time, .....values). I then want to integrate from 0 to those values for all times, then save that value to a text file.

from numpy import *
from pylab import *
import os, sys, shutil
import math

#######################

#Load Data
data = loadtxt('wh.txt')

#Open file to save plots to
shutil.rmtree("wh_files")
os.makedirs("wh_files")
os.chdir("wh_files")

for i in range(0,100,1):
   int = trapz(data[i,:],axis=0)
   print int
   savetxt('int.txt', int)

运行此命令时,出现以下错误:

When I run this I get the following error:


  File "integral.py", line 19, in 
    savetxt('int.txt', int)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/numpy/lib/npyio.py", line 960, in savetxt
    ncol = X.shape[1]
IndexError: tuple index out of range

几天以来,我一直在尝试解决此问题,但尚未找到解决方案.感谢您提供的任何帮助/意见.

I have been trying to solve this problem for a few days on an off now, but am yet to find a solution. I appreciate any help/comments you can give.

推荐答案

int的值是一个浮点数,但savetxt需要一个数组.您要为所有积分结果创建一个numpy数组,然后使用savetxt将其保存在最后.我认为类似这样的方法应该可以解决问题:

The value of int is a float but savetxt requires an array. You want to create a numpy array for all the integration results, then save it at the end using savetxt. I think something like this should do the trick:

int_array = apply_along_axis(trapz, 0, data)
savetxt('int.txt', int_array)

请记住,这(连同您的原始示例)将汇总时间字段,而不是跨时间进行积分.仅当沿x轴的间距为1时,才会产生有效的结果.否则,您将需要执行以下操作:

Keep in mind that this (along with your original example) will sum the time fields, rather than integrating across time. This will only yield valid results if the spacing along the x-axis is 1. Otherwise you'll want to do something like this:

t = data[0,:]
int_array = apply_along_axis(trapz, 0, data[1:,:], t)
savetxt('int.txt', int_array)

假设时间字段是数字.

编辑:下面是第二代码部分的进一步说明.

edit: Further explanation of 2nd code section follows.

您正在使用梯形法则来积分各种值,这是一种积分近似技术,其作用是将曲线上连续y值的平均值相加,再乘以两个y值之间x的变化.这相当于计算连接两个y值和x轴的梯形的面积,如下所示:

You're using the trapezoidal rule to integrate a variety of values, which is an integration approximation technique that works by summing the average of successive y-values on a curve multiplied by the change in x between the two y-values. This amounts to calculating the area of a trapezoid that connects the two y-values and the x-axis, like so:

您的问题尚不完全清楚,但似乎您正在跨时间对值进行积分,以便x轴表示时间.需要合并x值以获得每个梯形的正确面积(每个梯形的面积为(x 2 -x 1 )*(y 2 + y 1 )/2,最终积分结果是所有此类面积的总和.

It's not completely clear from your question, but it seemed that you were integrating the values across time, so that the x-axis would represent time. The x-values need to be incorporated to get the correct area of each trapezoid (the area of each trapezoid is (x2 - x1) * (y2 + y1) / 2 and the final integration result is the sum of all such areas).

合并这些x轴值的最简单方法是将其作为x参数传递到trapz函数中(请参见

The simplest way to incorporate these x-axis values is to pass it into the trapz function as the x parameter (see the docstring). In the example above, I used t = data[0,:] as the array of x values suchly.

另一个警告:如果x值之间的所有间距都相同(因此x 2 -x 1 是常数),则可以通过拉动来节省一些计算求和,然后在末尾简单地相乘.此功能可通过带有dx参数的trapz函数使用.因此,例如,如果您每30秒进行一次时间测量,则可以将第二个示例中的第二行替换为:

Another caveat: if all spacing between x values are the same (so that x2 - x1 is a constant), you can save yourself some calculation by pulling this out of the summation and simply multiplying it at the end. This functionality is available through the trapz function with the dx parameter. So, if you're time measurements were taken every 30 seconds, for example, you could replace the 2nd line in my 2nd example with:

int_array = apply_along_axis(trapz, 0, data[1:,:], None, 30)

希望有帮助.

这篇关于Python集成,然后保存文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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