NumPy - Creating Arrays
Thomas J. Kennedy
1 Overview
There are two main ways to initialize NumPy arrays:
- Directly in NumPy (e.g., setting everything to zero)
- Converting a Python
list
(or similar) structure
2 Array from Scratch
NumPy arrays can be…
-
Initialized to all zeroes
array_size = 8 zeroes_array = np.zeros(array_size) print(zeroes_array)
-
Initialized to all ones
array_size = 12 ones_array = np.ones(array_size) print(ones_array)
-
Allocated and left uninitialized
# Contents are "whatever happens to be in memory" array_size = 16 unitialized_array = np.empty(array_size) print(unitialized_array)
3 Array from a List or Tuple
Creating an array from an existing list seems straightforward…
python_list = [2, 4, 8, 16, 32, 64]
np_array = np.array(python_list)
print(np_array)
However, the resulting array will store int
s. To create an array of float
s… a decimal point must be included after each number.
python_list = [2., 4., 8., 16., 32., 64.]
np_array = np.array(python_list)
print(np_array)
You can also be explicit by using the dtype
keyword argument…
python_list = [2, 4, 8, 16, 32, 64]
np_array = np.array(python_list, dtype=np.double)
print(np_array)
4 Multiple Dimensions
It is possible to create a matrix (or even a tensor) by providing a multi-level list, e.g.,
matrix = [
[3, 2],
[2, 5]
]
matrix = np.array(matrix, dtype=np.double)