Home »
Python »
Python Programs
Python - numpy.vander() Method with Examples
Learn about the numpy.vander() method, how does it work?
By Pranit Sharma Last updated : December 25, 2023
Python - numpy.vander() Method
The numpy.vander() method is used to generate a Vandermonde matrix. A Vandermonde matrix is a type of matrix that arises in the polynomial least squares fitting, Lagrange interpolating polynomials, and the reconstruction of a statistical distribution from the distribution's moments. A Vandermonde matrix of order is of the following form:
The columns of the output matrix are powers of the input vector. The order of the powers is determined by the increasing boolean argument. Specifically, when increasing is False, the i-th output column is the input vector raised element-wise to the power of N - i - 1.
Syntax of numpy.vander() Method
Below is the syntax of numpy.vander() method:
numpy.vander(x, N=None, increasing=False)
Parameters of numpy.vander() Method
Below are the parameters of numpy.vander() method:
- x: input array (1D)
- n: int values which represent the number of columns in the output.
- increasing: Order of the powers of the columns.
Return Value of numpy.vander() Method
The method returns vandermonde matrix. If increasing is False, the first column is x^(N-1), the second x^(N-2) and so forth. If increasing is True, the columns are x^0, x^1, ..., x^(N-1). [Source]
Let us understand with the help of an example,
The numpy.vander() Method Examples
Practice the below example to understand the use of numpy.vander() method:
Example 1: Python program to demonstrate the example of numpy.vander() Method
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([1, 2, 3, 5])
# Display original array
print("Original Array:\n",arr,"\n")
# Creating a vandermonde matrix
res = np.vander(arr,3)
# Display result
print("Result:\n",res)
Output
Example 2
# Importing numpy module
import numpy as np
# Creating an array
arr = np.array([9, 29, 19])
# Printing the array
print("The arr is:\n", arr)
# Performing np.vander()
result = np.vander(arr)
# Printing the result
print("Vandermonde Matrix (result):\n", result)
Output
The arr is:
[ 9 29 19]
Vandermonde Matrix (result):
[[ 81 9 1]
[841 29 1]
[361 19 1]]
Web References
Python NumPy Programs »