NumPy Library
NumPy is the core
library for scientific computing in Python. It provides a high-performance
multidimensional array object, and tools for working with these arrays.
Create 2-D array using numpy from
list
import numpy as np
mylist=[[1,2,3],[4,5,6],[7,8,9]]
np.array(mylist)
Create 1-D and 2-D array using random
values
np.random.rand(5) #uniform distribution of random
values
np.random.rand(4,4)
# create 2-D array of random values
np.random.randn(4) #std. normal distribution, centered
around 0
Fetch Max on Min value from array
arr1=np.random.randint(0,100,10) #10 random integer elements
arr1.max() or
arr1.min()
Playing with the data using numpy
library
arr=np.arange(0,11) #create array of 10 elements between 1—10
arr[:5] #fetch 1st
5 elements of array arr
arr[5:] #fetch
elements from 5th positions till last elements
arr[:2]=100 #
replace the 1st two elements of array with value 100
arr=np.arange(0,25)
arr.reshape(5,5) #reshape method which
reshapes 1-D array into 2-D array
arr_2d=np.array([[1,2,3],[4,5,6],[7,8,9]]) #create 2-D array
arr_2d[:2,1:] # select elements in a particular row and column in 2D array
arr=np.arange(0,11)
arr > 5 #return
True for elements position which is more than 5
arr[arr<5]
# return True for elements position which is more than 5 and
pick array values for True positions
arr+arr #
element by element array addition
mat = np.arange(1,26).reshape(5,5) #generate 1D array of 15 elements and
convert it into 2D array
mat.std() #finding standard deviation on
elements
mat.sum(axis=0) #summing elements column wise in given array
mat
Hope you had enjoyed working with these basics of NumPy library. in next post, I would come up with another popular library used in Data Analytics.
Happy Learning! Please post your comments below :-)