Basics of Numpy
As we are now quite familiar with the numpy. Now let us explore it with some code & trick's which will help you to be a great Data Scientist.
Here we will see multiple examples and i'll explain it to you.
(1) np.zeros :
code :
import numpy as np
a=np.zeros((2,4)) #syntax = np.zeros((num_of_rows,num_of_columns))
print(a)
output :
>> [[ 0. 0. 0. 0.
0. 0. 0. 0.]]
a=np.zeros((2,4)) #syntax = np.zeros((num_of_rows,num_of_columns))
print(a)
output :
>> [[ 0. 0. 0. 0.
0. 0. 0. 0.]]
here you notice that .zeros is a function which is present in the numpy which we can use to create an array with number of row's(2) & column's(4) that we had specified.
(2) print numbers with specific interval :
code :
import pandas as pd
a=np.array((10,25,5)) #syntax = np.array((start_num,end_num,interval))
print(a)
output :
>>[10 15 20]
a=np.array((10,25,5)) #syntax = np.array((start_num,end_num,interval))
print(a)
output :
>>[10 15 20]
Now you must be thinking why i had not written 25...because the end_number is not included.
(3) np.linspace :
code :
(4) np.full :
code :
(5) random :
code :
(6) shape :
code :
Now notice here we print the shape not the matrix/array.In which the first element it is number of column i.e (2) & second element it is number of element it have i.e (3).
(7) range & .size :
code :
import numpy as np
a=np.array(5)
print(a.size)
print(a)
output :
>>5
[0 1 2 3 4]
In this we only gave a value i.e range & it created an 1D array of that range & .size helps us to show the size of that array.
(8) ndim :
code :
This will give us the dimension of that array.
(9) .dtype :
code :
It basically provides the data type of each elemnt in numpy array.
(3) np.linspace :
code :
import numpy as np
a=np.linspace(0,10,5) #syntax = np.linspace(start_num,end_num,num_of_split)
print(a)
output :
>>[0 2.5 5. 7.5 10]
At this in the last variable you will give the number of splits you want might give you float value.a=np.linspace(0,10,5) #syntax = np.linspace(start_num,end_num,num_of_split)
print(a)
output :
>>[0 2.5 5. 7.5 10]
(4) np.full :
code :
import numpy as np
a=np.full((3,4),5)
print(a)
output :
>>[[5 5 5]
[5 5 5]
[5 5 5]]
Remember np.zero we did exactly same thing but this time we printed the number which we want.a=np.full((3,4),5)
print(a)
output :
>>[[5 5 5]
[5 5 5]
[5 5 5]]
(5) random :
code :
import numpy as np
a=np.random.random(2,2)
print(a)
output :
>>[[0.856134546 0.97946144]
[1.884646544 2.66547456]]
Here we just given the number of row's & column's in which the random values will get filled automatically.a=np.random.random(2,2)
print(a)
output :
>>[[0.856134546 0.97946144]
[1.884646544 2.66547456]]
(6) shape :
code :
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a.shape)
output :
>>(2,3)
a=np.array([[1,2,3],[4,5,6]])
print(a.shape)
output :
>>(2,3)
Now notice here we print the shape not the matrix/array.In which the first element it is number of column i.e (2) & second element it is number of element it have i.e (3).
(7) range & .size :
code :
import numpy as np
a=np.array(5)
print(a.size)
print(a)
output :
>>5
[0 1 2 3 4]
In this we only gave a value i.e range & it created an 1D array of that range & .size helps us to show the size of that array.
(8) ndim :
code :
x=np.array([[1,2,3],[4,5,6]])
print(x.ndim)
output :
>>2
print(x.ndim)
output :
>>2
This will give us the dimension of that array.
(9) .dtype :
code :
x=np.array([[1,2,3],[4,5,6]])
print(x.dtype)
output :
>>int32
print(x.dtype)
output :
>>int32
It basically provides the data type of each elemnt in numpy array.
No comments:
No Spamming and No Offensive Language