The Power of NumPy

4. NumPy Arrays Part 1: Declaration Methods

In NumPy, there are generally 6 methods to declare an array, we will be covering the two most basic methods.

Method 1: Converting Python structures into NumPy Arrays

As a quick review from our Array course from Intro to Python course:

An array is the most basic and widely used data structure designed to hold a fixed number of data points (called the array size) of the same type in contiguous memory locations. The fixed number and data type need to be provided when the array is created, but the data itself can be manipulated and modified later, which is what makes an array powerful.

Python does not support arrays and instead uses a data structure called a list that functions very similarly.

Declaring a list in Python can be done like this:

list_of_numbers = [1, 2, 3, 4, 5, 6]

Converting this array into NumPy can be done with the np.array() function like this:

# Creating a Python list
list_of_numbers = [1, 2, 3, 4, 5, 6]

# Converting it into a NumPy array with np.array()
numpy_list_of_numbers = np.array(list_of_numbers)

# You could also do this without declaring list_of_numbers first
numpy_list = np.array([1, 2, 3, 4, 5, 6])

You can also change the desired data type of the resulting array with the dtype parameter for the np.array() function:

Just like Python, NumPy arrays are zero-indexed. This means you start counting from 0 rather than 1 when referring to the position of an element in an array.

Method 2: Using NumPy functions to create NumPy Arrays

NumPy has a whole variety of incredibly useful functions that make creating commonly used arrays easy to declare.

This section will cover two of the most useful ones.

np.arange creates an array with regularly incrementing values.

Giving one number as input creates an array of that length with numbers starting from 0 incrementing to that number - 1 until the end of the array.

np.arange(10)
# returns array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.arange(4)
# returns array([0, 1, 2, 3])

Giving two numbers as input creates an array of that length with the first number incrementing to the latter number - 1 until the end of the array.

np.arange(5, 10)
# returns array([ 5, 6, 7, 8, 9])

np.arange(2, 4)
# returns array([2, 3])

You can also change the increment by adding a third parameter of the desired step size.

np.arange(5, 10, 0.5)
# returns array([ 5.5, 6, 6.5,  7, 7.5, 8, 8.5, 9, 9.5]), notice the increment of 0.5

np.arange(2, 3, 0.1)
# returns array([2.,  2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])), notice the increment of 0.1

Another useful function is np.zeros().

With np.zeros() you can create an array of any dimension you like filled with 0 values.

np.zeros(5)
# returns array([0, 0, 0, 0, 0])

np.zeros((2, 3))
# returns array([[0., 0., 0.],
#                       [0., 0., 0.]])

np.zeros((2, 3, 2))
# returns array([[[0., 0.],
#                         [0., 0.],
#                         [0., 0.]],

#                        [[0., 0.],
#                         [0., 0.],
#                         [0., 0.]]])

np.ones() does the same thing but with the number 1.

Source: https://numpy.org/doc/stable/user/basics.creation.html