Now that we understand why NumPy is important for numerical tasks in Python, let's look at how to create the fundamental NumPy data structure: the ndarray
. Think of ndarray
s as more powerful versions of Python lists, specifically designed for fast mathematical operations.
There are several common ways to create NumPy arrays.
The most straightforward way to create a NumPy array is by converting an existing Python list or tuple using the np.array()
function. NumPy intelligently infers the data type (like integer or floating-point) from the list elements, but you can also specify it explicitly.
Let's start with a simple Python list and convert it into a 1D NumPy array (which we often use to represent vectors):
import numpy as np
# A regular Python list
my_list = [1, 2, 3, 4, 5]
# Convert the list to a NumPy array
my_vector = np.array(my_list)
print(my_vector)
print(type(my_vector))
Executing this code will output:
[1 2 3 4 5]
<class 'numpy.ndarray'>
Notice the output format [1 2 3 4 5]
without commas. This is the standard way NumPy displays arrays. Also, the type is confirmed as numpy.ndarray
.
We can create 2D arrays (which we use for matrices) by passing a list of lists:
import numpy as np
# A list of lists
my_nested_list = [[1, 2, 3], [4, 5, 6]]
# Convert to a 2D NumPy array (matrix)
my_matrix = np.array(my_nested_list)
print(my_matrix)
This produces:
[[1 2 3]
[4 5 6]]
NumPy arranges the data into rows and columns, forming a 2x3 matrix in this case.
NumPy provides convenient functions to create arrays with initial placeholder content, like zeros or ones, or sequences of numbers. This is often more efficient than creating a Python list first.
np.zeros()
The np.zeros()
function creates an array filled entirely with zeros. You must specify the desired shape of the array as a tuple.
import numpy as np
# Create a 1D array (vector) of 4 zeros
zeros_vector = np.zeros(4)
print("Vector of zeros:")
print(zeros_vector)
# Create a 2D array (matrix) of zeros with 2 rows and 3 columns
zeros_matrix = np.zeros((2, 3))
print("\nMatrix of zeros:")
print(zeros_matrix)
Output:
Vector of zeros:
[0. 0. 0. 0.]
Matrix of zeros:
[[0. 0. 0.]
[0. 0. 0.]]
Note that the default data type is floating-point (0.
).
np.ones()
Similarly, np.ones()
creates an array filled with ones. It also requires the shape as an argument.
import numpy as np
# Create a 1D array (vector) of 3 ones
ones_vector = np.ones(3)
print("Vector of ones:")
print(ones_vector)
# Create a 3x2 matrix of ones
ones_matrix = np.ones((3, 2))
print("\nMatrix of ones:")
print(ones_matrix)
Output:
Vector of ones:
[1. 1. 1.]
Matrix of ones:
[[1. 1.]
[1. 1.]
[1. 1.]]
Again, the default type is float.
np.arange()
The np.arange()
function is similar to Python's built-in range()
function but returns a NumPy array instead of a list iterator. It generates values within a specified interval with a defined step.
np.arange(stop)
: Values from 0 up to (but not including) stop
.np.arange(start, stop)
: Values from start
up to (but not including) stop
.np.arange(start, stop, step)
: Values from start
up to (but not including) stop
, incrementing by step
.import numpy as np
# Array from 0 up to (not including) 5
arr1 = np.arange(5)
print("np.arange(5):")
print(arr1)
# Array from 2 up to (not including) 8
arr2 = np.arange(2, 8)
print("\nnp.arange(2, 8):")
print(arr2)
# Array from 1 up to (not including) 10, step 2
arr3 = np.arange(1, 10, 2)
print("\nnp.arange(1, 10, 2):")
print(arr3)
Output:
np.arange(5):
[0 1 2 3 4]
np.arange(2, 8):
[2 3 4 5 6 7]
np.arange(1, 10, 2):
[1 3 5 7 9]
np.arange
typically creates integer arrays if all arguments are integers, but can create float arrays if any argument is a float.
np.linspace()
Another useful function is np.linspace(start, stop, num)
, which creates an array of num
evenly spaced values between start
and stop
(inclusive).
import numpy as np
# Create an array with 5 evenly spaced values between 0 and 1 (inclusive)
linear_arr = np.linspace(0, 1, 5)
print("np.linspace(0, 1, 5):")
print(linear_arr)
# Create an array with 4 evenly spaced values between 10 and 40
another_linear = np.linspace(10, 40, 4)
print("\nnp.linspace(10, 40, 4):")
print(another_linear)
Output:
np.linspace(0, 1, 5):
[0. 0.25 0.5 0.75 1. ]
np.linspace(10, 40, 4):
[10. 20. 30. 40.]
This is particularly handy when you need a specific number of points over a range.
NumPy arrays have a specific data type (dtype
) associated with them, like int64
(64-bit integer) or float64
(64-bit floating-point). While NumPy often infers the type correctly, you can explicitly set it using the dtype
argument during creation. This can be important for controlling memory usage or ensuring numerical precision.
import numpy as np
# Create an array of zeros, explicitly setting the type to integer
int_zeros = np.zeros((2, 2), dtype=np.int32)
print("Integer zeros matrix:")
print(int_zeros)
print("Data type:", int_zeros.dtype)
# Create an array from a list, explicitly setting the type to float
float_array = np.array([1, 2, 3], dtype=np.float64)
print("\nFloat array:")
print(float_array)
print("Data type:", float_array.dtype)
Output:
Integer zeros matrix:
[[0 0]
[0 0]]
Data type: int32
Float array:
[1. 2. 3.]
Data type: float64
You now have several ways to create NumPy arrays: directly from Python lists or using specialized functions like np.zeros
, np.ones
, np.arange
, and np.linspace
. These methods provide the building blocks for representing the vectors and matrices we'll use extensively in linear algebra and machine learning. In the next sections, we'll explore how to access elements within these arrays and perform basic operations.
© 2025 ApX Machine Learning