Connect with us

Hi, what are you looking for?

Artificial Intelligence

Learn Numpy With Examples – 1

Learn Numpy With Examples - Part 1
Learn Numpy With Examples - Part 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. Required fields are marked *

You May Also Like

Daily News

GitHub’s new feature “passwordless authentication” aims to increase account security and provide a smoother user experience. By opting for the “Enable passkeys” option within...


clean code is essential for updating scripts, adding new features to code, etc. So every developer, must learn how to clean the code. With...

Daily News

Cloud computing is a system that is often heard today and that most developers use at least once. In this article, we’ll look at...

Game Development

Visual Studio Code without a doubt, most popular and loved code editor in the code editor market. Many developers love working with Vs Code....