Home »
Python »
Python Programs
Python NumPy - Return n Copies of Array Concatenated Along First Axis
By IncludeHelp Last updated : September 11, 2024
Problem statement
Given a NumPy array, write a Python program to return n copies of array concatenated along first axis.
Returning n copies of array concatenated along first axis
For this purpose, we will define a function which will take a 1-D numpy array arr and an integer n as input. Here, we will use numpy.tile() to tile the input array n times along the first axis and creates a new tiled array. Finally, we return the new array.
Python code to return n copies of array concatenated along first axis
Let us understand with the help of an example:
# Importing numpy
import numpy as np
# Creating a string array
arr = np.array([1, 2,3, 4])
# Defining values for n
n = 2
# Display original array
print("Original array", arr,"\n")
# Defining a function
def fun(arr,n):
tiled_arr = np.tile(arr, (n, 1))
return tiled_arr
# Display result
print("Result:\n",fun(arr,n),"\n")
Output
The output of the above program is:
Original array [1 2 3 4]
Result:
[[1 2 3 4]
[1 2 3 4]]
Python NumPy Programs »