Connect with us

Hi, what are you looking for?

# Learn Numpy With Examples – 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

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("Subtracting" , x - 1)

y = np.array([[1 , 2 , 3] , [4 , 5 , 6]])
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 to collect the data of 2 arrays separately

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

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

### Cyber Security

TCP/IP is a set of rules that allow computers to communicate with each other on a network such as the Internet. Currently, we do...

### News

According to a study from Dartmouth College, Dartmouth’s Tuck School of Business, and Indiana University, artificial intelligence systems can be trained to produce human-like...

### Game Development

In this article, we are going to add the double jump feature for a simple 2D platform game. You can implement it in a...

### Cyber Security

In this article, we are going to look at XSS attack types and how these attacks harm us. Also, we are going to learn...