SlideShare a Scribd company logo
@DTAIEB55
Data Science Bootcamp at ODSC Europe 2017
Analyze Data and Build a Dashboard with PixieDust
London, October 2017
David Taieb
Distinguished Engineer
Developer Advocacy
IBM Watson Data Platform
©2017 IBM Corporation
@DTAIEB55
• How to use Jupyter Notebooks to load, visualize and
analyze data
• IBM Data Science Experience (DSX)
• PixieDust open source Python Library
• How to build a dashboard using PixieApps
• U.K Open Data
• Mapbox GL
What you will learn in this Workshop
©2017 IBM Corporation
@DTAIEB55
• The tutorial can be followed from a local Jupyter Notebook
environment. However, the instructions and screenshots here
walk through the notebook in the DSX environment.
• A corresponding notebook is available here:
https://github.com/DTAIEB/demos/blob/master/tutorials/ODS
C%20London%20UK%2BTraffic%2Banalysis.ipynb
For best results, use the latest version of either Mozilla Firefox or
Google Chrome.
Info before we start
©2017 IBM Corporation
@DTAIEB55
DSX is an interactive, collaborative, cloud-based environment
where data scientists, developers, and others interested in data
science can use tools (e.g., RStudio, Jupyter Notebooks, Spark,
etc.) to collaborate, share, and gather insight from their data
DSX
©2017 IBM Corporation
@DTAIEB55
Sign Up
• DSX is powered by IBM Bluemix, therefore your DSX login is same as your IBM
Bluemix login. If you already have a Bluemix account or previously accessed DSX
you may proceed to the Sign In section. Otherwise, you first need to sign up for
an account.
• From your browser:
1. Go to the DSX site: http://datascience.ibm.com
2. Click on Sign Up
3. Enter your Email
4. Click Continue
5. Fill out the form to register for IBM Bluemix
©2017 IBM Corporation
@DTAIEB55
From your browser:
1. Go to the DSX site: http://datascience.ibm.com
2. Click on Sign In
3. Enter your IBMid or email
4. Click Continue
5. Enter your Password
6. Click Sign In
Sign In
©2017 IBM Corporation
@DTAIEB55
You will need to create a noteboook to experiment with the data and a project to house your notebook.
After signing into DSX:
1. On the upper right of the DSX site, click the + and choose Create project.
2. Enter a Name for your project
3. Select a Spark Service
4. Click Create
From within the new project, you will create your notebook:
1. Click add notebooks
2. Click the Blank tab in the Create Notebook form
3. Enter a Name for the notebook
4. Select Python 2 for the Language
5. Select 1.6 for the Spark version
6. Select the Spark Service
7. Click Create Notebook
Create a blank new notebook
©2017 IBM Corporation
@DTAIEB55
You can also import the complete notebook
1. Click add notebooks
2. Click the From URL tab in the Create
Notebook form
3. Enter a Name for the notebook
4. In the Notebook URL enter:
https://github.com/DTAIEB/demos/ra
w/master/tutorials/ODSC%20London
%20UK%2BTraffic%2Banalysis.ipynb
5. Click Create Notebook
Skip this page if you prefer to start from a blank notebook.
©2017 IBM Corporation
@DTAIEB55
• When you use a notebook in DSX, you can run a cell only by selecting
it, then going to the toolbar and clicking on the Run Cell (▸) button.
When a cell is running, an [*] is shown beside the cell. Once the cell
has finished the asterisks is replaced by a number.
• If you don’t see the Jupyter toolbar showing the Run Cell (▸) button
and other notebook controls, you are not in edit mode. Go to the
dark blue toolbar above the notebook and click the edit (pencil) icon.
Make sure your Notebook is in Edit Mode
©2017 IBM Corporation
@DTAIEB55
Edit Mode
©2017 IBM Corporation
@DTAIEB55
PixieDust
You can find detailed info on PixieDust here: http://ibm.biz/wdp-pixiedust
In this section, we’ll show how to use the PixieDust Open Source Python library within
a Notebook to:
©2017 IBM Corporation
@DTAIEB55
Make sure PixieDust is at the latest version
• DSX already comes with the PixieDust library installed, but it is always a good idea to
make sure you have the latest version
• In the first cell of the notebook enter: !pip install --upgrade pixiedust
• Click on the Run Cell (▸) button
• After the cell completes, if instructed to restart the kernel, from the notebook toolbar
menu:
• Go to > Kernel > Restart
• Click Restart in the confirmation dialog
Note: The status of the kernel briefly flashes near the upper right corner, alerting when it
is Not Connected, Restarting, Ready, etc.
©2017 IBM Corporation
@DTAIEB55
Import PixieDust
• Before, you can use the PixieDust library it must be imported into the notebook
• In the next cell enter:
import pixiedust
• Click on the Run Cell (▸) button
Note: Whenever the kernel is restarted, the import pixiedust cell must be run before continuing.
PixieDust has been updated and imported, you are now ready to play with your data!
©2017 IBM Corporation
@DTAIEB55
Import U.K traffic accidents data
It would be a good idea to spend a few minutes browsing the San Francisco open data
site: https://data.gov.uk
• Load the data: With PixieDust, you can easily load CSV data from a URL into a PySpark
DataFrame in the notebook.
• In a new cell, enter:
accidents =
pixiedust.sampleData("http://data.dft.gov.uk/road-accidents-
safety-data/dftRoadSafetyData_Accidents_2016.zip")
©2017 IBM Corporation
@DTAIEB55
Explore the Data
In the next few cells, we’ll use the PixieDust display() to interactively explore the data. This tutorial will
provide a few suggestions for which options to choose, but feel free to also explore on your own.
For example:
(Pie Chart - Options: Keys = PdDistrict, Values = IncidntNum, Aggregation = Count)
©2017 IBM Corporation
@DTAIEB55
Initial Exploration
Let’s start exploring our data by answering the following questions
• In which police district do the most traffic accidents occur?
(Pie Chart - Options: Keys = PdDistrict, Values = IncidntNum, Aggregation = Count)
• We can also dig one level deeper by clustering by how each accident was resolved:
(Cluster By: Resolution)
• On what day of the week do the most traffic accidents occur?
(Bar Chart - Options: Keys = DayOfWeek, Values = IncidntNum, Aggregation = Count)
©2017 IBM Corporation
@DTAIEB55
Results
Menu to select type of viz to display
Chart
Options
Dialog
Chart
Rendering area
Dynamic Options
(depend on the type
of chart and
renderer)
Renderer
Selector
©2017 IBM Corporation
@DTAIEB55
More data exploration and hypothesis
• Immediately, we can identify a couple of areas of interest in our data without
having to write a single line of code:
1. Most accidents happen in the Southern and Taraval police districts
2. Most accidents happen on Wednesdays and Thursdays.
• We can also see that our data needs some cleansing if we want to make
analysis easier:
1. The Time field needs to be converted into its time components
2. Rename the DayOfWeek values so they are rendered in alphabetical
order
• We should condense the outcome types of each traffic accident if we want to
see the most common resolutions of traffic accidents in each police district,
since the clustering above was unclear.
©2017 IBM Corporation
@DTAIEB55
Cleansing the data
©2017 IBM Corporation
@DTAIEB55
More exploration on the cleansed data
• Hypothesis: Do accidents in one police district result in more arrests than
other police districts?
(Bar Chart - Options: Keys = PdDistrict, Values = IncidntNum, Aggregation =
Count, Cluster By: Res)
• Question: How does the number of accidents change over the course of
the week?
(Line Chart - Options: Keys = DayOfWeek, Values = IncidntNum, Aggregation
= Count)
©2017 IBM Corporation
@DTAIEB55
Some more results
©2017 IBM Corporation
@DTAIEB55
What have we learned
• A few lines of code makes it a lot easier to see that:
1. Accidents in the Richmond police district are much more
likely to result in arrest than all other districts
2. The number of accidents peaks during the middle of the
week, but decreases afterwards as the week winds down.
Now let's focus on the Taraval police district using some friendly
SQL notation
©2017 IBM Corporation
@DTAIEB55
Let’s use Spark SQL to focus on TARAVAL
• In a new cell, enter the following code to create a temporary SQL Table so we
can then use regular a SQL query to select the accidents from TARAVAL.
• The results are then stored in a new Spark DataFrame
accidents.registerTempTable("accidents")
taraval = sqlContext.sql("SELECT * FROM accidents WHERE
PdDistrict='TARAVAL'")
©2017 IBM Corporation
@DTAIEB55
Exploration on TARAVAL accidents
• Question: Where in Taraval do most accidents happen?
(Map - Options: Keys = [X,Y], Values = IncidntNum, Aggregation = Count,
Renderer: mapbox, kind: chloropleth-cluster)
• Question: What time of day do most accidents occur?
(Line Chart - Options: Keys = Hour, Values = IncidntNum, Aggregation = Count)
Note: The Notebook comes with a public Mapbox access token. You can also create
your own free Mapbox access token at the following url:
https://www.mapbox.com/help/create-api-access-token/
©2017 IBM Corporation
@DTAIEB55
Map Visualization
©2017 IBM Corporation
@DTAIEB55
Let’s recap
Most of the results from looking at the accident times are unsurprising:
• Less accidents during very early morning (people probably sleeping)
• Steady increase in number of accidents during morning commuting hours
• Less accidents during mid-evening (people probably eating dinner)
• (Sadly) more accidents late at night.
The interesting thing here is the sudden spike in accidents during mid-afternoon (2-
3PM) - twice as many accidents happen during this two-hour window!
In the next section, we’ll bring more data to find more insights on the data. But
instead of writing more code in linear cells, we’ll build a dashboard using another
PixieDust feature called PixieApps
©2017 IBM Corporation
@DTAIEB55
What are PixieApps
• PixieApps are Python classes that let you write UI for your analytics
• Easy to build: mostly HTML and CSS with some custom attributes
• Leverage PixieDust Display visualization for charting
• With PixieApps you can:
• Create different html views with routes to invoke them
• Invoke Python Scripts from user interactions
• Run in the notebook cell output or in a Dialog
• and much more…
• Use cases:
• Dashboards
• Data Browsers
• Data Pipeline Management
Note: Please take the time to browse the PixieApp documentation page here:
https://ibm-cds-labs.github.io/pixiedust/pixieapps.html
©2017 IBM Corporation
@DTAIEB55
Import app package to start things off
Simple annotation to tell PixieDust it’s an app
Define the default route (no args).
Method will return the view’s html fragment
Html fragment for the view.
Allows Jinja2 template macros
Define a new route that triggers when
option clicked is set to true
set option clicked to true when button is
pushed, so that correct route is loaded next
Import app package to start things off
PixieApp Hello Word
©2017 IBM Corporation
@DTAIEB55
Placeholder div for displaying data
Display the output in the specified target
Entity binding: use the df passed by user
Allows binding of any entity created by the app
Specify Display options for visualization
Pass data to the app
PixieApp HelloWorld with Data
©2017 IBM Corporation
@DTAIEB55
Back to our U.K Data
• In analyzing the geographical data, we can see a couple of clusters where
accidents occur more frequently in Taraval - the southeastern corner
looks particularly crowded.
• Some useful questions to ask at this point are:
• Does crime has an effect on the number of accidents?
• Are there more accidents in these areas because more people speed
there
• Do traffic calming devices reduce the number of accidents?
Note: In the next section, we’ll download datasets from the San Francisco
Open Data Web site: https://data.gov.uk
©2017 IBM Corporation
@DTAIEB55
PixieApp Dashboard
Step 1: Create the skeleton
from pixiedust.display.app import *
@PixieApp
class UKDashboard():
def mainScreen(self):
return """
<div class="well">
<center><span style="font-size:x-large">Analyzing U.K Public Safety data with
PixieDust</span></center>
<center><span style="font-size:large"><a href="https://data.gov.uk"
target="new">https://data.gov.uk</a></span></center>
</div>
<div class="row">
<div class="form-group col-sm-2" style="padding-right:10px;">
<div><strong>Layers</strong></div>
</div>
<div class="form-group col-sm-10">
<div id="map{{prefix}}”/>
</div>
</div>
“””
©2017 IBM Corporation
@DTAIEB55
PixieApp Dashboard
Step 2: Create the map of accidents
from pixiedust.display.app import *
@PixieApp
class SFDashboard():
def mainScreen(self):
return """
<div class="well">
<center><span style="font-size:x-large">Analyzing U.K data with PixieDust</span></center>
<center><span style="font-size:large"><a href="https://data.gov.uk"
target="new">https://data.gov.uk</a></span></center>
</div>
<div class="row">
<div class="form-group col-sm-2" style="padding-right:10px;">
<div><strong>Layers</strong></div>
</div>
<div class="form-group col-sm-10">
<div id="map{{prefix}}”>
<pd_options>
{
"handlerId": "mapView", ”mapboxtoken":"pk.eyJ1Ijoic…","timeseries": "false",
"keyFields": "Longitude,Latitude","aggregation": "COUNT","kind": "choropleth-cluster",
"valueFields": "Road_Surface_Conditions","basemap": "light-v9","rendererId": "mapbox"
}
</pd_options>
</div>
</div>
</div>
“””
• What have we added:
• pd_entity: tell PixieDust which dataset to work on
• pd_options: Contains the PixieDust options for the map (see next slide for more info)
©2017 IBM Corporation
@DTAIEB55
Generating the pd_options for the Map
The best way to generate the pd_options for a PixieDust visualization is to:
1. Call display() on a new cell
2. Graphically select the options for your chart
3. Select View/Cell Toobar/Edit metadata menu
4. Click on the “Edit Metadata” button and copy the pixiedust metadata
©2017 IBM Corporation
@DTAIEB55
Initialize the Layers
def setup(self):
self.setLayers([…])
Note: setup is a special method that will be called automatically when the PixieApp is initialized.
©2017 IBM Corporation
@DTAIEB55
PixieApp Dashboard
Step 3: Create the GeoJSON customer Layers
from pixiedust.display.app import *
from pixiedust.apps.mapboxBase import MapboxBase
@PixieApp
class SFDashboard(MapboxBase):
def setup(self):
…
self.setLayers([
{
"name": "Police Forces Area",
"url": "http://geoportal1-ons.opendata.arcgis.com/….."
},
{
"name": "Traffic Signals",
"url": "http://data.cyc.opendata.arcgis.com/datasets/…",
"type": "symbol",
"layout": {
"icon-image": "police-15",
"icon-size": 1.5
}
},
…
])
©2017 IBM Corporation
@DTAIEB55
PixieApp Dashboard
Step 4: Create the checkboxes from the layers
from pixiedust.display.app import *
from pixiedust.apps.mapboxBase import MapboxBase
@PixieApp
class UKDashboard(MapboxBase):
…
@route()
def mainScreen(self):
return """
<div class="well">
<center><span style="font-size:x-large">Analyzing U.K Traffic data with PixieDust</span></center>
<center><span style="font-size:large"><a href="https://data.gov.uk/" target="new">https://data.gov.uk</a></span></center>
</div>
<div class="row">
<div class="form-group col-sm-2" style="padding-right:10px;">
<div><strong>Layers</strong></div>
{% for layer in this.layers %}
<div class="rendererOpt checkbox checkbox-primary">
<input type="checkbox" pd_refresh="map{{prefix}}" pd_script="self.toggleLayer({{loop.index0}})">
<label>{{layer["name"]}}</label>
</div>
{%endfor%}
</div>
<div class="form-group col-sm-10">
<div id="map{{prefix}}" pd_entity>
<pd_options>
{"handlerId": "mapView",
”mapboxtoken":"pk.eyJ1IjoicmFqcnNpbmdoIiwiYSI6ImNqM2s4ZDg4djAwcGYyd3BwaGxwaDV3bWoifQ.d5Rklkdu5MeGAnXu1GMNYw",
"timeseries": "false","keyFields": "Longitude,Latitude","aggregation": "COUNT","kind": "choropleth-cluster",
"valueFields": "Road_Surface_Conditions","basemap": "light-v9","rendererId": "mapbox","rowCount": "600"
}
</pd_options>
</div>
</div>
</div>
"""
©2017 IBM Corporation
@DTAIEB55
Run the PixieApp Dashboard
You can find the final code for
the PixieApp in the complete
notebook :
https://github.com/DTAIEB/de
mos/blob/master/tutorials/ODS
C%20London%20UK%2BTraffic
%2Banalysis.ipynb
©2017 IBM Corporation
@DTAIEB55
Final Thoughts
• In this tutorial:
• We showed how PixieDust help notebook users (data scientists or not) to load
and visualize data without having to write code
• We also showed how to use notebooks in new ways thanks to PixieApps by
writing dashboard
• Next Steps
• Improve the analytics by finding new insights on the U.K Traffic accidents
dataset, or perhaps start a new notebook with a new dataset from
https://data.gov.uk
• Improve the PixieApp dashboard by adding new layers. Just find the geojson
link from the opendata site by clicking on the explore button (warning: not all
datasets come with a geojson link). Then just add a new entry in the layers
array

More Related Content

PDF
PixieDust
Margriet Groenendijk
 
PPTX
Data science bootcamp with pixie dust
David Taieb
 
PPTX
Inteligencia artificial, open source e IBM Call for Code
Luciano Resende
 
PDF
Version Control in AI/Machine Learning by Datmo
Nicholas Walsh
 
PPTX
Training on CSPro and step by steps.pptx
lenjisoHussein
 
PDF
DevOps for Data Engineers - Automate Your Data Science Pipeline with Ansible,...
Mihai Criveti
 
PDF
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Databricks
 
PPTX
Building an AI and ML Model Using KNIME and Python.pptx
ssuser448ad3
 
Data science bootcamp with pixie dust
David Taieb
 
Inteligencia artificial, open source e IBM Call for Code
Luciano Resende
 
Version Control in AI/Machine Learning by Datmo
Nicholas Walsh
 
Training on CSPro and step by steps.pptx
lenjisoHussein
 
DevOps for Data Engineers - Automate Your Data Science Pipeline with Ansible,...
Mihai Criveti
 
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Databricks
 
Building an AI and ML Model Using KNIME and Python.pptx
ssuser448ad3
 

Similar to Odsc london data science bootcamp with pixie dust (20)

PDF
IBM Bluemix Nice Meetup #1 - CEEI NCA - 20160630 -
IBM France Lab
 
PDF
Bringing an AI Ecosystem to the Domain Expert and Enterprise AI Developer wit...
Databricks
 
PDF
How to setup MateriApps LIVE!
Computational Materials Science Initiative
 
PDF
How to setup MateriApps LIVE!
Computational Materials Science Initiative
 
PDF
22nd Athens Big Data Meetup - 1st Talk - MLOps Workshop: The Full ML Lifecycl...
Athens Big Data
 
PDF
Fabric for Deep Learning
Animesh Singh
 
PPTX
Workshop using open source software for mobile data collection workshop - a...
Wisconsin Land Information Association
 
PPTX
JEEConf 2017 - In-Memory Data Streams With Hazelcast Jet
Neil Stevenson
 
PDF
Hacking the Kinect with GAFFTA Day 1
benDesigning
 
PDF
TTN things connected acount creation
Jisc
 
PDF
How to setup MateriApps LIVE!
Computational Materials Science Initiative
 
PDF
TensorFlow 16: Building a Data Science Platform
Seldon
 
PDF
Nexxworks bootcamp ML6 (27/09/2017)
Karel Dumon
 
PDF
Glynn Bird – Cloudant – Building applications for success.- NoSQL matters Bar...
NoSQLmatters
 
PDF
INET for Starters
Fayruz Rahma
 
PPTX
Northwestern 20181004 v9
home
 
PDF
Continuum Analytics and Python
Travis Oliphant
 
PPTX
Data platforms 2017
Kellyn Pot'Vin-Gorman
 
PDF
Website & Internet + Performance testing
Roman Ananev
 
PDF
Monitor your application and sleep
Gianluca Arbezzano
 
IBM Bluemix Nice Meetup #1 - CEEI NCA - 20160630 -
IBM France Lab
 
Bringing an AI Ecosystem to the Domain Expert and Enterprise AI Developer wit...
Databricks
 
How to setup MateriApps LIVE!
Computational Materials Science Initiative
 
How to setup MateriApps LIVE!
Computational Materials Science Initiative
 
22nd Athens Big Data Meetup - 1st Talk - MLOps Workshop: The Full ML Lifecycl...
Athens Big Data
 
Fabric for Deep Learning
Animesh Singh
 
Workshop using open source software for mobile data collection workshop - a...
Wisconsin Land Information Association
 
JEEConf 2017 - In-Memory Data Streams With Hazelcast Jet
Neil Stevenson
 
Hacking the Kinect with GAFFTA Day 1
benDesigning
 
TTN things connected acount creation
Jisc
 
How to setup MateriApps LIVE!
Computational Materials Science Initiative
 
TensorFlow 16: Building a Data Science Platform
Seldon
 
Nexxworks bootcamp ML6 (27/09/2017)
Karel Dumon
 
Glynn Bird – Cloudant – Building applications for success.- NoSQL matters Bar...
NoSQLmatters
 
INET for Starters
Fayruz Rahma
 
Northwestern 20181004 v9
home
 
Continuum Analytics and Python
Travis Oliphant
 
Data platforms 2017
Kellyn Pot'Vin-Gorman
 
Website & Internet + Performance testing
Roman Ananev
 
Monitor your application and sleep
Gianluca Arbezzano
 
Ad

Recently uploaded (20)

PDF
Technical Writing Module-I Complete Notes.pdf
VedprakashArya13
 
PDF
Fundamentals and Techniques of Biophysics and Molecular Biology (Pranav Kumar...
RohitKumar868624
 
PPTX
White Blue Simple Modern Enhancing Sales Strategy Presentation_20250724_21093...
RamNeymarjr
 
PDF
D9110.pdfdsfvsdfvsdfvsdfvfvfsvfsvffsdfvsdfvsd
minhn6673
 
PPTX
Introduction to Data Analytics and Data Science
KavithaCIT
 
PPTX
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PPTX
Pipeline Automatic Leak Detection for Water Distribution Systems
Sione Palu
 
PDF
SUMMER INTERNSHIP REPORT[1] (AutoRecovered) (6) (1).pdf
pandeydiksha814
 
PPTX
Data-Users-in-Database-Management-Systems (1).pptx
dharmik832021
 
PDF
Practical Measurement Systems Analysis (Gage R&R) for design
Rob Schubert
 
PPT
From Vision to Reality: The Digital India Revolution
Harsh Bharvadiya
 
PPTX
Future_of_AI_Presentation for everyone.pptx
boranamanju07
 
PPTX
Fuzzy_Membership_Functions_Presentation.pptx
pythoncrazy2024
 
PDF
717629748-Databricks-Certified-Data-Engineer-Professional-Dumps-by-Ball-21-03...
pedelli41
 
PPTX
HSE WEEKLY REPORT for dummies and lazzzzy.pptx
ahmedibrahim691723
 
PPTX
Probability systematic sampling methods.pptx
PrakashRajput19
 
PPTX
Data Security Breach: Immediate Action Plan
varmabhuvan266
 
PPTX
Presentation on animal welfare a good topic
kidscream385
 
PDF
Mastering Financial Analysis Materials.pdf
SalamiAbdullahi
 
PPTX
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
Technical Writing Module-I Complete Notes.pdf
VedprakashArya13
 
Fundamentals and Techniques of Biophysics and Molecular Biology (Pranav Kumar...
RohitKumar868624
 
White Blue Simple Modern Enhancing Sales Strategy Presentation_20250724_21093...
RamNeymarjr
 
D9110.pdfdsfvsdfvsdfvsdfvfvfsvfsvffsdfvsdfvsd
minhn6673
 
Introduction to Data Analytics and Data Science
KavithaCIT
 
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
Pipeline Automatic Leak Detection for Water Distribution Systems
Sione Palu
 
SUMMER INTERNSHIP REPORT[1] (AutoRecovered) (6) (1).pdf
pandeydiksha814
 
Data-Users-in-Database-Management-Systems (1).pptx
dharmik832021
 
Practical Measurement Systems Analysis (Gage R&R) for design
Rob Schubert
 
From Vision to Reality: The Digital India Revolution
Harsh Bharvadiya
 
Future_of_AI_Presentation for everyone.pptx
boranamanju07
 
Fuzzy_Membership_Functions_Presentation.pptx
pythoncrazy2024
 
717629748-Databricks-Certified-Data-Engineer-Professional-Dumps-by-Ball-21-03...
pedelli41
 
HSE WEEKLY REPORT for dummies and lazzzzy.pptx
ahmedibrahim691723
 
Probability systematic sampling methods.pptx
PrakashRajput19
 
Data Security Breach: Immediate Action Plan
varmabhuvan266
 
Presentation on animal welfare a good topic
kidscream385
 
Mastering Financial Analysis Materials.pdf
SalamiAbdullahi
 
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
Ad

Odsc london data science bootcamp with pixie dust

  • 1. @DTAIEB55 Data Science Bootcamp at ODSC Europe 2017 Analyze Data and Build a Dashboard with PixieDust London, October 2017 David Taieb Distinguished Engineer Developer Advocacy IBM Watson Data Platform
  • 2. ©2017 IBM Corporation @DTAIEB55 • How to use Jupyter Notebooks to load, visualize and analyze data • IBM Data Science Experience (DSX) • PixieDust open source Python Library • How to build a dashboard using PixieApps • U.K Open Data • Mapbox GL What you will learn in this Workshop
  • 3. ©2017 IBM Corporation @DTAIEB55 • The tutorial can be followed from a local Jupyter Notebook environment. However, the instructions and screenshots here walk through the notebook in the DSX environment. • A corresponding notebook is available here: https://github.com/DTAIEB/demos/blob/master/tutorials/ODS C%20London%20UK%2BTraffic%2Banalysis.ipynb For best results, use the latest version of either Mozilla Firefox or Google Chrome. Info before we start
  • 4. ©2017 IBM Corporation @DTAIEB55 DSX is an interactive, collaborative, cloud-based environment where data scientists, developers, and others interested in data science can use tools (e.g., RStudio, Jupyter Notebooks, Spark, etc.) to collaborate, share, and gather insight from their data DSX
  • 5. ©2017 IBM Corporation @DTAIEB55 Sign Up • DSX is powered by IBM Bluemix, therefore your DSX login is same as your IBM Bluemix login. If you already have a Bluemix account or previously accessed DSX you may proceed to the Sign In section. Otherwise, you first need to sign up for an account. • From your browser: 1. Go to the DSX site: http://datascience.ibm.com 2. Click on Sign Up 3. Enter your Email 4. Click Continue 5. Fill out the form to register for IBM Bluemix
  • 6. ©2017 IBM Corporation @DTAIEB55 From your browser: 1. Go to the DSX site: http://datascience.ibm.com 2. Click on Sign In 3. Enter your IBMid or email 4. Click Continue 5. Enter your Password 6. Click Sign In Sign In
  • 7. ©2017 IBM Corporation @DTAIEB55 You will need to create a noteboook to experiment with the data and a project to house your notebook. After signing into DSX: 1. On the upper right of the DSX site, click the + and choose Create project. 2. Enter a Name for your project 3. Select a Spark Service 4. Click Create From within the new project, you will create your notebook: 1. Click add notebooks 2. Click the Blank tab in the Create Notebook form 3. Enter a Name for the notebook 4. Select Python 2 for the Language 5. Select 1.6 for the Spark version 6. Select the Spark Service 7. Click Create Notebook Create a blank new notebook
  • 8. ©2017 IBM Corporation @DTAIEB55 You can also import the complete notebook 1. Click add notebooks 2. Click the From URL tab in the Create Notebook form 3. Enter a Name for the notebook 4. In the Notebook URL enter: https://github.com/DTAIEB/demos/ra w/master/tutorials/ODSC%20London %20UK%2BTraffic%2Banalysis.ipynb 5. Click Create Notebook Skip this page if you prefer to start from a blank notebook.
  • 9. ©2017 IBM Corporation @DTAIEB55 • When you use a notebook in DSX, you can run a cell only by selecting it, then going to the toolbar and clicking on the Run Cell (▸) button. When a cell is running, an [*] is shown beside the cell. Once the cell has finished the asterisks is replaced by a number. • If you don’t see the Jupyter toolbar showing the Run Cell (▸) button and other notebook controls, you are not in edit mode. Go to the dark blue toolbar above the notebook and click the edit (pencil) icon. Make sure your Notebook is in Edit Mode
  • 11. ©2017 IBM Corporation @DTAIEB55 PixieDust You can find detailed info on PixieDust here: http://ibm.biz/wdp-pixiedust In this section, we’ll show how to use the PixieDust Open Source Python library within a Notebook to:
  • 12. ©2017 IBM Corporation @DTAIEB55 Make sure PixieDust is at the latest version • DSX already comes with the PixieDust library installed, but it is always a good idea to make sure you have the latest version • In the first cell of the notebook enter: !pip install --upgrade pixiedust • Click on the Run Cell (▸) button • After the cell completes, if instructed to restart the kernel, from the notebook toolbar menu: • Go to > Kernel > Restart • Click Restart in the confirmation dialog Note: The status of the kernel briefly flashes near the upper right corner, alerting when it is Not Connected, Restarting, Ready, etc.
  • 13. ©2017 IBM Corporation @DTAIEB55 Import PixieDust • Before, you can use the PixieDust library it must be imported into the notebook • In the next cell enter: import pixiedust • Click on the Run Cell (▸) button Note: Whenever the kernel is restarted, the import pixiedust cell must be run before continuing. PixieDust has been updated and imported, you are now ready to play with your data!
  • 14. ©2017 IBM Corporation @DTAIEB55 Import U.K traffic accidents data It would be a good idea to spend a few minutes browsing the San Francisco open data site: https://data.gov.uk • Load the data: With PixieDust, you can easily load CSV data from a URL into a PySpark DataFrame in the notebook. • In a new cell, enter: accidents = pixiedust.sampleData("http://data.dft.gov.uk/road-accidents- safety-data/dftRoadSafetyData_Accidents_2016.zip")
  • 15. ©2017 IBM Corporation @DTAIEB55 Explore the Data In the next few cells, we’ll use the PixieDust display() to interactively explore the data. This tutorial will provide a few suggestions for which options to choose, but feel free to also explore on your own. For example: (Pie Chart - Options: Keys = PdDistrict, Values = IncidntNum, Aggregation = Count)
  • 16. ©2017 IBM Corporation @DTAIEB55 Initial Exploration Let’s start exploring our data by answering the following questions • In which police district do the most traffic accidents occur? (Pie Chart - Options: Keys = PdDistrict, Values = IncidntNum, Aggregation = Count) • We can also dig one level deeper by clustering by how each accident was resolved: (Cluster By: Resolution) • On what day of the week do the most traffic accidents occur? (Bar Chart - Options: Keys = DayOfWeek, Values = IncidntNum, Aggregation = Count)
  • 17. ©2017 IBM Corporation @DTAIEB55 Results Menu to select type of viz to display Chart Options Dialog Chart Rendering area Dynamic Options (depend on the type of chart and renderer) Renderer Selector
  • 18. ©2017 IBM Corporation @DTAIEB55 More data exploration and hypothesis • Immediately, we can identify a couple of areas of interest in our data without having to write a single line of code: 1. Most accidents happen in the Southern and Taraval police districts 2. Most accidents happen on Wednesdays and Thursdays. • We can also see that our data needs some cleansing if we want to make analysis easier: 1. The Time field needs to be converted into its time components 2. Rename the DayOfWeek values so they are rendered in alphabetical order • We should condense the outcome types of each traffic accident if we want to see the most common resolutions of traffic accidents in each police district, since the clustering above was unclear.
  • 20. ©2017 IBM Corporation @DTAIEB55 More exploration on the cleansed data • Hypothesis: Do accidents in one police district result in more arrests than other police districts? (Bar Chart - Options: Keys = PdDistrict, Values = IncidntNum, Aggregation = Count, Cluster By: Res) • Question: How does the number of accidents change over the course of the week? (Line Chart - Options: Keys = DayOfWeek, Values = IncidntNum, Aggregation = Count)
  • 22. ©2017 IBM Corporation @DTAIEB55 What have we learned • A few lines of code makes it a lot easier to see that: 1. Accidents in the Richmond police district are much more likely to result in arrest than all other districts 2. The number of accidents peaks during the middle of the week, but decreases afterwards as the week winds down. Now let's focus on the Taraval police district using some friendly SQL notation
  • 23. ©2017 IBM Corporation @DTAIEB55 Let’s use Spark SQL to focus on TARAVAL • In a new cell, enter the following code to create a temporary SQL Table so we can then use regular a SQL query to select the accidents from TARAVAL. • The results are then stored in a new Spark DataFrame accidents.registerTempTable("accidents") taraval = sqlContext.sql("SELECT * FROM accidents WHERE PdDistrict='TARAVAL'")
  • 24. ©2017 IBM Corporation @DTAIEB55 Exploration on TARAVAL accidents • Question: Where in Taraval do most accidents happen? (Map - Options: Keys = [X,Y], Values = IncidntNum, Aggregation = Count, Renderer: mapbox, kind: chloropleth-cluster) • Question: What time of day do most accidents occur? (Line Chart - Options: Keys = Hour, Values = IncidntNum, Aggregation = Count) Note: The Notebook comes with a public Mapbox access token. You can also create your own free Mapbox access token at the following url: https://www.mapbox.com/help/create-api-access-token/
  • 26. ©2017 IBM Corporation @DTAIEB55 Let’s recap Most of the results from looking at the accident times are unsurprising: • Less accidents during very early morning (people probably sleeping) • Steady increase in number of accidents during morning commuting hours • Less accidents during mid-evening (people probably eating dinner) • (Sadly) more accidents late at night. The interesting thing here is the sudden spike in accidents during mid-afternoon (2- 3PM) - twice as many accidents happen during this two-hour window! In the next section, we’ll bring more data to find more insights on the data. But instead of writing more code in linear cells, we’ll build a dashboard using another PixieDust feature called PixieApps
  • 27. ©2017 IBM Corporation @DTAIEB55 What are PixieApps • PixieApps are Python classes that let you write UI for your analytics • Easy to build: mostly HTML and CSS with some custom attributes • Leverage PixieDust Display visualization for charting • With PixieApps you can: • Create different html views with routes to invoke them • Invoke Python Scripts from user interactions • Run in the notebook cell output or in a Dialog • and much more… • Use cases: • Dashboards • Data Browsers • Data Pipeline Management Note: Please take the time to browse the PixieApp documentation page here: https://ibm-cds-labs.github.io/pixiedust/pixieapps.html
  • 28. ©2017 IBM Corporation @DTAIEB55 Import app package to start things off Simple annotation to tell PixieDust it’s an app Define the default route (no args). Method will return the view’s html fragment Html fragment for the view. Allows Jinja2 template macros Define a new route that triggers when option clicked is set to true set option clicked to true when button is pushed, so that correct route is loaded next Import app package to start things off PixieApp Hello Word
  • 29. ©2017 IBM Corporation @DTAIEB55 Placeholder div for displaying data Display the output in the specified target Entity binding: use the df passed by user Allows binding of any entity created by the app Specify Display options for visualization Pass data to the app PixieApp HelloWorld with Data
  • 30. ©2017 IBM Corporation @DTAIEB55 Back to our U.K Data • In analyzing the geographical data, we can see a couple of clusters where accidents occur more frequently in Taraval - the southeastern corner looks particularly crowded. • Some useful questions to ask at this point are: • Does crime has an effect on the number of accidents? • Are there more accidents in these areas because more people speed there • Do traffic calming devices reduce the number of accidents? Note: In the next section, we’ll download datasets from the San Francisco Open Data Web site: https://data.gov.uk
  • 31. ©2017 IBM Corporation @DTAIEB55 PixieApp Dashboard Step 1: Create the skeleton from pixiedust.display.app import * @PixieApp class UKDashboard(): def mainScreen(self): return """ <div class="well"> <center><span style="font-size:x-large">Analyzing U.K Public Safety data with PixieDust</span></center> <center><span style="font-size:large"><a href="https://data.gov.uk" target="new">https://data.gov.uk</a></span></center> </div> <div class="row"> <div class="form-group col-sm-2" style="padding-right:10px;"> <div><strong>Layers</strong></div> </div> <div class="form-group col-sm-10"> <div id="map{{prefix}}”/> </div> </div> “””
  • 32. ©2017 IBM Corporation @DTAIEB55 PixieApp Dashboard Step 2: Create the map of accidents from pixiedust.display.app import * @PixieApp class SFDashboard(): def mainScreen(self): return """ <div class="well"> <center><span style="font-size:x-large">Analyzing U.K data with PixieDust</span></center> <center><span style="font-size:large"><a href="https://data.gov.uk" target="new">https://data.gov.uk</a></span></center> </div> <div class="row"> <div class="form-group col-sm-2" style="padding-right:10px;"> <div><strong>Layers</strong></div> </div> <div class="form-group col-sm-10"> <div id="map{{prefix}}”> <pd_options> { "handlerId": "mapView", ”mapboxtoken":"pk.eyJ1Ijoic…","timeseries": "false", "keyFields": "Longitude,Latitude","aggregation": "COUNT","kind": "choropleth-cluster", "valueFields": "Road_Surface_Conditions","basemap": "light-v9","rendererId": "mapbox" } </pd_options> </div> </div> </div> “”” • What have we added: • pd_entity: tell PixieDust which dataset to work on • pd_options: Contains the PixieDust options for the map (see next slide for more info)
  • 33. ©2017 IBM Corporation @DTAIEB55 Generating the pd_options for the Map The best way to generate the pd_options for a PixieDust visualization is to: 1. Call display() on a new cell 2. Graphically select the options for your chart 3. Select View/Cell Toobar/Edit metadata menu 4. Click on the “Edit Metadata” button and copy the pixiedust metadata
  • 34. ©2017 IBM Corporation @DTAIEB55 Initialize the Layers def setup(self): self.setLayers([…]) Note: setup is a special method that will be called automatically when the PixieApp is initialized.
  • 35. ©2017 IBM Corporation @DTAIEB55 PixieApp Dashboard Step 3: Create the GeoJSON customer Layers from pixiedust.display.app import * from pixiedust.apps.mapboxBase import MapboxBase @PixieApp class SFDashboard(MapboxBase): def setup(self): … self.setLayers([ { "name": "Police Forces Area", "url": "http://geoportal1-ons.opendata.arcgis.com/….." }, { "name": "Traffic Signals", "url": "http://data.cyc.opendata.arcgis.com/datasets/…", "type": "symbol", "layout": { "icon-image": "police-15", "icon-size": 1.5 } }, … ])
  • 36. ©2017 IBM Corporation @DTAIEB55 PixieApp Dashboard Step 4: Create the checkboxes from the layers from pixiedust.display.app import * from pixiedust.apps.mapboxBase import MapboxBase @PixieApp class UKDashboard(MapboxBase): … @route() def mainScreen(self): return """ <div class="well"> <center><span style="font-size:x-large">Analyzing U.K Traffic data with PixieDust</span></center> <center><span style="font-size:large"><a href="https://data.gov.uk/" target="new">https://data.gov.uk</a></span></center> </div> <div class="row"> <div class="form-group col-sm-2" style="padding-right:10px;"> <div><strong>Layers</strong></div> {% for layer in this.layers %} <div class="rendererOpt checkbox checkbox-primary"> <input type="checkbox" pd_refresh="map{{prefix}}" pd_script="self.toggleLayer({{loop.index0}})"> <label>{{layer["name"]}}</label> </div> {%endfor%} </div> <div class="form-group col-sm-10"> <div id="map{{prefix}}" pd_entity> <pd_options> {"handlerId": "mapView", ”mapboxtoken":"pk.eyJ1IjoicmFqcnNpbmdoIiwiYSI6ImNqM2s4ZDg4djAwcGYyd3BwaGxwaDV3bWoifQ.d5Rklkdu5MeGAnXu1GMNYw", "timeseries": "false","keyFields": "Longitude,Latitude","aggregation": "COUNT","kind": "choropleth-cluster", "valueFields": "Road_Surface_Conditions","basemap": "light-v9","rendererId": "mapbox","rowCount": "600" } </pd_options> </div> </div> </div> """
  • 37. ©2017 IBM Corporation @DTAIEB55 Run the PixieApp Dashboard You can find the final code for the PixieApp in the complete notebook : https://github.com/DTAIEB/de mos/blob/master/tutorials/ODS C%20London%20UK%2BTraffic %2Banalysis.ipynb
  • 38. ©2017 IBM Corporation @DTAIEB55 Final Thoughts • In this tutorial: • We showed how PixieDust help notebook users (data scientists or not) to load and visualize data without having to write code • We also showed how to use notebooks in new ways thanks to PixieApps by writing dashboard • Next Steps • Improve the analytics by finding new insights on the U.K Traffic accidents dataset, or perhaps start a new notebook with a new dataset from https://data.gov.uk • Improve the PixieApp dashboard by adding new layers. Just find the geojson link from the opendata site by clicking on the explore button (warning: not all datasets come with a geojson link). Then just add a new entry in the layers array