Intro To Py and ML - Part 1
Intro To Py and ML - Part 1
Intro To Py and ML - Part 1
DATA ANALYTICS
OAU5362/DAM5362
May 2021
OUTCOMES
2
OUTLINE
• Jupyter Notebook
• First Python codes and Variables
• Variables and Strings
• Operators
• Decision
• Repetition
• Functions
• Library and Array
• Data Frame (Table)
3
JUPYTER NOTEBOOK
• “Notebook” - documents containing code and rich text elements i.e. figures, links, equations – for data analysis
• Installation:
1.Download Anaconda from https://www.anaconda.com/products/individual (find latest Python version)
2.Install the Anaconda by following the instructions on the download page (choose default settings)
3.Well done!
• To open Jupyter Notebook, click Start → Jupyter Notebook (Anaconda 3). The apps will be opened in the
browser on the link http://localhost:8888/
4
JUPYTER NOTEBOOK
• The tabs:
o Files – location where files are kept
o Running – shows the works that are in progress
o Clusters – allows parallel computing framework
• To start a new notebook, click New → Python 3 (Fig. 1). Folder can also be created to organized our files.
• New notebook is shown in Fig. 2
• Change the notebook name by clicking on the “Untitled” (Fig. 3)
Fig. 1
Fig. 2
Fig. 3
5
FIRST PYTHON CODES & VARIABLES
• To run the code, either click “Run” button OR press Shift + Enter OR Ctrl + Enter (cell must be selected)
• To create new cell, click “+” button OR Esc + a (new cell created above) or Esc + b (new cell created below)
• String:
▪ fr1 = "banana"
fr2 = "mango"
fr3, fr4, fr5 = "rambutan", "durian", "water melon"
▪ print(fr2[0])
▪ print(fr2[3])
▪ print(len(fr3))
Q: change the spe, pre and tem values to other number and see what happens
• Compare with:
10
REPETITION
• Repetition (a.k.a loop) is a process to execute the block of codes for several times. This is done based on
condition.
11
FUNCTIONS
• A function is a block of codes that becomes executed when it is called – using its name.
• So far, we have seen the print function that displays the values we supply in the parenthesis (this is called
arguments).
• Print function and many others are predefined functions provided by the tools/library.
• Other than predefined functions, we may also create functions, and these are known as user-defined functions.
• So far, variables that we have seen are normal variables – store a single value only. E.g.:
num1 = 3
num1 = 3 * 12
print(num1)
• An array variable can store multiple values. To utilize array, the numpy library is used.
import numpy #importing library
arr = numpy.array([10, 22, 35, 44, 51]) #using function
print(arr)
• Type and run: • Type and run:
▪ print(arr[1]) ▪ print(arr[3:])
▪ print(arr[4]) ▪ print(arr[:3])
▪ ans = arr[0]*arr[3] ▪ print(arr[-2:])
print(ans) ▪ print(sum(arr))
▪ print(arr[0:3]) 13
LIBRARY & ARRAY
• Try:
a_list = numpy.array([1,25,"Three"])
print(a_list[0]+a_list[1])
b_list = numpy.array([1,25,3])
print(b_list[0]+b_list[1])
14
DICTIONARY
• A dictionary is a collection of unordered, changeable and indexed data.
• To delete item:
del cars["model"]
print(cars) 15
DATA FRAME (TABLE)