Connect with us

Hi, what are you looking for?

Learn Numpy With Examples - Part 1
Learn Numpy With Examples - Part 1

Data Science

Learn Numpy With Examples – 1

Unlike other educational articles, this article aims to learn through examples, rather than explaining a topic at length.

We will do examples, we will learn the concepts to be used before each example and we will look at their Syntax.

If you want to come prepared and repeat the topics a little bit, read the Numpy article for beginners here.

Numpy Array

Numpy Array is very similar to a regular string, which is more useful and offers more functions than regular arrays.

At the moment we will simply grasp the Numpy Array, then learn more advanced functions. Let’s review the Syntax.

import numpy as np

x = np.array([1 , 2 , 3]) # 1D array
y = np.array([[1 , 2] , [3 , 4]]) # 2D array
z = np.array(("a","b","c")) # Tuple to Array

In terms of syntax, it is very similar to a normal string, now let’s learn indexing and move on to our project from slow.

x[0] # Accessing first value
x[0:] # Accessing first value to end value
x[0:1] # From index 0 to index 1
y[[0] , [1]] # Retrieves the 1st index of the 1st array
y[[1] , [1]] # Retrieves the 2st index of the 2st array

Project With Numpy Array

In this project, we will have 2 different data sets and we will write a function for the user who wants to access the information in it.

For example, a dataset of books will be magazines in a dataset, and the user will determine the type and index to select the book with the input it enters.

import numpy as np

BooksAndMagazine = np.array([["B1" , "B2" , "B3"] , ["M1" , "M2"]])

We created two types of books and magazines. There are 3 books and 2 magazines. Now let’s start writing the function.

def Selector(types , index):
    return BooksAndMagazine[[types] , [index]]

types = int(input("Select Type: "))
index = int(input("Select index: "))

print("Your Selection:" , Selector(types , index))
Select Type: 0
Select index: 2
Your Selection: B3

Numpy Array Advanced Subjects

Next, we will learn the functions and operations of numpy arrays, then we will create a project again.

# Adding and Subtracting
import numpy as np

x = np.array([1,2,3])
print("Adding:" , x + 1)
print("Subtracting" , x - 1)

y = np.array([[1 , 2 , 3] , [4 , 5 , 6]])
print("Adding:" , y + 1)
print("Subtracting:" , y - 2)
[2 , 3 , 4]
[0 , 1 , 2]

[2 , 3 , 4],
[5 , 6 , 7]

[-1 , 0 , 1],
[2 , 3 , 4]

Let’s learn the concept of dtype and see how to change values in a array. dtype shows the type of data inside the array, and also allows you to change the type while creating the array.

import numpy as np

x = np.array([1, 2 , 3])

y = np.array([1.5, 2.2])

After creating the Array, you can use the dtype argument to change the types of data. This method will give you speed in type conversions.

import numpy as np
x = np.array([1 , 2 , 3] , dtype = np.float32)
[1. 2. 3.]

Next, we have operations with arrays, we will look at how we can do basic and complex operations with arrays.

import numpy as np

x = np.array([1 , 2 , 3])
y = np.array([4 , 16 , 25])

add = np.add(x , y)
 # add to collect the data of 2 arrays separately
print("Add:" , add)

sum = np.sum(x) 
# collects all data in it
print("Sum:" , sum)

Sqrt = np.sqrt(y)
 # Square root of Array
print("Sqrt:" , Sqrt)
Add: [ 5 18 28]
Sum: 6
Sqrt: [2. 4. 5.]
Click to comment

Leave a Reply

Your email address will not be published.

You May Also Like

Game Development

In this article, we will generate a movement script for FPS games in Unity. We’ll cover all the mechanics like camera control, crouching, and...

Game Development

In this article, we are going to look at how to use SQLite RDBMS in Unity 3D. After this article, you’ll be able to...

Machine Learning

In this article, we are going to learn how to build regression models with PyCaret in Python. If you don’t have any experience with...


In this article, we will make a simple login system in SwiftUI. After this article, you will be able to make a custom login...