Professional Documents
Culture Documents
SBMT5730 pt3
SBMT5730 pt3
Email: jkwok@ust.hk
Course Plan – Part 7
Python – Python - Python – Python –
Introduction Python - App
Environment Basics Data Control Flow
Data Types,
Data
Python Colab Comment Type Looping
Analytics
Conversion
print() String
input() Objects
Operators
2
Program2_pt7.ipynb
3
Currency Exchange
[1] while True:
print("1: HKG to CNY")
print("2: HKG to USD")
print("3: Exit")
break
[2] choice = 2
if choice==1:
print("choice 1")
elif choice==2:
print("choice 2")
elif choice==3:
print("choice 3")
else:
print("Other choice")
4
Control Flow
Statements – if, elif, else
if-else
Objectives
while, break
5
Control Flow
Statements –
if-else
6
if-else
A decision making process if-else
7
Control flow statements - Basics
• Control flow statements and program structure uses indentation to
mark blocks (or scopes)
• The amount of white space (space or tab characters) at the start of a
line is very important.
• Python uses boolean expressions to evaluate conditions. The Boolean
values True and False are returned when an expression is compared
or evaluated.
8
Control flow statements –
Relational Operators
Symbol Meaning
== True, if it is equal
!= True, if not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
9
Control flow statements –
Boolean Operators
Operator Meaning
and Logical and
or Logical or
not Not
10
if, elif, else
11
if
if some_condition:
code block
[1] x = 12
if x > 10:
print("Hello")
Hello
[1] x = 32
if 30 < x < 40:
print("HKUST")
else:
print("MBA")
HKUST
13
if, elif, else
if some_condition:
code block
elif some_condition:
code block
else:
code block
[1] x = 10
y = 12
if x > y:
print("x>y")
elif x < y:
print("x<y")
else:
print("x=y")
x<y
14
while, break
15
Looping
A loop statement allows us to execute a statement or group of statements multiple times.
16
while loop
while condition:
code block
[1] i = 1
while i < 3:
print(i ** 2)
i = i+1
print('Bye')
1
4
Bye
17
break
• The break statement terminates the loop
containing it. Control of the program
flows to the statement immediately after
the body of the loop.
• If break statement is inside a nested loop
(loop inside another loop), break will
terminate the innermost loop.
18
while loop with break
Enter a number9
Enter a number8
Enter a number0
End
19
20
Exercise
Exercise 4
• Write a currency exchange program.
21
Course Plan – Part 8
Python – Python - Python – Python –
Introduction Python - App
Environment Basics Data Control Flow
Data Types,
Data
Python Colab Comment Type Looping
Analytics
Conversion
print() String
input() Objects
Operators
22
Program 3: Data Analytics
[1] # import packages
import pandas as pd
import numpy as np
24
Data Exploration
using pandas
25
Program3_pt8.ipynb
results.csv
Data Exploration - Loading Data
Given data file:
• Import useful libraries for data management results.csv
[1] # import useful libraries used for data loading and management
import pandas as pd
Drag the data file
https://pandas.pydata.org
to Colab first!
df=pd.read_csv('results.csv')
26
Data Exploration
• Simply typing in the name of the data object (technically it is called
pandas DataFrame object) will result in nicely formatted outputs
[3] # Type in the name of the data frame to view the dataset
df
… … … ……… … … … … … … … …
27
Data Exploration – Size
df.shape
(16, 10)
Try
df.shape[0]
df.shape[1]
28
Data Exploration – first 5
• Use .head() to view the first 5 instances
df.head()
29
Data Exploration – last 5
• Use .tail() to view the last 5 instances
df.tail()
30
View Dataset Information – Information
• Use .info() and .describe() to get an overview of the dataset
[7] # Use .info() to get the types of features
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16 entries, 0 to 15
Data columns (total 10 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 no_of_CNN 16 non-null int64
10 numeric features (int64, float64)
1 Filter_size 16 non-null int64
2 no_of_DNN 16 non-null int64
3 neurons 16 non-null int64
4 epochs 16 non-null int64
5 batch_size 16 non-null int64
6 Training_loss 16 non-null float64
7 Training_accuracy 16 non-null float64
8 Test_loss 16 non-null float64
9 Test_accuracy 16 non-null float64
dtypes: float64(4), int64(6)
memory usage: 1.4 KB
31
View Dataset Information – Description
• Use .info() and .describe() to get an overview of the dataset
[8] # Use .describe() to get the summary statistics of numeric features (count, mean,
standard deviation, min, Q1, Q2, Q3, and max)
df.describe()
Only shows the
numeric features
32
View Dataset Information – Description
df['Test_loss'].describe()
count 16.000000
mean 0.730551
std 0.066901
min 0.681946
25% 0.694514
50% 0.703078
75% 0.733349
max 0.938225
Name: Test_loss, dtype: float64
33
View Dataset Information – Column Names
df.columns
34
View Dataset Information –
A Column / Feature
• Use df['neurons'] to get ALL data in a column/feature
[11] # View data in a column / feature
df['neurons']
0 16
1 64
2 16
3 64
4 16
5 64
6 16
7 64
8 16
9 64
10 16
11 64
12 16
13 64
14 16
15 64
Name: neurons, dtype: int64 35
Removing
Column(s)/Feature(s)
36
Removing Column(s)/Feature(s)
• Use .drop() to remove column(s) or feature(s)
[12] # Use .columns to get the names of ALL features
df.columns
37
Slicing and Indexing
using
pandas.DataFrame.iloc
38
After df.drop()
Indexing Negative
index
Positive
index
value value
-16 0
-15 1
-14 2
-13 3
-12 4
-11 5
-10 6
-9 7
-8 8
-7 9
-6 10
-5 11
-4 12
-3 13
-2 14
-1 15
39
.iloc
• .iloc[index] is primarily integer position based (from 0 to length-1)
of the axis.
• Usage:
df.iloc[0]
df.iloc[1]
df.iloc[0:3]
df['Training_loss'].iloc[1]
df['Training_loss'].iloc[-1]
40
Indexing - positive value
df.iloc[start:end] # items start through end-1
df.iloc[start:] # items start through the rest of the list
df.iloc[:end] # items from the beginning through end-1
df.iloc[:] # a copy of the whole list
Examples
df.iloc[0:3]
df.iloc[:3]
df.iloc[10:]
df.iloc[:]
41
Indexing – negative value
df.iloc[start:end]
start or end may be a negative number, which counts from the end of
the array instead of the beginning.
Examples
df.iloc[-1] # last item in the list
df.iloc[-2:] # last two items in the list
df.iloc[:-2] # everything except the last two items
42
Basic Operations
43
Basic Operations - mean(), max(), min(), std()
• Here are some commonly used built-in functions
.mean()
.max()
.min()
.std()
• Usage:
df['Training_loss'].mean()
df['Training_loss'].max()
df['Training_loss'].min()
df['Training_loss'].std()
44
45
Exercise
Given data file:
Ex5_data.csv
Exercise 5
With the given dataset, find out the following and present your findings to
your manager.
• What is the size of the dataset? (how many samples and how many
features)
• Determine the highest accuracy in “Training” and “Test”?
(Hint: find out the highest values in “Accuracy of train dataset” and “Accuracy of test
dataset”, then use “if-else” statement to present the output)
Expected answer:
46
Course Plan
Python – Python - Python – Python –
Introduction Python - App
Environment Basics Data Control Flow
Data Types,
Data
Python Colab Comment Type Looping
Analytics
Conversion
print() String
input() Objects
Operators
47
End