ESIoT Lab 3 - Big Data and Python Programming
ESIoT Lab 3 - Big Data and Python Programming
Part A
Explore a Large Dataset
Learning Objectives
Explore a sample dataset to view the power of Big Data.
Background
Before data can become meaningful information, it needs to be processed.
Scenario
You are required to develop a systematic process to find a predetermined number. The
process developed is represented in a flowchart. Using the flowchart, we can check and
verify the logical process toward the problem.
Required Resources
PC with Internet access.
Part B
Using Excel to Forecast
Learning Objectives
1. Input data into a spreadsheet
2. Execute a data Forecast
Background / Scenario
Forecasting is a way of predicting values in the future based on data. Managers wants
data instantly in order to make decisions and they rely on techniques such as forecasting
to make those decisions. With big data, volumes of data is produced instantaneously.
This presents a challenge to collect and process this data in real time.
This lab exersice is very basic and is designed to just show you how forecasting is
performed in Microsoft Excel. You will be inputting a set of weekly grades and using
the forecast feature to see what grades are predicted for the next few weeks.
Note: The Forecast menu option is available in the 2016 version of Excel. If you do not
have this version, the formula is provided. You might do better copying the formula
from the lab than inputting it.
Note: If you do not have the Forecast icon available in the Data menu option, but have
the 2016 version of Excel, select the File menu option > Options > Add-Ins > Go >
enable the checkbox beside Analysis ToolPak > OK. If you return to the File >
Options > Add-Ins window, you should see the Analysis ToolPak in the top section
where the active add-ins list.
However, it's important to note that the specific location of the menu option might
differ slightly depending on the Excel interface language and potential
customizations. It's generally found under the Data tab, but in some cases, it might be
under a different tab or require additional steps to access. As a final-year engineering
2
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
3
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
• Microsoft Excel with the Analysis ToolPak option installed or ability to enable
as an Add-In
Step 1: Input the Data
In this part, you will input grades and dates into specific Microsoft Excel cells.
a) Open Microsoft Excel.
b) In the A1 cell, type Date.
c) In the B1 cell, type Grade.
d) In the C1 cell, type Forecast.
e) In the D1 cell, type Lower Confidence Boundary.
f) In the E1 cell, type Upper Confidence Boundary.
g) Starting with the A2 cell, type the following dates in the A2 through A11 cells:
08/01/2018, 15/01/2018, 22/01/2018, 29/01/2018, 05/02/2018, 12/02/2018,
19/02/2018, 26/02/2018, 05/03/2018, 12/03/2018, 19/03/2018, 26/03/2018,
02/04/2018, 09/04/2018
Note: If you get pound signs (###) in your cell, click, hold down, and drag the line
to the right of the dates to make the A column bigger or you can right-click the A
that is above the word “Date,” select Column width, type 10, and click OK.
h) Starting with the B2 cell, type the following grades in the B2 through B11 cells:
100, 90, 75, 80, 50, 95, 85, 100, 80, 75
4
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
d) Expand the Options area by clicking once on the arrow beside the word “Options.”
Notice that you can adjust the confidence interval (the upper and lower limits of
what Excel predicts you will score in the next few weeks).
e) Click the Create button. The window and graph shown should be similar to what is
shown. Note that the graph was moved below the data just to capture the screen.
A Forecast can't be created because the timeline isn't evenly spaced. The dates or
numbers in a valid timeline must have a consistent gap between consecutive points. For
example, use consecutive days that are always 7 days apart, or a numerical timeline of
consecutive indices.
f) Notice that Excel predicts that you will make 80.39 on March 19th, but is 95%
confident that it really will be a score somewhere between 47.54 and 113.23.
What score is predicted for 2nd April?
78.955
Within what range of scores is Excel 95% confident that you will make on 9th
April? – Answer these questions in your
5
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
6
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
g) If you do not have the 2016 version of Excel or if you just like using a formula
instead of the using the menu, you can input a formula and get the same numbers.
In C12 enter the following formula:
=FORECAST.ETS(A12,$B$2:$B$11,$A$2:$A$11,1,1)
In D12, enter the following formula: =C12-
FORECAST.ETS.CONFINT(A12,$B$2:B$11,$A$2:$A$11,0.95,1,1)
In E12, enter the following formula:
=C12+FORECAST.ETS.CONFINT(A12,$B$2:B$11,$A$2:$A$11,0.95,1,1)
Breakdown of the Formulas:
• Cell C12:
o =FORECAST.ETS(A12,$B$2:$B$11,$A$2:$A$11,1,1)
o This formula calculates the forecasted value for the date in cell A12
based on the historical data in cells B2:B11 and their corresponding dates
in A2:A11.
o The arguments 1,1 specify parameters related to the forecasting model
used (Exponential Trend Smoothing in this case).
• Cell D12:
o =C12-
FORECAST.ETS.CONFINT(A12,$B$2:B$11,$A$2:$A$11,0.95,1,1)
o This formula calculates the lower confidence boundary for the forecasted
value in cell C12.
o It uses the FORECAST.ETS.CONFINT function with a confidence level
of 0.95 (representing a 95% confidence interval).
• Cell E12:
o =C12+FORECAST.ETS.CONFINT(A12,$B$2:B$11,$A$2:$A$11,0.95,
1,1)
o This formula calculates the upper confidence boundary for the forecasted
value in cell C12.
o It mirrors the formula in cell D12 but adds the confidence interval value
instead of subtracting it.
7
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Healthcare:
Big data in healthcare is used to personalize patient care by analyzing
massive databases of clinical notes, genomic data, and radiology scans.
Business and Marketing:
Companies use big data for real-time tracking of sales, customer
demand, and to streamline operations. Amazon, for example, analyzes
vast datasets concerning customer purchases and interactions to target
ads and product recommendations effectively.
9
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Part C
Python Programming
Learning Objectives
1. Creating Functions: Define and utilize reusable blocks of code.
2. Conditions and Loops: Control program flow using conditional statements
aniterative structures.
3. Python Modules: Import and leverage functionalities from external libraries.
4. Python Errors: Identify, understand, and handle errors during code execution.
5. Combining Python Knowledge and Understanding: Apply learned concepts
to solve problems and create programs.
Background
Python, a programming language, allows for simpler statements. Python is very easy to
use, powerful, and versatile. It has become the language of choice for many IoT
developers. One of the main reasons for the popularity of Python is the developer
community. Python developers have created and made available many specific modules
that can be imported into any program to immediately lend added functionality. In this
lab, you will learn and practice some basic Python programming. More specifically, we
will use Python version 3 in the lab.
Python is a free open-source language and environment but has tremendous potential in
the scientific computing domain. Python is a scripting language, meaning that it is
interpreted. It is a modern language, concise, easy to read and quick to learn. It's full of
freely available libraries, in particular scientific ones. It is useful in a wider setting and
widely used in industrial applications. Getting started with Python may seem a little
daunting at first, but the language has been designed with simplicity in mind. Like most
things, you need to start slow, master the basics, learn how to get a result and how to get
what you want from the code. Note that whilst a text editor is an ideal environment to
enter code into, it’s not an absolute necessity. You can enter and execute code directly
from the IDLE but a text editor, such as Notepad++, offers more advanced features and
colour coding when entering code.
Python is an interpreted language. This means that it inherits all the described
advantages and disadvantages. Of course, it adds some of its unique features to both
sets. If you want to program in Python, you will need the Python interpreter. You will
not be able to run your code without it. Fortunately, Python is free. This is one of its
most important advantages. Due to historical reasons, languages designed to be utilized
in the interpretation manner are often called scripting languages, while the source
programs encoded using them are called scripts.
Python is a widely used, interpreted, object-oriented, and high-level programming
language with dynamic semantics, used for general-purpose programming. And while
you may know the python as a large snake, the name of the Python programming
language comes from an old BBC television comedy sketch series called Monty
10
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Python's Flying Circus. At the height of its success, the Monty Python team were
performing their sketches to live audiences across the world, including at the
Hollywood Bowl. Since Monty Python is considered one of the two fundamental
nutrients to a programmer (the other being pizza), Python's creator named the language
in honour of the TV show.
Python is not a young language. It is mature and trustworthy. It's not a one-hit wonder.
It is a bright star in the programming firmament, and time spent learning Python is a
very good investment. There is more than one Python. There are two main kinds of
Python, called Python 2 and Python 3. Python 3 is the newer (to be precise, the current)
version of the language. It is going through its own evolution path, creating its own
standards and habits. These two versions of Python are not compatible with each other.
Python 3 is not just a better version of Python 2 - it is a completely different language,
although it's very similar to its predecessor. When you look at them from a distance,
they appear to be the same, but when you look closely, though, you notice a lot of
differences. If you are going to start a new Python project, you should use Python 3.
It is important to remember that there may be smaller or bigger differences between
subsequent Python 3 releases (e.g., Python 3.6 introduced ordered dictionary keys by
default under the CPython implementation) - the good news, though, is that all the
newer versions of Python 3 are backwards compatible with the previous versions of
Python 3. Whenever meaningful and important, we will always try to highlight those
differences in the course.
In addition to Python 2 and Python 3, there is more than one version of each. Another
Python family member is Cython. Cython is one of a possible number of solutions to
the most painful of Python's trait - the lack of efficiency. Large and complex
mathematical calculations may be easily coded in Python (much easier than in "C" or
any other traditional language), but the resulting code's execution may be extremely
time-consuming. Another version of Python is called Jython. "J" is for "Java". Imagine
a Python written in Java instead of C. This is useful, for example, if you develop large
and complex systems written entirely in Java and want to add some Python flexibility to
them. The traditional CPython may be difficult to integrate into such an environment, as
C and Java live in completely different worlds and do not share many common ideas.
Jython can communicate with existing Java infrastructure more effectively. This is why
some projects find it usable and needful.
Starting your work with Python
Now that you have Python 3 installed (refer to Lab 0), it is time to check if it works and
make the very first use of it. This will be a very simple procedure, but it should be
enough to convince you that the Python environment is complete and functional. There
are many ways of utilizing Python, especially if you are going to be a Python developer.
To start your work, you need the following tools:
• an editor which will support you in writing the code (it should have some
special features, not available in simple tools); this dedicated editor will give
you more than the standard OS equipment;
11
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
• a console in which you can launch your newly written code and stop it forcibly
when it gets out of control;
• a tool named a debugger, able to launch your code step by step and allowing
you to inspect it at each moment of execution.
Besides its many useful components, the Python 3 standard installation contains a very
simple but extremely useful application named IDLE. IDLE is an acronym: Integrated
Development and Learning Environment.
Popular IDE options for beginners include:
• IDLE: Bundled with Python, offering a basic IDE environment.
• Visual Studio Code: A versatile and customizable IDE with extensive Python
support.
• PyCharm: A powerful IDE specifically designed for Python development.
Required Resources
• A modern Personal computer with internet access.
• Windows 10/11 or Linux or macOS or VirtualBox or WSL or Colab (as learnt in
Lab 0) or else.
Lab Work
Python programming
Creating Functions
You have already used a few functions, such as the print command but Python enables
you to define your own functions. A function is a command that you enter into Python
to do something. It is a little piece of self-contained code that takes data, works on it and
then returns the result. A function is group of statements that exist within a program for
the purpose of performing a specific task.
1. It is not just data that a function works on. They can do all manner of useful things
in Python, such as short data, change items from one format to another and check
the length or type of items. A function is a short word that is followed by brackets.
For example, len(), list() or type().
2. A function takes data, usually a variable, works on it depending on what the
function is programmed to do and returns the end value. The data being worked on
goes inside the brackets, so if you wanted to know how many letters are in the word
otorhinolaryngological, then you would enter: len("otorhinolaryngological") and the
number 22 would return.
3. You can pass variables through functions in much the same manner. Let us assume
you want the number of letters in a person’s surname, you could use the following
code (enter the text editor for this example):
name=input ("Enter your surname: ")
count=len(name)
print("Your name has", count, "letters in it. ")
12
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
13
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
11. What you have just done is import the Hello function from the saved Hello.py
program and then use it to say hello to Alex. This is how modules and functions
work: you import the module then use a function. Try this one and modify it for
extra credit:
def add(a, b):
result = a + b
return result
Conditions and Loops
Conditions and loops are what make a program interesting; they can be simple or rather
complex. How you use them depends greatly on what the program is trying to achieve;
they could be the number of lives left in a game or just displaying a countdown.
Keeping conditions simple to begin with makes learning to program a more enjoyable
experience. Let us start then by checking if something is TRUE, then doing something
else if it is not.
12. Let us create a new Python program that will ask the user to input a word, then
check it to see if it is a four-letter word or not. Start with File > New File, and begin
with the input variable:
word=input("Please enter a four-letter word: ")
13. Now you can create a new variable, then use the len function and pass the word
variable through it to get the total number of letters the user has just entered.
word_length=len(word)
14. Now you can use an if statement to check if the word-length variable is equal to four
and print a friendly conformation if it applies to the rule:
if word_length==4:
print(word, "is a four-letter word. Well done.")
The double equals sign (==) means check if something is equal to something else.
15. The colon at the end of IF tells Python that if this statement is true do everything
after the colon that is intended. Next, move the cursor back to the beginning of the
Editor:
word=input("Please enter a four-letter word: ")
word_length=len(word)
if word_length==4:
print(word, "is a four-letter word. Well done.")
else:
print(word, "is not a four-letter word.")
16. Press F5 and save the code to execute it. Enter a four-letter word in the Shell to
begin with, you should have the returned message that it's the word is four-letters.
Now press F5 again and return the program but this time enter a five-letter word.
The Shell will display that it's not a four-letter word.
17. Now expand the code to include another condition. Eventually, it could become
quite complex. A condition for three-letter words is added:
14
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
15
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Python Modules
We have mentioned modules previously, (the Math module) but as modules are such a
large part of getting the most from Python, it's worth dedicating a little more time to
them. In this instance you are using the Windows versions of Python 3. Thing of
modules as an extension that’s imported into your Python code to enhance the extent its
capabilities. There are countless modules available and as you have seen, you can even
make your own.
24. Although good, the build-in functions within Python are limited. The use of
modules, however, allows us to make more sophisticated programs. As you are
aware, modules are Python scripts that are imported, such as import math.
25. Some modules, especially on the Raspberry Pi, are included by default, the Math
module being a prime example. Sadly, other modules aren't always available. A
good example on non-Pi platforms is the Pygame module, which contains many
functions to help create games. Try: import pygame
The result is an error in the IDLE Shell, as the Pygame module isn’t recognised or
installed in the Python. To install a module you can use PIP (Pip installs packages).
Close down the IDLE Shell and drop into a command prompt or Terminal session.
At the elevated admin command prompt, enter:
pip install pygame
26. The PIP Installation requires an elevated status due its installing components at
different locations. Windows users can search for cmd via the Start button and right-
click the result then click Run as administrator. Linux and Mac users can use the
sudo command, with sudo pip install package.
27. Close the command prompt or Terminal and relaunch the IDLE Shell. When you
now enter: import pygame, the module will be imported into the code without any
problems. You will find that most code downloaded or copied from the Internet will
contain a module, mainstream of unique, these are usually the source of errors in
execution due to them being missing.
16
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
28. The modules contain the extra code needed to achieve a certain result within your
own code, as you have previously experimented with. For example: import random
brings in the code from the Random Number Generator module. You can then use
this module to create something like:
for i in range(10):
print(random.randint(1, 25))
This code, when saved and executed, would display ten random numbers from 1 to
25. You can play around with the code to display more or less, and from a great or
lesser range. For example:
import random
for i in range(25):
print(random.randint(1, 100))
29. Multiple modules can be imported within your code. To extend our example use:
import random
import math
for i in range(5):
print(random.randint(1, 25))
print(math.pi)
30. The result is a string of random numbers followed by the value of Pi as pulled from
the Math module using the print(math.pi) function. You can also pull in certain
functions from a module by using the from and import commands, such as:
from random import randint
for i in range(5):
print(randint(1, 25))
31. This helps create a more streamlined approach to programming. You can also use
import module*, which will import everything defined within the named module.
However, it’s often regarded as a waste of resources but it works nonetheless.
Finally, modules can be imported as aliases:
import math as m
print(m.pi)
Of course, adding comments helps to tell others what's going on.
17
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Python Errors
It goes without saying that you will eventually come across an error in your code, where
Python declares it's not able to continue due to something being missed out, wrong or
simply unknown. Being able to identify these errors makes for a good programmer.
Debugging - Errors in code are called bugs and are perfectly normal. They can often be
easily rectified with a little patience. The important thing is to keep looking,
experimenting and testing. Eventually your code will be bug free.
32. Code isn’t as fluid as the written word, no matter how good the programming
language is. Python is certainly easier than most languages but even it is prone to
some annoying bugs. The most common are typos by the user and whilst easy to
find in simple dozen-line code, imagine having to debug multi-thousand-line code.
The most common with errors is the typo, as we have mentioned. The typos are
often at the command level: mistyping the print command for example. However,
they also occur when you have numerous variables, all of which have lengthy
names. The best advice is to simply go through the code and check your spelling.
33. Thankfully Python is helpful when it comes to displaying error messages. When you
receive an error, in read text, from the IDLE shell, it will define the error itself along
with the line number where the error has occurred. Whilst in the IDLE Editor this is
a little daunting for lots of code; text editors help by including line numbering.
Syntax errors are probably the second most common errors you will come across as
a programmer. Even if the spelling is correct, the actual command itself is wrong. In
Python 3 this often occurs when Python 2 syntaxes are applied. The most annoying
of this is the print function. In Python 3 you use print("words"), whereas Python 2
use print "words".
34. Pesky brackets are also a nuisance in programming errors, especially when you have
something like: print(balanced_check(input()))
Remember that for every '(' there must be an equal number of ')'.
35. There are thousands of online Python resources, code snippets and lengthy
discussions across forums on how best to achieve something. Whilst 99% of it is
good code, don't always be lured into copying and pasting random code into your
editor. More often than not, it won't work and the worst part is that you haven't
learnt anything.
Intents are a nasty part of Python programming that a lot of beginners fall foul of.
Recall the if loop from the Conditions and Loops section, where the colon means
everything intended following the statement is to be executed as long as it is true?
Missing the indent, or having too much of indent, will come back with an error.
36. An excellent way to check your code step-by-step is to use Python’s Tutor’s
Visualise webpage, found at www.pythontutor.com/visualize.html#mode=edit.
Simply paste your code into the editor and click the Visualise Execution button to
run the code line-by-line. This helps to clear bugs and any misunderstandings.
18
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
37. Planning makes for good code. Whilst a little old school, it's a good habit to plan
what your code will do before sitting down to type it out. List the variables that will
be used and the modules too; then write out a script for any user interaction or
outputs.
Purely out of interest, the word debugging in computing terms comes from Admiral
Grace Hopper, who back in the ‘40s was working on a monolithic Harvard Mark II
electromechanical computer. According to legend Hopper found a moth stuck in a
relay, thus stopping the system from working. Removal of the moth was hence
called debugging.
19
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5))
pi = 1/pi
return str(pi)
41. The previous step defines the rules that make up the algorithm and creates the string
but will eventually display the value of Pi, according to the Chudnovsky brothers’
algorithm. You have no doubt already surmised that it will be handy to actually
output the value of Pi to the screen. To rectify that you can add: print(calc(1))
42. You can save and execute the code at this point if you like. The output will print the
value of Pi to 27 decimal places: 3.14159… Whilst pretty impression on its own,
you want some user interaction, to ask the user as to how many places Pi should be
calculated.
43. You can insert an input line before the Pi calculation Def command. It needs to be
an integer, as it will otherwise default to a string. You can call it numberofdigits and
use the getcontext function:
numberofdigits = int(input("Please enter the number of decimal place to
calculate Pi to: "))
getcontext().prec = numberofdigits
44. You can execute the code now and it asks the user how many decimal places they
want to calculate Pi to, outputting the result in the IDLE Shell. Try it with 1000
places but don't go too high or else your computer will be locked up in calculating
Pi. Part of programming is being able to modify code, making it more presentable.
Let's include an element that times how long it takes our computer to calculate the
Pi decimal places and present the information in a different colour. For this, drop
into the command line and import the Colorama module (RPi users already have it
installed): pip install colorama
45. To finish your code, you need to initialize the Colorama module and start the time
function at the point where the calculation starts, and when it finishes. The end
result is a coloured ink displaying how long the process took (in the Terminal or
command line):
from decimal import Decimal, getcontext
import math
import time
import colorama
from colorama import Fore
20
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
colorama.init()
start_time = time.time()
def calc(n):
46. Now you need to import the Colorama module (which will output text in different
colours) along with the Fore function (which dictates the foreground, ink, colour)
and the Time module to start a virtual stopwatch to see how long your calculations
take:
import time
import colorama
from colorama import Fore
…
print(Fore.RED + "\nTime taken:", time.time() - start_time)
47. In case you face some difficulties executing the program in Windows 10, try the
Linux version of Python 3. To install Colorama module in Linux, type:
sudo apt-get install python-colorama
A sample execution of the program is given below:
21
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Code:
colorama.init()
numberofdigits = int(input("Please enter the number of
decimal place to calculate Pi to: "))
getcontext().prec = numberofdigits
start_time = time.time()
def calc(n):
t = Decimal(0)
pi = Decimal(0)
deno = Decimal(0)
k = 0
for k in range(n):
t = (Decimal(-
1)**k)*(math.factorial(6*k))*(13591409 + 545140134*k)
deno =
math.factorial(3*k)*(math.factorial(k)**3)*(640320**(3*Deci
mal(k)))
pi += Decimal(t)/Decimal(deno)
pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5))
pi = 1/pi
print(Fore.RED + "\nTime taken:", time.time() -
start_time)
return str(pi)
print(calc(2))
22
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
Postlab Exercise
Automating Everyday Events
Learning Objectives
Imagine daily activities that would make your life easier if they were automated.
Background / Scenario
Many activities performed daily are redundant and have the potential of being
automated with advances in technology. Use your imagination or search videos to help
you think of activities that could be made more useful if they contained intelligent
sensors.
Required Resources
• None
Step 1: Activities to Automate
Think about your day in your home, school, or office. Are there any activities that you
think would save you time if they were automated? As an example, if the smart
refrigerator in your home had sensors to detect weight, it could determine that the milk
carton was getting low and automatically order milk that is delivered to your home. Use
the table below to list at least 5 activities and how each particular activity could be
automated. If you use any videos to spark ideas for automation. List the name of the
video and main URL after the table and potentially share with group members.
1. List the activities that you selected and how the activity might be automated.
23
Lab Workshop/Lab 3 Embedded Systems and the IoT EEE_6_ESI
24