| by Arround The Web | No comments

Numpy Create 2D array

A two-dimensional Numpy array is defined from a list of python lists. Just like a single dimension Numpy array, all elements in an array must be of a single type. If a NumPy array is declared with lists of multiple types, type coercion will occur and the values will all be converted to a single type. Type coercion is the one in which the data types are converted from one to the other, it is implicit or automatic. In two-dimensional arrays, dimensions can be more than two.

In simple words, we can define the two-dimensional array as an array within another array. Its index starts with “0” and ends at the size of array “-1”. Arrays can be constructed n times within arrays. A two-dimensional array can change size both vertically and horizontally, in both directions.

Syntax

The syntax to declare an array is as below:

array_name = [r_arr][c_arr]

array_name is the name of the array that we want to create. Whereas, “r_arr” are the rows of the array and “c_arr” is the column of the array. This syntax allows us to create the memory location where the array will be stored, or we can say that the memory location can be reserved for the array.

There is another method to declare a 2D array:

array_name = [[R1C1, R1C2, R1C3, ...], [R2C2, R2C2, R2C3, ...], . . ..]

In the syntax above, array name is the name of the array where “R1C1”, “R2C1”, … n are the elements of the array where “R” denotes rows and “c” denotes columns. As we can see in the first square braces, the number of rows is changing while the columns are the same. This is because, within the array, we define columns using multiple arrays while the rows are defined inside the inner arrays.

Example # 01: Creating a Two-dimensional Array

Let us give a practical example of creating a two-dimensional array and get a better idea of how a two-dimensional array is created. To create a 2D array, we will first import our NumPy library which will enable us to implement some packages that NumPy provides us with for creating the array. Next, we will initialize a variable that holds the two-dimensional array to create an array. We will pass the np.array() function that allows us two create any type of array whether it is 1D, 2D, or so on. To that function, we will pass multiple arrays within this array which lets us create a 2-dimensional array.

As we can see in the screenshot below, in the second line, we passed three arrays to that function which means we have three rows and within those arrays, we passed 6 elements to each which means there are 6 columns. One thing to notice, is we always pass elements in square brackets which means we are passing array elements and we can see that we have passed multiple arrays within the single array.

import numpy as np

array = np.array([[1, 2, 3,4, 5, 6], [4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11, 12]])

print(array)

In the end, we printed the array using a print statement. As shown in the screenshot below, we can see the array is displayed which contains 3 rows and 6 columns.

Example # 02: Accessing the Values

As we studied the method to create 2D arrays, one thing must have clicked in our mind: how can we access the elements of the 2D array? While accessing the element of the 2D array is not a big issue. Numpy enables us to manipulate the elements of arrays by a simple line of code that is:

Array[row index][column index]

The array is the name of the array from which we have to access or fetch the data where the row index is the memory location of the row. And the column index is the location of the column to be accessed, suppose we have to access the index “2” element of row and index “0” element of a column.

As we can see in the figure below, we first imported the NumPy library to access the packages of NumPy. Then, we declared the variable name “array” which holds the 2D array and then passed it the values which we want to store in it. We first displayed the array as it is which we have initialized. Then, we passed the array with the index to our print() statement which will display the whole array that is stored at index “2”. In the next line of code, we again passed the array with two indexes to the print() statement. The first one is the row of the array and the second one is the column of the array which is “0” and “2”.

import numpy as np

array = np.array([[1, 2, 3,4, 5, 6], [4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11, 12]])

print("Array display:",array)

print("Display the second row:",array[2])

print("Display the first row and 2 column element:",array[0][2])

The following output is returned while running the code compiler prints the array as it is. Then, the second row according to the code. Lastly, the compiler returns the element that is stored at index “0” for rows and index “2” for the column.

Example #03: Updating the Values

We have already discussed the methodology of how we can create or access the data or elements within the 2D array, but when we have to change the elements of the array, we can simply use the method that is provided by the NumPy packages that allow us to update the desired value within an array.

To update the value, we use:

array[row_index][column_index]= [values]

In the syntax above, the array is the name of the array. The row index is the place or location which we will edit. The column index is the location of the column at which the value is updated, where the value is the one that should be added to the desired index.

As we can see, we first import our NumPy library. And then declared an array of size 3×6 and passed its integer values. Then, we passed the value “21” to the array which means we want to store the value “21” in the array at “0” of a row and “2” of a column which means we want to store it at the index of the first row and the 3rd column of the array. Then print both arrays, the original one and also the element that we have stored in the array.

import numpy as np

array = np.array([[1, 2, 3,4, 5, 6], [4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11, 12]])

array[0][2]=21

print("Array display:",array)

print("Display the first row and 2 column element:",array[0][2])

As displayed below, the value is successfully updated in the array by just adding a simple line of code that is provided by the NumPy package.

Conclusion

In this article, we explained different ways to create two-dimensional arrays and how we can manipulate them using NumPy’s built-in functions. We discussed how we can access the elements within the array and update them. Numpy enables us to create and manipulate multi-dimensional arrays by a single line of code. Numpy arrays are clearer and more effective than python lists.

Share Button

Source: linuxhint.com

Leave a Reply