ENGG1003 Lab09 PythonDataProcessing(2) (1)
ENGG1003 Lab09 PythonDataProcessing(2) (1)
2
Activities
Iteration: for-loop
3
Academic Honesty
4
Mandatory Tasks
5
Scenario
Suppose you are now a student helper in the PHED
department, or working as an intern within the health service
sector
We are going to process any given data set (in such format)
via Python
6
Task 1: Calculate BMI by Function
Recall from earlier laboratories, the BMI measurement is defined as
We have done this via excel earlier; we will also do it via Python here
Important notes on the above definition:
Weight is measured in kilogram
Height is in meter scale
Function definition in python:
7
Task 1: Calculate BMI by Function
# define a function with input parameters
8
Task 1: Calculate BMI by Function
Fill in this
blank!
The sample code should look like the above
You may attempt this task by modifying the given line(s)
Notice the variable bmi should be overwritten upon finishing the task
Pay attention to unit conversion!
9
Task 1: Expected Output
10
Task 2: Body Check
Body Status BMI
Underweight below 18.5
Fill in these
blanks!!
12
Task 2: Expected Output
13
Task 3: Automation of Data Processing
2 155 68
bodyCheck bmi = 39.7
3 153 93
bodyCheck bmi = 13.4
4 181 44
Notice that we have been performing the task over few records only
Each time we have to manually input the data for each record in the console
This is fine for 4 data records but certainly not for 100,000,000 records!
14
Task 3: Automation of Data Processing
2 155 68 process()
bmi = 39.7
3 153 93
avg = 25.54
15
Task 3: Automation of Data Processing
We will assume all input data are tidy up as a List of
Tuples
Suppose there are m data records
The first data record is stored as (h1, w1)If you forgot what List and
Tuple are and how to use
The second is (h2, w2), and so on…
them…
The m-th data record will be (hm, wm)
Check your lecture slides!
16
List of Tuples: Example
heigh
weight
t
1 163 55
2 155 68
3 153 93
4 181 44
To give you a numerical example, consider the data set described above
We can input the data like the RHS picture
When we type X[i] it gives us the i-th tuple where i starts from 0
Each X[i] is a tuple of the form (h, w)
Consult the lecture notes on how to use for-loop to iterate through list X
17
Task 3: Automation of Data Processing
Fill in these
blanks
with a for-loop!!
18
Sample Output
BMI of a person with height=168 cm, weight=74 kg is: 26.218820861678008
Student Data: [(168, 74), (183, 61), (162, 65), (166, 46)]
A weight of 168 cm and 74 kg...
Your BMI is estimated as 26.218820861678008
Body Status: Overweight We will test and check your
A weight of 183 cm and 61 kg... functions with other data
Your BMI is estimated as 18.21493624772313 sets!
Body Status: Underweight
A weight of 162 cm and 65 kg...
Your BMI is estimated as 24.76756591982929
Body Status: Normal
A weight of 166 cm and 46 kg...
Your BMI is estimated as 16.693279140659023
Body Status: Underweight
The average BMI of this batch of data is 21.473650542472363
19
Bonus Task
20
Scenario
Alongside centimeters, people also express unit of length in inches
This bonus part exploits the conversion between inches and
centimeters:
1 inches = 2.54 centimeters
Task: write a Python function to:
Receive a string as input
Example:
Sample input: “The model is 10 inches tall”
(by given code that joins the returned list of split strings)
21
Task
Goal: know how to modify selective parts of a python list
Possible approach:
Replace the numeric value N with a new value using conversion N*2.54
22
Hints and Tips
You might have used split() to obtain a list of words from the input
string
Numbers from the output list are string type values also, you will need
to convert the number from a string to a suitable type, before
calculation
Ending Remark
23
Blackboard Submission