M2M – June – Update #3

This month I am aiming to re-introduce myself to Python coding. This week, I decided to follow along to Jay Alammar’s post that I found on Hacker News.

His write-up focuses on NumPy, a foundational library that unlocks data analysis and machine learning in Python. Let’s import it and get started.

numpy_import_numpy

A fundamental building block of NumPy is the ability to create “arrays”, which can be anywhere from 1 to 3+ dimensions.

There are various options for how to create an array:

  • Specific the values in the array: np.array([1,2,3])
  • Create an array of all 1’s or all 0’s: np.ones(3) or np.zeros(5) based on how many values desired
  • Create an array of random decimals: np.random.random(8)

m2m_numpy_array_random

Once we have two arrays with the same dimension, we can add or multiply them. For individual arrays, we can scale them, find the max value, or sum them.

numpy_array_add_multiply_max

But arrays can contain multiple dimensions. This is when it starts to get interesting. We can do mathematical functions across the dimensions.

numpy_two_dimensional_create_add

Even if they are different shapes, we can still do the math functions.

numpy_two_dimension_multiply_unbalanced

We can reshape an array by specifying the dimensions we want, or even transpose.

numpy_reshape_transpose

Building on all this, we can add a third dimension.

numpy_3d_array

We can apply a 3-dimensional array to 2-dimension values over time, red/green/blue values for each pixel of an image, or feeding text data to a machine learning model.

With NumPy, the possibilities are endless. That’s all for this week.

Leave a Reply

Your email address will not be published. Required fields are marked *