NumPy: The 10 functions that you should know.

TC. Lin
5 min readJan 3, 2023

--

As mentioned in my article on Data Preprocessing of Machine Learning, NumPy is considered to be the backbone of Data Science, as it dominates numerical computing in python. Essentially, it is the foundation of turning data into a series of numbers.

In order to easily apply NumPy like a pro, here are the most common functions that you will need to know when doing numerical computing and machine learning.

Note: The parameter(s) given in the example are those that are the most common used.

1. np.array()
Allows us to create n-dimensional arrays.


# Create a 1-dimensional array
a1 = np.array([1,2,3])

# Create a 2-dimensional array
a2 = np.array([[1,2,3],[4,5,6]])

# Create a 3-dimensional array
a3 = np.array([[[1,2,3],[4,5,6],[7,8,9]],
[[10,11,12],[13,14,15],[16,17,18]]])

2. _.shape, _.ndim, _.dtype, _.size
It is important to always keep track of the current shape and dimensions of your NumPy array, and to do so, we can apply these functions to the above variables like so:

_.shape describes the current shape of the array.
_.ndim describes the number of dimensions of the array.
_.dtype describes the data type of the array.
_.size describes the total number of elements in the array.

3. np.ones((_, _))
This function allows us to create a numpy array with an initial value of 1, according to the shape that is given.

# Create an array of shape (10, 2) with only ones
ones = np.ones((10,2))

This returns an array with 2 columns, 10 rows.

4. np.zeros((_, _))
Similar to the above, but this initiates the array with only zeros.

# Create an array of shape (7, 2, 3) of only zeros
zeros = np.zeros((7,2,3))

Initials the array with 3 columns, 2 rows, and 7 sets of this shape.

5. np.arange(start, stop, step)
To create an array of values that are evenly spaced out, we can make use of this function. We simply input a starting value and a ending value, followed by the step interval that we want.

# Create an array within a range of 0 and 100 with step 3
range_array = np.arange(0, 100, 3)

This will returns an array starting from 0, ending at 100, with a step of 3 in between.

6. np.random.randint(low, high, size)
This is another very useful function that allows us to create an array with random integers according to the given size. If high is not being filled (high = None), then the low will act as the highest integer in the sample.


# Create a random array with numbers between 0 and 10 of size (7, 2)
random_array = np.random.randint(10, size=(7,2))

This creates an array with 2 columns, 7 rows with random integer between 0 and 10.

6. np.random.random(size)
Similar to the randint, this returns an array of type floats between 0 and 1 according to the given size.

random_array = np.random.random((3,5))

This creates an array with 5 columns, 3 rows with random floats between 0 and 1.

7. np.random.seed(number)
Random number in Numpy is actually not random, but they use an idea like a random state. The number given in the parameter can be any number, and this number determines the random number that the numpy will generate.

For example, if I set the random.seed number to 13, no matter how many times I run any random functions, they will return the same random number, which in this case, not random anymore, but fixed.

This is very useful for us to replicate our result during ML or DL.

np.random.seed(13)

np.random.randint(10, size=(3,5))

This randint function will then always return the same number of integers everytime.

8. Accessing the index of array
Let’s say we have a following array:

This is a (3, 7) shape array, 7 columns, 3 rows.

array[0]

This will return the first row of the array: [0, 7, 2, 2, 0, 4, 9].

array[:2, :2]

Similarly, we can adjust the number of values that we want a function to return.
By using the above command, we get the first 2 columns of the first 2 rows of the array. In other words, the first 2 values of the first 2 rows.

9. Transposing an array
Let’s say we have a array with shape (3, 5), 5 columns & 3 rows.

By applying:

array.T

We get a transposed shape of the array, that is (5, 3), 3 columns & 5 rows.

10. Mathematic algorithms in NumPy
Like Python functions, NumPy has its own square, mean, max, min, std (standard deviation), var (variance) method.

If the data type is a NumPy data type, stick with NumPy method as NumPy does incredibly well in numerical computing, which means, it performs at a much faster speed when you have a large data.

All in all…

NumPy is a very robust library with a lot of very useful functions.
Most of us can never remember a function off the top of our head, and it requires us searching it up.

In fact, this is what the 90% of the programmers do when doing a project.

The 10 functions above are the most common functions that we will come across when using NumPy. Enjoy coding!

--

--

TC. Lin

Predicting the future isn't magic, it's artificial intelligence.