From Novice to Ninja: NumPy Arrays in Python — Day 01 (Getting Started with NumPy arrays)

Data Science Delight
4 min readJul 4, 2023

--

In this exciting series, we will take you on a journey from being a beginner to becoming a ninja in handling NumPy arrays. Today, we kickstart our adventure with Day 01.

Stay tuned till the end of this session for an exciting exercise section, where you can practice & reinforce the concepts learned!

∘ Table of Contents:
·
What are NumPy arrays?
Why use NumPy?
Python Lists Vs NumPy
One-dimensional NumPy Array:
Two-dimensional NumPy Array:
·
Creating array using NumPy functions:
→ Creating a NumPy array using 'arange' method:
→ Generating arrays of all zeros:
→ Generating array with equally spaced values:
→ Generating Identity matrix using NumPy:
·
Exercise Questions:
·
That’s it for now:

What are NumPy arrays?

NumPy arrays are fundamental data structures in the NumPy library for Python. They are similar to Python lists but provide many additional features and benefits for numerical computing.

NumPy arrays are homogenous meaning they can only contain elements of same data type. NumPy arrays are created using “np.array()” function, which takes Python lists or tuple as input and returns a NumPy array.

Before diving directly into the code, let’s first understand:

Why use NumPy?

NumPy provides a more efficient, optimized, and powerful toolset for working with numerical data than Python lists. Python lists are flexible and can store different data types but, they are less efficient for numerical operations and do not provide the same level of functionality as NumPy arrays.

Python Lists Vs NumPy

  • Python lists contain elements of different data types whereas NumPy arrays are homogenous and contain elements of the same data type.
  • NumPy arrays are faster than Python lists for many numerical operations, especially for larger data.
  • NumPy arrays occupy less memory & are more convenient to use compared to Python lists.
  • NumPy arrays provide powerful indexing and slicing capabilities than Python lists, making it easier to work with multi-dimensional data.

Creating an Array from List:

import numpy as np

lst = [1, 2, 3, 4, 5]
arr = np.array(lst)

print(arr)

Here, ‘np’ acts as a short form for NumPy.

One-dimensional NumPy Array:

Vector:

import numpy as np

vec = np.array([10, 50, 87])

vec

Two-dimensional NumPy Array:

Matrix:

mat = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mat

Matrices are 2-D array but note that matrices can also have one row & one column.

NOTE: For passing a Matrix we use list of list.

NumPy arrays can also be created using functions like “np.zeros()”, “np.ones()”, “np.random.rand()”, etc.

Creating array using NumPy functions:

Creating a NumPy array using 'arange' method:

The most common way of creating a NumPy array is using “np.arange()” function. The “np.arange()” takes in argument: (start, stop, stepsize)

arr = np.arange(0, 11, 2)
arr

The np.arange() function is similar to that of python range function. The function returns an array containing values from start value upto, but not including the stop value, incrementing by specified step value.

→ Generating arrays of all zeros:

For generating arrays of all zeros, we can use “np.zeros()” function:

Case — I: For a one-dimensional vector, we can pass a single digit inside a tuple:

zer1 = np.zeros(3)
zer1

Case — II: For a two-dimensional array, we can pass a tuple of the dimensions we want:

zer2 = np.zers((2, 3))
zer2

→ Generating arrays with equally spaced values:

Suppose I want to create a 1d array containing 25 evenly spaced values from 1 to 10. In such case, we use “np.linspace()” function.

It takes in argument: (start, stop, no. of values)

arr2 = np.linspace(1, 10, 25)
arr2

The resultant array might look like a 2d array but we can say the size of the matrix just by seeing the brackets at the opening & closing end.

Here it has only one square bracket at the opening & closing end so it is 1d arry. If it has 2 open square brackets at the opening end and 2 closing square brackets at the closing end then it is considered as 2d array.

→ Generating Identity matrix using NumPy:

We can use “np.eye()” function to create identity matrix.

It takes in argument: (single digit)

idt = np.eye(3)
idt

It is an useful function when you are dealing with linear algebra problems.

This represents a 2d square matrix, where the number of rows is equal to the number of columns, and it has ones along the diagonal while all other elements are zeros. That’s the reason it takes only a single digit as an argument.

Exercise Questions:

Now, your task is to try out the below questions on your own based on the NumPy functions that we have discussed in this session, and if you have any doubts feel free to ask me.

For each question, write the code using the appropriate NumPy function to generate the desired array:

  1. Create an array that contains all the even numbers from 2 to 20.
  2. Create an array of 5 equidistant values between 0 and 1.
  3. Create a 2D array of shape (3, 4) filled with zeros.
  4. Create a 3D array of shape (2, 2, 3) filled with zeros.
  5. Generate a 2D array of shape (5, 5) filled with ones.
  6. Create a 5x5 identity matrix.
  7. Difference between arange() and linspace().

That’s it for Day 01:

That’s it for today’s session. In the next session, we will be discussing about NumPy functions & methods in detail along with the code.

Please Subscribe/Follow, Like, Share, and Clap as it would encourage me to write more such blog posts espescially on data analytics and data science field.

Stay Tuned!!

Thank you!

--

--

Data Science Delight

Content Creator | Sharing insights & tips on data science | Instagram: @datasciencedelight | YouTube: https://www.youtube.com/channel/UCpz2054mp5xfcBKUIctnhlw