While NumPy offers specialized functions for creating arrays (which we'll see shortly), a very common starting point is to convert existing Python data structures, particularly lists, into NumPy arrays. This is often the first step when you have data already loaded or generated using standard Python code.
The primary function for this conversion is np.array()
. Let's see how it works.
Imagine you have a simple Python list of numbers:
import numpy as np
# A standard Python list
python_list = [5, 10, 15, 20, 25]
print(f"Original Python list: {python_list}")
print(f"Type: {type(python_list)}")
# Convert the list to a NumPy array
numpy_array = np.array(python_list)
print(f"Converted NumPy array: {numpy_array}")
print(f"Type: {type(numpy_array)}")
print(f"Array's data type (dtype): {numpy_array.dtype}")
When you run this code, you'll notice a few things:
np.array()
function takes the Python list as input.numpy_array
, is no longer a standard Python list but an object of type numpy.ndarray
. This is the N-dimensional array we discussed.dtype
of int64
(the exact integer type, like int32
or int64
, might vary slightly depending on your system).This automatic type detection is convenient. If your list contains floating-point numbers, NumPy will create a float array:
float_list = [1.0, 2.5, 3.7, 4.2]
float_array = np.array(float_list)
print(f"Float array: {float_array}")
print(f"Float array dtype: {float_array.dtype}") # Likely float64
Transformation of a 1D Python list into a NumPy ndarray using
np.array()
.
NumPy arrays can have more than one dimension. You can create multi-dimensional arrays by passing nested Python lists (lists containing other lists) to np.array()
.
# A nested Python list (list of lists)
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(f"Original nested list:\n{nested_list}")
# Convert to a 2D NumPy array
numpy_2d_array = np.array(nested_list)
print(f"Converted 2D NumPy array:\n{numpy_2d_array}")
print(f"Array shape: {numpy_2d_array.shape}") # Output: (3, 3) -> 3 rows, 3 columns
print(f"Array dimensions: {numpy_2d_array.ndim}") # Output: 2
print(f"Array dtype: {numpy_2d_array.dtype}") # Output: int64 (usually)
Here, the list of three lists, each containing three integers, is transformed into a 2-dimensional array (a matrix) with 3 rows and 3 columns. NumPy arranges the data accordingly.
Transformation of a nested Python list into a 2D NumPy ndarray.
For NumPy to create a standard multi-dimensional array efficiently, the inner lists must have the same length. If they don't, NumPy might create an array of dtype=object
, where each element is a Python object (like a list), which generally loses the performance benefits of NumPy arrays.
# Example of an uneven list (use with caution)
uneven_list = [[1, 2], [3, 4, 5]]
# NumPy may create an object array
# Note: Behavior might change slightly across NumPy versions.
# Explicitly setting dtype=object ensures this behavior if needed.
object_array = np.array(uneven_list, dtype=object)
print(f"Array from uneven list:\n{object_array}")
print(f"Array dtype: {object_array.dtype}") # Output: object
dtype
)While NumPy's automatic type inference is useful, sometimes you need explicit control over the array's data type. You can specify the desired type using the dtype
argument in the np.array()
function. This is helpful for:
float32
instead of float64
, or int8
instead of int64
) use less memory.Here's how you can create an array of floating-point numbers from a list of integers:
integer_list = [1, 2, 3, 4]
# Create a float array from integers
float_array = np.array(integer_list, dtype=float) # or np.float64, np.float32 etc.
print(f"Original integer list: {integer_list}")
print(f"Converted float array: {float_array}")
print(f"Array dtype: {float_array.dtype}") # Output: float64 (or specified float type)
# Create an integer array with a smaller integer type
int8_array = np.array(integer_list, dtype=np.int8)
print(f"Converted int8 array: {int8_array}")
print(f"Array dtype: {int8_array.dtype}") # Output: int8
Common NumPy data types include np.int8
, np.int16
, np.int32
, np.int64
, np.uint8
(unsigned integer), np.float32
, np.float64
, np.complex64
, np.complex128
, np.bool_
, and np.object_
. Choosing the right dtype
is an important consideration, especially when working with large datasets.
Converting Python lists is a fundamental way to start working with NumPy arrays, providing a bridge from standard Python data structures to NumPy's powerful numerical computing capabilities. In the next section, we will look at functions NumPy provides to create arrays directly, without needing a pre-existing Python list.
© 2025 ApX Machine Learning