Connect with us

Hi, what are you looking for?

Data Science

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))
Output:
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)
Output:
[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])
print(x.dtype) 

y = np.array([1.5, 2.2])
print(y.dtype)
Output: 
int32
float64

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)
print(x)
print(x.dtype)
Output:
[1. 2. 3.]
float32

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)
Output:
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

Programming

This article is prepared to explain concepts that will be important for programming beginners such as data types, variables, heap and stack, and operators....

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...