Introduction to Python NumPy Creating Arrays
Python NumPy Creating Arrays
You can create arrays using various methods.
Here are some commonly used methods to create arrays in NumPy:
Creating an Array from a Python List
You can create a NumPy array by passing a Python list (or a nested list) to the numpy.array()
function. The resulting array will have the same shape as the input list.
import numpy as np
# Create a 1-dimensional array
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
# Create a 2-dimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)
Creating an Array with Zeros or Ones
NumPy provides functions to create arrays filled with zeros or ones. The numpy.zeros()
function creates an array of the specified shape filled with zeros, while the numpy.ones()
function creates an array filled with ones.
import numpy as np
# Create a 1-dimensional array filled with zeros
zeros = np.zeros(5)
print(zeros)
# Create a 2-dimensional array filled with ones
ones = np.ones((3, 2))
print(ones)
Creating an Array with a Range of Values
NumPy's numpy.arange()
function creates an array with values within a specified range. It takes the start, stop, and step size as arguments.
import numpy as np
# Create a 1-dimensional array with values from 0 to 9
arr = np.arange(10)
print(arr)
# Create a 1-dimensional array with values from 2 to 10 (exclusive) with step 2
arr = np.arange(2, 10, 2)
print(arr)
Creating an Array with Equally Spaced Values
The numpy.linspace()
function generates an array with a specified number of equally spaced values between a start and end value (inclusive).
import numpy as np
# Create a 1-dimensional array with 5 equally spaced values between 0 and 1
arr = np.linspace(0, 1, 5)
print(arr)
Creating an Array with Random Values
NumPy's numpy.random
module provides functions to create arrays with random values. For example, you can use the numpy.random.rand()
function to generate an array of random values from a uniform distribution between 0 and 1.
import numpy as np
# Create a 1-dimensional array with 5 random values between 0 and 1
arr = np.random.rand(5)
print(arr)
Dimensions in Arrays
The dimensions of an array refer to the number of axes or dimensions it has. It determines how the elements of the array are organized and accessed. The number of dimensions is often referred to as the array's "rank" or "ndim".
Here are the common terminologies used to describe different dimensions in NumPy arrays:
1-Dimensional Arrays
A 1-dimensional array, also known as a vector, has a single axis. It represents a sequence of elements arranged in a single row or column.
import numpy as np
# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
Output:
[1 2 3 4 5]
Shape: (5,)
Dimensions: 1
2-Dimensional Arrays
A 2-dimensional array, also known as a matrix, has two axes or dimensions. It represents a table of elements arranged in rows and columns.
import numpy as np
# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
Output:
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
Dimensions: 2
3-Dimensional Arrays
A 3-dimensional array has three axes or dimensions. It represents a collection of 2-dimensional arrays arranged along the third axis. It can be visualized as a cube of elements.
import numpy as np
# Create a 3-dimensional array
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr)
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
Output:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
Shape: (2, 2, 3)
Dimensions: 3