Method, Packages , Numpy




Method
Method are similar to function( yes it is a function) but are different in a sense than it is object. Method are according to the type of object( like string, list, float)
They work differently on different object
for eg:

Name = "Viking"
Name.upper()
output = VIKING

Similarly count of "i" in Name ( yes in "viking")
No_of_I = Name.count("i")

append() //takes only one character to append
reverse() // doesnot take any thing and usually use in List


Packages
There are Many packages available in Python in which most commonly used packages are :
1) Numpy         = Numeric Calculation
2) Matplotlib    = Data Visuallisation
3) Sci-kit learn = Machine Learning Algorithm

We can install this package using "pip" install, but i recommend using "Anaconda" which is free to use.

we import packages(Whole) eg:
import math   or import numpy as np
math.pi           or np.array([1,2,3])

or
from numpy import *
we can import as part of packages also
from numpy import array


Numpy
let's start with why Numpy ?
height  = [1.45, 1.32 , 1.54, 1.47]
weight = [45, 50, 67, 61 ]
BMI = height / weight **2
Don't expect output for this since pure python cannot go through every itemset of the list. so, here comes our "Numpy"

import numpy as np  // we named np for covenience
height  = [1.45, 1.32 , 1.54, 1.47]
np_height = np.array(height)
weight = [45, 50, 67, 61 ]
np_weight = np.array(weight)
BMI = np_height / np_weight ** 2

yeah great right ? but it comes with some trade off like:
list can have any variable x = [1, 2.3, "hello", True]
but np.array([1,34,5,5] have only one data type
similarly, when you add two normal list
x= [1,2,3,4] + [1,2,3,4] = [1,2,3,4,1,2,3,4]
but numpy array may result more numerically like
x= [1,2,3,4] + [1,2,3,4] = [2,4,6,8]



Numpy subsetting
Subsetting is similar to list where we access the item of list.
Mark = array([78, 82, 92, 87, 64, 45])
Mark[1] (output : 82)
Mark >80 (ouput: array([False, True, True, True, False, False], dtype = bool)
Mark[Mark>90] (output: 92)
Print(type(np.Mark)) (output: numpy.ndarray)
ie. ndarray =  N dimensional Array


2D Numpy ArrayFollowing code creates a 2 dimensional Numpy array
np_2d = np.array([[1,2,3,4,5],
                               [5,6,7,8,9]])
np_2d.shape (output: 2 rows and 5 columns)
Now, lets subset the 2d array we have just created

To select single Row
np_2d[0]     or  np_2d[1]
np_2d[0][:]  or  np_2d[1, :]
To select a single Column
np_2d[:][1]  or  np_2d[:, 5]
To select a single point
np_2d[1][2]  or np_2d[1,2] // just like co-ordinate
To select a cube
np_data[:][1:3]  or np_data[:, 1:3]



Comments

Popular Posts