Introduction To Numpy
NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of functions for processing those arrays.Basically NumPy arrays are 50x faster then python lists that’s why we may have to use NumPy to handle big data .The array object in NumPy is called “ndarray”, it provides a lot of supporting functions that make working with “ndarray” very easy.Arrays are very frequently used in data science, where speed and resources are very important.
INSTALLATION AND IMPORTING NumPy
First of all open your Jupyter notebook,then go to your command panel write “pip install numpy”.Here we go,you are done with installation of NumPy library .Now,to use NumPy library you need to import NumPy in your notebook,to do so write “import numpy as np”.You are ready to use NumPy Library.
GETTING STARTED WITH NumPy
1.Converting a list into a array
Let say,
Import numpy as np
my_list=[1,2,3,4,5]
#one dimension array
arr=np.array(my_list)
print(arr) #it will print a array
print(type(arr)) ##type array
######Two Dimensional array
my_list=[[1,2,3][4,5,6][6,7,8]]
arr=np.array(my_list)
##indexing of arrays are similar to list indexing
2.Creating automatic one dimensional array
arr=np.arange(a,b,c) #a=star(inclusive),b=end(exclusive),c=step
##it will generate array like for loop
**Split of an array
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)
**output
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
**to find a specific eliment index
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
output
(array([3, 5, 6], dtype=int64),)
*to sort an array
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))
*output
[0 1 2 3]
*to filter an array with some specific condition
arr = np.array([41, 42, 43, 44])
filter_arr = []
for element in arr:
if element > 42:
filter_arr.append(True)
else:
filter_arr.append(False)
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)
*output
[False, False, True, True]
[43 44]
3.Generating matrix with Natural Numbers
arr=np.zeros(a) #a → length
output=array([0., 0., 0.]) #a=3
arr=np.zeros((a,b)) #a→row,by→column
output=array([[0., 0., 0.], #here a=2 and b=3
[0., 0., 0.]])
####we can also generate with other numbers
arr=np.ones((2,3))
arr=np.twos((5,5))
4.Generating fractional array (linspace)
arr=np.linspace(x,y,z)
x →starting number(inclusive),y →ending number(inclusive),z →total number of numbers
5.Generating Identity metrics
arr=np.eye(x) ##x →Number of rows and column
6.the random number numpy genarate is basically psudo random number cause there is and algorithm that generate random number random can generate three types of random number interger,float and negative.
arrays of random numbers
arr=np.random.rand(a) #a —>single line length
arr=np.random.rand(a,b) #a by b matrix
np.random.randn(2,4) #row column dimension float
np.random.randint(1,100) #it will generate a random number from 0 to 99
np.random.randint(1,100,10) # it will generate 10 random int number from 1 to 100 and 100 exclusive
7.#reshape method
arr= np.arange(25)
arr.reshape(5,5)
8.#to return max value of a array
arr.max()
9.# to return min
arr.min()
10.# to see max and min value location # arg
arr.argmax()
arr.argmin()
11.##numpy indexing and selection
arr=np.arange(1,10)
arr
arr[8]
arr[2:5]
12.# to change the value by using index
arr[0:3]=100
arr
13.##to access any element from the arr_2D
arr_2D=np.array([[1,2,3],[4,5,6],[7,8,9]])
arr_2D[0][1] #matrix concept
14.##conditional selection
arr=np.arange(1,15)
arr
arr>5
bool_arr=arr>5
arr[bool_arr]
15.###numpy operations
import numpy as np
arr=np.arange(10)
Arr+arr
Arr+100
16.#numpy subtract
arr-arr
17.##numpy multiplication
arr*arr
19.#numpy division
arr/arr
0 by 0 infinity
20.##numpy exponential
arr**2
np.sin(arr)
Top comments (0)