Python Lesson 18: Intro to NumPy (NumPy pt. 1)

Advertisements

Hello everyone,

Michael here, and today’s Python lesson will be a little different than the Python lessons we’ve done before. Now that I’ve covered some of the basic building blocks of Python, I figured it’s time to dive into some cooler stuff (e.g. data analytics, natural language processing, etc.) that you can do with Python. Right now, I’ll start with diving into the data analytics side of Python.

Now, let’s get started with learning the basics of one of Python’s most basic data analytics packages-NumPy. NumPy is a special Python package that is used for working with numerical arrays-NumPy is actually short for “numerical Python”.

However, to use NumPy, you need to be sure that it’s installed on your computer. To install a Python package to your computer, you would need to pip install the package on your computer’s command prompt like so:

If you’re wondering what pip is, it’s the package that Python uses to install other packages (think of pip as Python’s main package manager). Oftentimes, pip will already come installed with whatever Python IDE you chose to use (I use both Jupyter Notebook and Anaconda in my development), but in case it isn’t, here’s the link to download pip onto your computer-https://pypi.org/project/pip/.

To install the NumPy package, simply type this line onto your command prompt-pip install numpy. Sometimes, there are certain packages already pip installed on your laptop, so you’ll get a message on the command prompt that says Requirement already satisfied:, as I did above (apparently NumPy was already pip installed on my computer).

  • If you wanted to uninstall NumPy (or any other Python package), simply type pip uninstall numpy (you can replace this with any other python package you pip installed) onto the command prompt. The pip package manager will then ask you if you want to uninstall the python package.
  • You don’t have to pip install Python packages in any particular directory, but I’d recommend that you perform any pip installs in your main directory (the directory with the structure “C:/Users/[your username]”)

Now, if you wanted to see a list of all the Python packages that are installed on your computer (along with the corresponding versions of each package), type pip list onto the command prompt like so:

As you can see, I have numpy version 1.18.5 installed on my computer.

  • Depending on the Python library you need for your project, running pip list can help save you a pip install.

Now that I’ve covered all the pip install material, let’s dive right in to NumPy!

To use NumPy in your IDE, first run this line of code-import numpy as np. The as np part is completely optional since np is simply the alias for numpy. Including the as np part in your import statement allows you to refer to the NumPy package as np rather than numpy (in other words, as np is just convenient shorthand for NumPy).

Now here’s an example on how to create a simple NumPy array:

import numpy as np

array1 = np.array([2,4,6,8,10])

print(array1)

[ 2  4  6  8 10]

To create a NumPy array, simply use the np.array() function and use an array-like object as the parameter for this function. In this example, I used the list [2, 4, 6, 8, 10] as the parameter for the np.array() function.

  • For your information, NumPy arrays are stored with the type ndarray.

Now, I did mention that you can use an array-like object as the parameter for the np.array() function. Lists aren’t the only parameter accepted by this function; tuples work great too. Here’s an example of using a tuple in the np.array() function:

array2 = np.array((1,2,3))

print(array2)
print(type(array2))

[1 2 3]
<class 'numpy.ndarray'>

In this example, the tuple I passed into the np.array() function is converted into a NumPy array (of type ndarray). The magic of the np.array() function is that it can convert any array-like object (like a list or a tuple) into a NumPy array.

Pretty cool stuff right? Now, did you know that you can create multi-dimensional arrays, which are arrays with extra levels of array depth. Put simply, multi-dimensional arrays can contain any number of nested arrays, which are arrays within arrays.

Here’s how to make a 0-D (0-dimensional) array:

array3 = np.array(50)

print(array3)

50

Looks simple right? 0-dimensional arrays consist of a single element. That’s it.

Now, if you want to know to create a 1-D array, refer to the examples with array1 and array2, as these are two excellent examples of 1-D NumPy arrays. 1-D arrays consist of array-like objects (like lists or tuples) as their elements.

Now here’s an example of how to create a 2-D array:

array4 = np.array([[1, 3, 5], [7, 9, 11], [13, 15, 17]])

print(array4)
print(array4.ndim)

[[ 1  3  5]
 [ 7  9 11]
 [13 15 17]]
2

In this example, I created a 2-D array by wrapping a list of three 1-D arrays inside a pair of square brackets ([]). As for the ndim call, it simply shows you how many dimensions are in a NumPy array (2 in this case).

Now here’s an example of how to create a 3-D array:

array5 = np.array([[[3, 6, 9], [12, 15, 18]], [[3, 6, 9], [12, 15, 18]]])

print(array5)

[[[ 3  6  9]
  [12 15 18]]

 [[ 3  6  9]
  [12 15 18]]]

A NumPy 3-D array consists of several 2-D arrays; each 2-D array in the 3-D array is printed on a separate line, as you can see above.

  • Getting the brackets right for 3-D arrays does get tricky, so keep that in mind.

Now I’ve shown you how to create 0-D, 1-D, 2-D, and 3-D arrays. However, NumPy arrays aren’t limited to a 3-dimension maximum; the sky’s the limit when it comes to the amount of dimensions a NumPy array can have.

Let’s say we wanted to create a 6-dimensional NumPy array. How would we go about doing that? Take a look at the code below:

array6 = np.array([-10, -8, -6, -4, -2, 0], ndmin=6)

print(array6)

[[[[[[-10  -8  -6  -4  -2   0]]]]]]

To add X number of dimensions to a NumPy array, simply create a 1-D array and specify the number of dimensions you want in the array using the ndmin parameter.

As you can see, since I created a 6-dimensional array in the above example, there are six pairs of square brackets surrounding the array. Just for fun, let’s see what happens when we create a 50-dimensional array (using the same elements as array6):

array7 = np.array([-10, -8, -6, -4, -2, 0], ndmin=50)

print(array7)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-f4b851766b21> in <module>
----> 1 array7 = np.array([-10, -8, -6, -4, -2, 0], ndmin=50)
      2 
      3 print(array7)

ValueError: ndmin bigger than allowable number of dimensions NPY_MAXDIMS (=32)

As you can see, that didn’t work. Apparently NumPy arrays can only have 32 dimensions tops. Even an experienced Python coder like me learns something new everyday.

Thanks for reading,

Michael

Leave a ReplyCancel reply