Connect with us

Hi, what are you looking for?

Data Science

Learn Numpy With Examples – 2

Learn Numpy With Examples - Part two
Learn Numpy With Examples - Part two

For the beginner level, we will learn Numpy basic functions and Random methods in the second part of the Numpy series. If you want to reach the first section, you can reach it here.

As in the first lesson, we will repeat the topics we learned with examples, and also the codes of the topics in this lesson will be given on Github.

Github embed will be used for the readability and easy accessibility of the codes. Let’s start with the first topic.

Numpy Methods

Numpy makes your work easier with many methods and functions. In this section, we will cover simple methods. In the other section, we will look at more complex methods.

Let’s start with the Arange method, the arange method creates a numpy array with all the values between the two numbers your select.

import numpy as np

array = np.arange(1,10)
Output: [1 2 3 4 5 6 7 8 9]

One feature of the Arange method is that you can specify how the numbers will increase. The value you write to the 3rd argument determines the multiples of increments.

import numpy as np

array = np.arange(1,10,2)
Output: [1 3 5 7 9]
Zeros Method

Used to create a data set filled with 0. Can be used for calculations in data science. Let’s see it on python now.

import numpy as np

zero_array = np.zeros(5)
 # Create array with 5 zero

Output: [0. 0. 0. 0. 0.]

You can create 1D arrays as well as 2D arrays with the zeros method. Now let’s create a 2D array using the zeros method.

import numpy as np

zero_array = np.zeros((3 , 3))


[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Note: If you write ones instead of zeros function, the data will be 1 instead of 0. Using the same operations with ones will only convert the data from 0 to 1.

Linspace Method

The Linspace method is used to create a numpy array with values between 2 numbers divided by equal intervals.

import numpy as np

linspace = np.linspace(0 , 20 , 5)

Output: [0. 5. 10. 15. 20.]

As you can see, the last argument shows how much to divide between two numbers. The Arange method calculates the value between 2 numbers according to the 3rd argument.

import numpy as np

linspace = np.linspace(0 , 100 , 5)
arange = np.arange(0 , 100 , 5)

print("Linspace: " + linspace)
print("Arange:" , arange)
Linspace: [  0.  25.  50.  75. 100.]
Arange: [ 0  5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]
Eye Method In Numpy

It is used to create a matrix and creates a matrix with the values you provide. It has 1 argument, just enter how much matrix you want to create.

import numpy as np

matrix = np.eye(5)
 # Create 5 x 5 matrix

[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

Random Methods In Numpy

Now we will see the methods used more frequently in projects, after this section we will repeat all the topics with an example.

Let’s start with the random method, we will process it in 2 parts. The Random method generates an array of random data as evident from its name.

import numpy as np

randomFloat = np.random.randn(3)
randomFloat2D = np.random.randn(1,1)

print("Float Random: " + randomFloat)
print("Float2D Random:" , randomFloat2D)
Float Random: [ 0.30076992 -0.0201109   2.91586947]
Float2D Random: 
[[-1.56550763  2.38728812]
 [-1.33298184  1.07111689]]

Let’s look at randint, another method of the random module, and repeat it with examples.

import numpy as np

# Random number from 0 to 10
randomInt = np.random.randint(0 , 10)

# Random 10 numbers from 0 to 10
randomInt2 = np.random.randint(0 , 10 , 10)

print("Int Random:" , randomInt)
print("Int Random2:" , randomInt2)
Int Random: 6
Int Random2: [2 9 3 1 8 0 7 6 0 9]

Summary Numpy Methods

Now let’s summarize all the topics to understand the related topic. In this section, we will summarize all the topics covered. If you do not understand, please read again.

arange = to used create number array
zeros = to used create array with 0 value
ones = to used create array with 1 value
linspace = to used create array by to number range
eye = to used create matrix
randn = to used create random float value
randint = to used create random int value
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....