anopheles
anopheles

Reputation: 434

Integrating using scipy's trapz function

I'm currently trying to integrate a function which consists of XY point pairs. Feel free to take a look: https://dl.dropboxusercontent.com/u/2635172/example.csv

I'm using pandas to read the file

data_df = pd.read_csv("example.csv", sep="\t", index_col=0, names=["test"])

If you look closely, the spacing between consecutive x values are identical, therefore I can write the integral as following:

integral = integrate.trapz(data_df.values.transpose())*data_df.index[1]

where integrate is imported from scipy and data_df.index[1] refers to the spacing. The following value is returned: 189274.48501691

If I perform the integration the following way:

  integrate.trapz(data_df.values.transpose(), x=data_df.index)

A completely different value is returned (5.846689e+08). Any ideas why this is the case?

Note that the first result should be correct. This is also returned by MATLAB's trapz function.

Upvotes: 1

Views: 3390

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58895

You have to use the values from the Index instance:

integrate.trapz(data_df.values.transpose(), x=data_df.index.values)
#[189274.48501691408]

It also works If you transform your data_df.index from Index to ndarray:

integrate.trapz(data_df.values.transpose(), x=data_df.index.view(pd.np.ndarray))
#[189274.48501691408]

Upvotes: 1

Related Questions