Creating Arrays in NumPy
NumPy provides powerful tools for creating arrays of various dimensions. You can create arrays from lists, using specific functions, or even initialize arrays with default values.
1D Arrays
A 1D array is a simple linear collection of elements. It represents a single row or a single column of data.
Using array()
import numpy as np
# Create a 1D array from a list
array = np.array([1, 2, 3, 4, 5])
print("1D Array:", array)
Output:
1D Array: [1 2 3 4 5]
Using arange()
# Create a range of numbers
array = np.arange(5, 15, 2) # Start from 5, step by 2, up to 15
print("1D Array:", array)
Output:
1D Array: [ 5 7 9 11 13]
Using linspace()
# Create evenly spaced numbers
array = np.linspace(0, 1, 5) # 5 numbers between 0 and 1
print("1D Array:", array)
Output:
1D Array: [0. 0.25 0.5 0.75 1. ]
2D Arrays
A 2D array is a table of rows and columns, commonly used for matrix representations.
Using array()
# Create a 2D array from a nested list
array = np.array([[1, 2, 3], [4, 5, 6]])
print("2D Array:\n", array)
Output:
2D Array:
[[1 2 3]
[4 5 6]]
Using zeros()
# Create a 2D array filled with zeros
array = np.zeros((2, 3)) # 2 rows, 3 columns
print("2D Array:\n", array)
Output:
2D Array:
[[0. 0. 0.]
[0. 0. 0.]]
Using ones()
# Create a 2D array filled with ones
array = np.ones((3, 2)) # 3 rows, 2 columns
print("2D Array:\n", array)
Output:
2D Array:
[[1. 1.]
[1. 1.]
[1. 1.]]
Using reshape()
# Reshape a 1D array into 2D
array = np.arange(6).reshape(2, 3) # 2 rows, 3 columns
print("2D Array:\n", array)
Output:
2D Array:
[[0 1 2]
[3 4 5]]
nD Arrays
A 3D array or n-dimensional array represents data with multiple dimensions, useful for complex applications like image processing or scientific computations.
Using zeros()
# Create a 3D array filled with zeros
array = np.zeros((2, 3, 4)) # 2 blocks, 3 rows, 4 columns
print("3D Array:\n", array)
Output:
3D Array:
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
Using ones()
# Create a 3D array filled with ones
array = np.ones((3, 2, 2)) # 3 blocks, 2 rows, 2 columns
print("3D Array:\n", array)
Output:
3D Array:
[[[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]]]
Using full()
# Create a 3D array filled with a specified value
array = np.full((2, 2, 2), 7) # Fill with 7
print("3D Array:\n", array)
Output:
3D Array:
[[[7 7]
[7 7]]
[[7 7]
[7 7]]]
Comparison of Array Creation Functions
Function | Purpose | Example | Output |
---|---|---|---|
array() | Create arrays from lists or tuples | np.array([1, 2, 3]) | [1 2 3] |
arange() | Create range of values | np.arange(1, 10, 2) | [1 3 5 7 9] |
linspace() | Create evenly spaced values | np.linspace(0, 1, 5) | [0. 0.25 0.5 0.75 1.] |
zeros() | Create arrays filled with zeros | np.zeros((2, 3)) | [[0. 0. 0.] [0. 0. 0.]] |
ones() | Create arrays filled with ones | np.ones((3, 2)) | [[1. 1.] [1. 1.] [1. 1.]] |
full() | Create arrays filled with a specified value | np.full((2, 2), 5) | [[5 5] [5 5]] |
Try It Yourself
Problem 1: Create and Reshape
Create a 1D array of numbers from 1 to 12 and reshape it into a 2D array with 3 rows and 4 columns.
Show Code
import numpy as np
# Create and reshape
array = np.arange(1, 13).reshape(3, 4)
print("2D Array:\n", array)
Output:
2D Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Problem 2: Create a Custom Array
Create a 3D array filled with the value 9, having dimensions 2x2x2.
Show Code
import numpy as np
# Create a custom 3D array
array = np.full((2, 2, 2), 9)
print("3D Array:\n", array)
Output:
3D Array:
[[[9 9]
[9 9]]
[[9 9]
[9 9]]]