{"id":8700,"date":"2021-06-12T10:56:15","date_gmt":"2021-06-12T05:26:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=8700"},"modified":"2021-11-22T18:40:39","modified_gmt":"2021-11-22T13:10:39","slug":"create-numpy-array-of-different-shapes-initialize-with-identical-values-using-numpy-full-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/create-numpy-array-of-different-shapes-initialize-with-identical-values-using-numpy-full-in-python\/","title":{"rendered":"Create Numpy Array of different shapes & initialize with identical values using numpy.full() in Python"},"content":{"rendered":"
In this article we will see how we can create a numpy array of different shapes but initialized with identical values. So, let’s start the explore the concept to understand it well.<\/p>\n
Numpy module provides a function Where,<\/p>\n But to use Numpy we have to include following module i.e.<\/p>\n Here array length is 8 and array elements to be initialized with 2.<\/p>\n Let’s see the below the program.<\/p>\n Here 2D array of row 3 and column 4 and array elements to be initialized with 5.<\/p>\n Let’s see the below the program.<\/p>\n Here initialized value is 1.<\/p>\n Let’s see the below the program.<\/p>\nnumpy.full()<\/code> to create a numpy array of given shape and initialized with a given value.<\/p>\n
Syntax : numpy.<\/span>full<\/span>(<\/span>shape, given_value, dtype=<\/span>None<\/span>, order=<\/span>'C'<\/span>)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n
\n
import<\/span> numpy <\/span>as<\/span> np<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n
\n
\n
<\/a>Example-1 : Create a 1D Numpy Array of length 8 and all elements initialized with value 2<\/h3>\n
import numpy as np\r\n# 1D Numpy Array created of length 8 & all elements initialized with value 2\r\nsample_arr = np.full(8,2)\r\nprint(sample_arr)<\/pre>\n
Output :\r\n[2 2 2 2 2 2 2 2]<\/pre>\n
<\/a>Example-2 : Create a 2D Numpy Array of 3 rows | 4 columns and all elements initialized with value 5<\/h3>\n
import numpy as np\r\n#Create a 2D Numpy Array of 3 rows & 4 columns. All intialized with value 5\r\nsample_arr = np.full((3,4), 5)\r\nprint(sample_arr)<\/pre>\n
Output :\r\n[[5 5 5 5]\r\n[5 5 5 5]\r\n[5 5 5 5]]<\/pre>\n
<\/a>Example-3 : Create a 3D Numpy Array of shape (3,3,4) & all elements initialized with value 1<\/h3>\n
import numpy as np\r\n# Create a 3D Numpy array & all elements initialized with value 1\r\nsample_arr = np.full((3,3,4), 1)\r\nprint(sample_arr)<\/pre>\n
Output :\r\n\r\n[[[1 1 1 1]\r\n[1 1 1 1]\r\n[1 1 1 1]]\r\n\r\n[[1 1 1 1]\r\n[1 1 1 1]\r\n[1 1 1 1]]\r\n\r\n[[1 1 1 1]\r\n[1 1 1 1]\r\n[1 1 1 1]]]<\/pre>\n<\/div>\n