How to upsample a matrix by repeating elements using NumPy in Python?
Upsampling a matrix simply means expanding it and obviously upsampling can be done by adding more elements to the original matrix. It can be done in various ways like adding new elements and expanding the original matrix or it can be done by the matrix elements of original matrix itself. The later approach is discussed below along with 2 methods to do the same.
Method 1: using repeat()
We use the numpy.repeat() method to upsample the matrix by repeating the numbers of the matrix. We pass the matrix in repeat() method with the axis to upsample the matrix. This method is used to repeat elements of array.
numpy.repeat(array, repeats, axis=0)
- array=Name of the array
- repeats= Numbers of repetitions of every element
- axis= The axis along which to repeat the values. By default, axis is set to None.
- For row-wise axis=0 and for column-wise axis=1.
- Import module
- Create array
- Pass it to repeat method
- Print matrix
In this approach we will see how np.kron is used to upsample a matrix. We pass the matrix along with an ones matrix which will multiply with each other using kron() method and the result will be an upsampled matrix.
where a and b are two arrays.
- It returns the Kronecker product of two arrays.
- Its parameters are two arrays whose product to be calculated
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course