Thursday, April 8, 2021

Python Pool: How to Use numpy trapz() Function in Python

In the Numpy module, we have discussed many functions used to operate on the multidimensional array. In this tutorial, we will discuss the concept of the numpy trapz() function used to integrate along the given axis using the composite trapezoidal rule.

What is numpy trapz() function?

The numpy trapz() function integrates along the given axis using the composite trapezoidal rule.

Syntax numpy trapz()

numpy.trapz(y, x = None, dx = 1.0, axis = -1)

Parameters

  • y: It is an input array that is to be integrated.
  • x: it is also array-like, which is optional as an input. The sample points corresponding to the y values. If x is set to None, the sample points are assumed to be evenly spaced dx apart. By default, it is None.
  • dx: It is the scalar value which is also an optional input. It is the spacing between sample points when x is None. by default. It is 1.
  • axis: It is an integer value and an optional input. It is the axis along which we have to integrate.

Return value

The return value is the float value and the definite integral as approximated by the trapezoidal rule.

Examples of numpy trapz() function

Here, we will be discussing how we can write the trapz() function from the numpy library.

1. Taking only y as a parameter

In this example, we will take y as an array-like input parameter and see the output. Let us look at the example for understanding the concept in detail.

#taking y as a parameter
#import numpy library
import numpy as np

y = [3,4,5,6]
output = np.trapz(y)
print("Output : ",output)

Output:

Output :  13.5

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take an array as y.
  • Then, we will apply the trapz() function with y as a parameter and store the output in the output variable.
  • At last, we will print the output.
  • Hence, you can see the output is in float value.

2. Taking x and y as parameter

In this example, we will be taking x and y both as an array-like input and perform the trapz() function. Hence, we will see the output. Let us look at the example for understanding the concept in detail.

#taking x and y as a parameter
#import numpy library
import numpy as np

y = [3,4,5,6]
x = [1,2,3,4]
output = np.trapz(y,x)
print("Output : ",output)

Output:

Output :  13.5

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take an array as y.
  • After that, we will take an array as x.
  • Then, we will apply the trapz() function with y and x as a parameter and store the output in the output variable.
  • At last, we will print the output.
  • Hence, you can see the output is in float value.

3. Taking y and dx as a parameter

In this example, we will be taking x as an array-like input and dx = 2 and perform the trapz() function. Hence, we will see the output. Let us look at the example for understanding the concept in detail.

#taking dx and y as a parameter
#import numpy library
import numpy as np

y = [3,4,5,6]

output = np.trapz(y,dx = 2)
print("Output : ",output)

Output:

Output :  27.0

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take an array as y.
  • Then, we will apply the trapz() function with y and dx = 2 as a parameter and store the output in the output variable.
  • At last, we will print the output.
  • Hence, you can see the output is in float value.

4. Taking multidimensional array and axis as a parameter

In this example, we will be taking input as a multidimensional array with the help of arange() function in the numpy library. Then, we will set the axis as 0 and 1 and check the output on both the axis. Hence, you can see the output. Let us look at the example for understanding the concept in detail.

#taking dx and y as a parameter
#import numpy library
import numpy as np

a = np.arange(6).reshape(2, 3)
print(a)
print("\n")

output = np.trapz(a,axis = 0)
out = np.trapz(a,axis = 1)
print("Output with axis = 0 : ",output)
print("\n")
print("Output with axis = 1 : ",out)

Output:

[[0 1 2]
 [3 4 5]]


Output with axis = 0 :  [1.5 2.5 3.5]


Output with axis = 1 :  [2. 8.]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will apply arange() function from the numpy library to take the input in a multidimensional array.
  • Then, we will print the array that we have taken as input.
  • We will then apply the trapz() function with a and axis = 0 and axis =1 as the parameter and stored the output in the output and out variable.
  • Finally, we have printed both the output by applying ” \n” for leaving a gap of the single line between both the output.
  • Hence, you can see the output.

Faster alternative for Numpy trapz() function

The faster alternative for numpy trapz() function simps() function from scipy. simps() function is used to get the integration of y(x) using samples along the axis and composite simpson’s rule by using scipy.integrate.simps() method.

Let us look at the example for understanding the concept in detail.

import numpy as np
from scipy.integrate import simps

arr = np.array([5, 20, 4, 18, 20, 18, 3, 4])
output = simps(arr, dx=5)
print("output =", output)

Output:

output = 443.333333333

Conclusion

In this tutorial, we have discussed the concept of the numpy trapz() function. We have discussed what the numpy trapz() function is and when we use it. We have discussed how we can use the numpy trapz() function from the numpy library, with examples explained in detail. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

The post How to Use numpy trapz() Function in Python appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...