To create a two-dimensional array using NumPy, you can use the numpy.array
function with nested lists. Each nested list represents a row of the array. Here is an example code snippet that demonstrates how to create a two-dimensional array with NumPy:
import numpy as np
# Create a two-dimensional array with 2 rows and 3 columns
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
In this example, we imported the NumPy library using the import
statement, and then we used the numpy.array
function to create a two-dimensional array with 2 rows and 3 columns. We passed in a list of two nested lists, each containing three integers. We assigned the resulting array to the variable arr
. Finally, we printed the contents of the array using the print
statement.
You can create a two-dimensional array using NumPy in many other ways, such as using the numpy.zeros
function or the numpy.random.rand
function, depending on your needs.
Comments
Post a Comment