How to upsample a matrix by repeating elements using NumPy in Python? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisites: Numpy 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. Syntax: numpy.repeat(array, repeats, axis=0) Parameters: array=Name of the arrayrepeats= Numbers of repetitions of every elementaxis= 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. Approach Import moduleCreate arrayPass it to repeat methodPrint matrix Example: Python3 # importing required module import numpy as np # declaring an array a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # use the repeat function to upsample the array print(np.repeat(a, 5, axis=1).repeat(3, axis=0)) Output: Method 2: 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. Syntax: np.kron(a ,b) 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 Example: Python3 # import required libraries import numpy as np # creating an array using numpy a = np.array([[9, 8, 5], [11, 12, 14], [20, 21, 22]]) # using kron function upsampling the array upsampled_array = np.kron(a, np.ones((2, 2))) # printing the desired result print(upsampled_array) Output : Comment More info T tushardhiman1999 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-numpy Python numpy-Matrix Function +1 More Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like