Reader small image

You're reading from  Advanced Analytics with R and Tableau

Product typeBook
Published inAug 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786460110
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Ruben Oliva Ramos
Ruben Oliva Ramos
author image
Ruben Oliva Ramos

Ruben Oliva Ramos is a computer systems engineer from Tecnologico de Leon Institute, with a master's degree in computer and electronic systems engineering and a specialization in teleinformatics and networking from the University of Salle Bajio in Leon, Guanajuato, Mexico. He has more than 5 years of experience of developing web applications to control and monitor devices connected with Arduino and Raspberry Pi, using web frameworks and cloud services to build the Internet of Things applications. He is a mechatronics teacher at the University of Salle Bajio and teaches students of the master's degree in design and engineering of mechatronics systems. Ruben also works at Centro de Bachillerato Tecnologico Industrial 225 teaching subjects such as electronics, robotics and control, automation, and microcontrollers. He is a consultant and developer for projects in areas such as monitoring systems and datalogger data using technologies (such as Android, iOS, HTML5, and ASP.NET), databases (such as SQlite, MongoDB, and MySQL), web servers, hardware programming, and control and monitor systems for data acquisition and programming.
Read more about Ruben Oliva Ramos

Jen Stirrup
Jen Stirrup
author image
Jen Stirrup

Jen Stirrup is a data strategist and technologist, a Microsoft Most Valuable Professional (MVP), and a Microsoft Regional Director, a tech community advocate, a public speaker and blogger, a published author, and a keynote speaker. Jen is the founder of a boutique consultancy based in the UK, Data Relish, which focuses on delivering successful business intelligence and artificial intelligence solutions that add real value to customers worldwide. She has featured on the BBC as a guest expert on topics relating to data.
Read more about Jen Stirrup

View More author details
Right arrow

Chapter 8. Interpreting Your Results for Your Audience

If we feel a cold, then we use a jacket. When we are hungry, we decide to eat. These decisions can be made by us, but how does a machine make a decision? In this chapter, we will learn how to build a decision system that can be implemented on IoT devices. All of these systems can analyze with all the chapters seen in this book. The main idea of this chapter is to use the analytics algorithms, and to learn how to apply them in IoT projects

We will explore the following topics:

  • Introduction to decision system and machine learning

  • Building a simple decision system-based Bayesian theory

  • Integrating a decision system and IoT project

  • Building your own decision system-based IoT

Introduction to decision system and machine learning


A decision system that makes a decision based on several input parameters. A decision system is built on decision theories. Being human involves making decisions for almost all life cases.

The following are examples of decisions that humans make:

  • Shall I buy the car today? The decision depends on my preferences. This car looks fine, but it is too expensive for me.

  • Shall I bring an umbrella today? This decision depends on the current condition in the area where we are staying. If it is cloudy, it's better to bring an umbrella even though it may not rain.

Generally speaking, we teach a machine such as a computer in order to understand and achieve a specific goal. This case is called machine learning. Varieties of programs are implemented in machines so they can make decisions. Machine learning consists of using various algorithms to build a decision system. In this book, I use fuzzy logic and Bayesian algorithms to make a decision system. I...

Decision system-based Bayesian


Bayesian uses the manipulation of conditional probabilities approach to interpret data. In this section, we build a decision system using the Bayesian method. Consider D, called the decision space, which denotes the space of all possible decisions d that could be chosen by the decision maker (DM). Θ is the space of all possible outcomes or state of nature ω, ω Θ.

Decision system-based Bayesian is built by Bayesian theory. For illustration, I show a simple spam filter using Bayesian. Imagine the sample space X is the set of all possible datasets of words, from which a single dataset word x will result. For each ω Θ and x X, the sampling model P(ω) describes a belief that x would be the outcome of spam probability. P(x|ω), prior to distribution, is the true population characteristics and supposes a spam probability for x.P(ω|x), posterior distribution, describes a belief that ω is the true value of spam, having observed dataset x.

The posterior distribution...

Bayesian Theory


We can implement Bayesian probability using Python. For our demo, we generate output values from two independent variables, x1 and x2. The output model is defined as follows:

c is a random value. We define α, β1, β2, and σ as 0.5, 1, 2.5, and 0.5.

These independent variables are generated using a random object from the NumPy library. After that, we compute the model with these variables.

We can implement this case with the following scripts:

import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
# initialization
np.random.seed(100)
alpha, sigma = 0.5, 0.5
beta = [1, 2.5]
size = 100

# Predictor variable
X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.37
# Simulate outcome variable
Y = alpha + beta[0]*X1 + beta[1]*X2 + np.random.randn(size)*sigma
fig, ax = plt.subplots(1, 2, sharex=True, figsize=(10, 4))
fig.subplots_adjust(bottom=0.15, left=0.1)
ax[0].scatter(X1, Y)
ax[1].scatter(X2, Y)
ax[0].set_ylabel('Y')
ax[0].set_xlabel('X1...

Fuzzy logic


One of the famous Python libraries for fuzzy logic is scikit-fuzzy. Several fuzzy logic algorithms have already been implemented on this library. Since scikit-fuzzy is an open source library, you can review the source code at https://github.com/scikit-fuzzy/scikit-fuzzy.

Before you install this library, you should already have installed NumPy and SciPy libraries. You can install scikit-fuzzy using pip, by typing the following command:

$ sudo pip install scikit
-fuzzy

As another option, you can install the scikit-fuzzy library from source code.

Type these commands:

$ git clone https://github.com/scikit-fuzzy/scikit-fuzzy
$ cd scikit-fuzzy/
$ sudo python setup.py install

After completing the installation, you can use scikit-fuzzy. To test how to work with scikit-fuzzy, we will build a fuzzy membership for temperature using the fuzz.trimf() function. You can write the following scripts:

import matplotlib
matplotlib.use('Agg')

import numpy as np
import skfuzzy as fuzz
import matplotlib...

Building a simple decision system-based Bayesian theory


In this section, we build a simple decision system using Bayesian theory. A smart water system is a smart system that controls water. In general, you can see the system architecture in the following figure:

After using a sensing process on water to obtain the water quality, you can make a decision. If the water quality is good, we can transfer the water to customers. Otherwise, we purify the water.

To implement a decision system-based Bayesian theory, firstly we define the state of nature. In this case, we define two states of nature:

  • ω1: Water is ready for drinking

  • ω2: Water should be cleaned (kotor)

For inputs, we can declare x1 and x2 as negative and positive as the observation results. We define prior values and class conditional probabilities as follows:

To build a decision, we should make a loss function. The following is a loss function for our program:

Integrating a decision system and IoT project


IoT boards help us to perform sensing and actuating. To build a decision system with IoT boards, we can use a sensing process on IoT boards as input parameters for our decision system. After performing decision computing, we can make some actions through actuating on IoT boards.

In general, we can integrate our decision system with IoT boards, as shown in the following figure:

Several sensor devices can be attached to the IoT board that is used for sensing. Depending on what you need; you can gather environmental data, such as temperature, as digital inputs that will be used for our decision system. You can see samples of sensor devices in the following figure:

Several actuator devices can be used in our decision system. Each final output from a system can be mapped into an action. This action can be represented as turning on an actuator device.

Some systems may not do sensing on their environment to gather input data. We can obtain data from a database...

Building your own decision system-based IoT


In this section, we build a simple decision system using fuzzy logic on Raspberry Pi. We use Python for implementation. We build a system to monitor temperature and humidity in a room to decide if the environment is comfortable or not. If the environment is not comfortable, then we turn on a cooler machine.

The following is our design system:

Wiring

We use DHT22 and relay modules for our wiring. Connect the DHT22 module into the following connections:

  • DHT22 pin 1 (VDD) is connected to the 3.3V pin from Raspberry Pi

  • DHT22 pin 2 (SIG) is connected to the GPIO23 (see the BCM column) pin from Raspberry Pi

  • DHT22 pin 4 (GND) is connected to the GND pin from Raspberry Pi

  • A relay VCC is connected to the 3.3V pin from Raspberry Pi

  • A relay GND is connected to the GND pin from Raspberry Pi

  • A relay signal is connected to the GPIO26 (see the BCM column) pin from Raspberry Pi

The complete wiring is shown in the following figure:

Writing the program

We build a fuzzy logic...

Summary


We have reviewed some basic decision systems by taking two samples, that is, Bayesian and fuzzy logic. We also explored Python libraries for implementing Bayesian and fuzzy logic and then practiced with them. As the last topic, we deployed a decision system using fuzzy logic as a study sample on how to integrate a decision system on an IoT project with Raspberry Pi.

References


The following is a list of recommended books where you can learn more about the topics in this chapter:

  • Introduction to Machine Learning, Ethem Alpaydin, The MIT Press, 2004.

  • A First Course in Bayesian Statistical Methods, Peter D. Hoff. Springer, New York, 2009.

  • Bayes' Rule: A Tutorial Introduction to Bayesian Analysis, James V Stone. Sebte Press. 2013.

  • Bayesian risk management: A guide to model risk and sequential learning in financial markets, Matt Sekerke. Wiley & Sons. 2015.

  • Fuzzy logic with engineering applications, 3rd Edition, Timothy J. Ross, John Wiley & Sons. 2010.

  • A First Course in Fuzzy Logic, 3rd Edition, Hung T. Nguyen and Elbert A. Walker, CRC Press. 2006.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Advanced Analytics with R and Tableau
Published in: Aug 2017Publisher: PacktISBN-13: 9781786460110
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Ruben Oliva Ramos

Ruben Oliva Ramos is a computer systems engineer from Tecnologico de Leon Institute, with a master's degree in computer and electronic systems engineering and a specialization in teleinformatics and networking from the University of Salle Bajio in Leon, Guanajuato, Mexico. He has more than 5 years of experience of developing web applications to control and monitor devices connected with Arduino and Raspberry Pi, using web frameworks and cloud services to build the Internet of Things applications. He is a mechatronics teacher at the University of Salle Bajio and teaches students of the master's degree in design and engineering of mechatronics systems. Ruben also works at Centro de Bachillerato Tecnologico Industrial 225 teaching subjects such as electronics, robotics and control, automation, and microcontrollers. He is a consultant and developer for projects in areas such as monitoring systems and datalogger data using technologies (such as Android, iOS, HTML5, and ASP.NET), databases (such as SQlite, MongoDB, and MySQL), web servers, hardware programming, and control and monitor systems for data acquisition and programming.
Read more about Ruben Oliva Ramos

author image
Jen Stirrup

Jen Stirrup is a data strategist and technologist, a Microsoft Most Valuable Professional (MVP), and a Microsoft Regional Director, a tech community advocate, a public speaker and blogger, a published author, and a keynote speaker. Jen is the founder of a boutique consultancy based in the UK, Data Relish, which focuses on delivering successful business intelligence and artificial intelligence solutions that add real value to customers worldwide. She has featured on the BBC as a guest expert on topics relating to data.
Read more about Jen Stirrup