Convert Python List to numpy Arrays
NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we’ll explore these two methods with examples for converting a list into a numpy array.
Let’s start with a basic example to show how we can convert a Python list to a NumPy array.
import numpy as np
a = [1, 2, 3, 4, 5]
# Convert the list to a NumPy array
arr = np.array(a)
print(arr)
Output
[1 2 3 4 5]
Let’s now discuss the different ways of converting a list to an array in more detail.
Using numpy.array()
The numpy.array() function is the most common way to convert a list into an array. It takes the Python list as an argument and returns a NumPy array containing the same elements.
import numpy as np
a = [1.2, 2.6, 3.3, 4.2]
# Convert the list to a NumPy array
arr = np.array(a)
print(arr)
Output
[1.2 2.6 3.3 4.2]
Explanation: We used np.array(a) to convert the list into a NumPy array and storing it in arr.
Using numpy.asarray()
The numpy.asarray() function is another way to convert a list into an array.
import numpy as np
a = [1, 2, 3, 4]
# Convert the list to a NumPy array using asarray
arr = np.asarray(a)
print(arr)
Output
[1 2 3 4]
Explanation: We used np.asarray(a) to convert the list into a NumPy array and storing it in arr.
Note:
- In both cases (numpy.array() and numpy.asarray()), a new NumPy array is created from Python list.
- The Python list and resulting NumPy array do not share memory because Python lists and NumPy arrays use different memory management systems.
To know the actual difference between these methods, please refer to “Difference between numpy.array() and numpy.asarray()“.
Specifying Data Types in NumPy Arrays
When converting a list to a NumPy array, we can also explicitly specify the data type of the elements using the dtype parameter.
import numpy as np
# List contains float number
a = [1.5, 2.8, 3.1]
# Convert to NumPy array with specified data type (integer)
arr = np.array(a, dtype=int)
print(arr)
Output
[1 2 3]
Converting Nested Lists to NumPy Arrays
We can also convert nested lists (i.e., lists of lists) into multi-dimensional NumPy arrays. This is particularly useful for representing matrices.
import numpy as np
# Define a nested Python list
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Convert to a 2D NumPy array
mat = np.array(a)
print(mat)
Output
[[1 2 3] [4 5 6] [7 8 9]]
Explanation: Each inner list becomes a row in the NumPy 2D array and this allow efficient mathematical operations on rows and columns.
Please refer “Convert Python Nested Lists to Multidimensional NumPy Arrays” for deep dive into this topic.
Reshaping NumPy Arrays
After converting a list into a NumPy array, we may want to reshape it. This can be useful for creating matrices or preparing data for machine learning.
import numpy as np
a = [1, 2, 3, 4, 5, 6]
# Convert to NumPy array
arr = np.array(a)
# Reshape the array to 2 rows and 3 columns
resArr = arr.reshape(2, 3)
print(resArr)
Output
[[1 2 3] [4 5 6]]
Explanation: We reshaped a 1D array into a 2D array with 2 rows and 3 columns.
Please refer “Reshape NumPy Array” for deep dive into this topic.
Combining Multiple Lists into a NumPy Array
We may also need to combine multiple lists into a single NumPy array. This is easily done using np.array().
import numpy as np
# Define multiple Python lists
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
# Combine lists into a single NumPy array
arr = np.array([a, b, c])
print(arr)
Output
[[1 2 3] [4 5 6] [7 8 9]]
Explanation: Each list becomes a row in a 2D NumPy array, making it easy to handle multiple lists as one unit.
Related Articles:
- Difference between numpy.array() and numpy.asarray()
- Convert a 1D array to a 2D Numpy array
- Convert NumPy array to list