NumPy tricks I wish I knew earlier

Manpreet Singh
3 min readSep 8, 2021

Welcome back! NumPy is an awesome Python package that’s used heavily in data science, so let’s talk about some NumPy tricks that I wish I had known earlier. Now, these tricks maybe random, but you could find ways to implement these inside of your projects! With this long introduction out of the way, let’s get started!

Generating Random Numbers

Starting off, one of the coolest things you can do with NumPy is generating random numbers, we use the np.random() function, here is an example of this:

import numpy as npnp.random.rand(2,10)

This is the output of this project:

This creates a 2 dimensional array with 10 random values.

Creating a Placeholder Array

Another awesome trick we have within NumPy is the ability of creating a placeholder array, what is a placeholder array? Essentially it’s an array that holds the shape that we want, but it’s full of zeros, to do this, we use the following command:

import numpy as npnp.zeros((2,10))

This will create a 2 dimensional array with 10 zeros.

Comparing 2 arrays

--

--

Manpreet Singh
Manpreet Singh

No responses yet