Page
1
M A N N I N G Angelica Lo Duca
Page
2
DATA INFORMATION KNOWLEDGE WISDOM RAW INSIGHT CONTEXT ACTION Lorem ipsum dolor sit Lorem ipsum dolor sit Background Background Next Steps Turn data into wisdom through the data, information, knowledge, wisdom (DIKW) pyramid. Each step elaborates and enriches data, and at the top, data is transformed into a powerful, data-driven story.
Page
3
Data Storytelling with Altair and AI
Page
4
(This page has no text content)
Page
5
Data Storytelling with Altair and AI ANGELICA LO DUCA MANN I NG SHELTER ISLAND
Page
6
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2024 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The authors and publisher have made every effort to ensure that the information in this book was correct at press time. The authors and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Ian Hough 20 Baldwin Road Technical editor: Ninoslav Čerkez PO Box 761 Review editors: Dunja Nikitović and Isidora Isakov Shelter Island, NY 11964 Production editor: Keri Hales Copy editor: Christian Berk Proofreader: Katie Tennant Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781633437920 Printed in the United States of America
Page
7
To Andrea, my unique love and best friend of my life
Page
8
(This page has no text content)
Page
9
brief contents PART 1 INTRODUCING ALTAIR AND GENERATIVE AI TO DATA STORYTELLING................................................1 1 ■ Introducing data storytelling 5 2 ■ Running your first data story in Altair and GitHub Copilot 22 3 ■ Reviewing the basic concepts of Altair 49 4 ■ Generative AI tools for data storytelling 86 PART 2 USING THE DIKW PYRAMID FOR DATA STORYTELLING..........................................................121 5 ■ Crafting a data story using the DIKW pyramid 123 6 ■ From data to information: Extracting insights 154 7 ■ From information to knowledge: Building textual context 183 8 ■ From information to knowledge: Building the visual context 218 9 ■ From knowledge to wisdom: Adding next steps 248vii
Page
10
BRIEF CONTENTSviiiPART 3 DELIVERING THE DATA STORY ....................................283 10 ■ Common issues while using generative AI 285 11 ■ Publishing the data story 300
Page
11
contents preface xv acknowledgments xvii about this book xviii about the author xxii about the cover illustration xxiii PART 1 INTRODUCING ALTAIR AND GENERATIVE AI TO DATA STORYTELLING ......................................1 1 Introducing data storytelling 5 1.1 The art of data storytelling 6 Why should you use data storytelling? 8 ■ What problems can data storytelling solve? 10 ■ What are the challenges of data storytelling? 10 1.2 Why should you use Python Altair and generative AI for data storytelling? 11 The benefits of using Python in all the steps of the data science project life cycle 13 ■ The benefits of using generative AI for data storytelling 13 1.3 When Altair and generative AI tools are not useful for data storytelling 15ix
Page
12
CONTENTSx1.4 Using the data, information, knowledge, wisdom pyramid for data storytelling 16 From data to information 17 ■ From information to knowledge 19 ■ From knowledge to wisdom 20 2 Running your first data story in Altair and GitHub Copilot 22 2.1 Introducing Altair 23 Chart 23 ■ Mark 24 ■ Encodings 24 2.2 Use case: Describing the scenario 27 The dataset 27 ■ Data exploration 28 2.3 First approach: Altair 30 From data to information 30 ■ From information to knowledge 38 ■ From knowledge to wisdom 40 Comparing Altair and Matplotlib 42 2.4 A second approach: Copilot 44 Loading and cleaning the dataset 44 ■ Calculating the percentage increase 45 ■ Plotting the basic chart in Altair 46 ■ Enriching the chart 47 3 Reviewing the basic concepts of Altair 49 3.1 Vega and Vega-Lite 49 Vega 50 ■ Vega-Lite 55 ■ How to render a Vega or Vega-Lite visualization 57 3.2 The basic components of an Altair chart 58 Encodings 59 ■ Marks 61 ■ Conditions 61 ■ Compound charts 62 ■ Interactivity 66 ■ Configurations 68 3.3 Case study 73 From data to information 74 ■ From information to knowledge 80 ■ From knowledge to wisdom 82 4 Generative AI tools for data storytelling 86 4.1 Generative AI tools: On the giants’ shoulders 87 What is artificial intelligence? 88 ■ What is machine learning? 88 ■ What is deep learning? 91 ■ What is generative AI? 92 ■ Generative AI tools landscape 93 4.2 The basic structure of a ChatGPT prompt 96 Defining the task 96 ■ Acting as a role 97 ■ Tailoring to an audience 98
Page
13
CONTENTS xi4.3 The basic structure of a DALL-E prompt 99 Subject 99 ■ Style 102 ■ The Edit Image tool 102 4.4 Using Copilot to build the components of an Altair chart 103 Prerequisites 104 ■ Marks 104 ■ Encodings 104 Conditions 105 ■ Compound charts 105 ■ Interactivity 106 4.5 Case study: Your training team 106 Turning data into information 107 ■ Turning information into knowledge 112 ■ Turning knowledge into wisdom 117 PART 2 USING THE DIKW PYRAMID FOR DATA STORYTELLING ................................................121 5 Crafting a data story using the DIKW pyramid 123 5.1 Breaking the ice: The homelessness tale 124 What was wrong with the chart? 126 ■ What was wrong with the presentation? 130 5.2 Uncovering the narrative: What a data story is 132 Using the DIKW pyramid to streamline a data story 133 DIKW in action: Completing the homelessness tale 134 5.3 Incorporating generative AI into the DIKW pyramid 138 5.4 Behind the scenes: The homelessness tale 140 Creating a compelling subtitle 140 ■ Generating images 141 5.5 Another example: Fake news 142 From data to information 144 ■ From information to knowledge 148 ■ From knowledge to wisdom 151 6 From data to information: Extracting insights 154 6.1 An intuitive approach to extract insights 155 Connection strategy 155 ■ Coincidence strategy 157 Curiosity 159 ■ Contradictions 160 6.2 Choosing the characters of your story 161 6.3 Choosing the right chart 163 The cooking charts family 165 ■ The bar charts family 169 The line charts family 173 ■ The geographical map family 175 Dot charts family 177 6.4 Case study: Salmon aquaculture 177
Page
14
CONTENTSxii7 From information to knowledge: Building textual context 183 7.1 Introducing context 184 7.2 Calibrating the story to the audience 185 General public 185 ■ Executives 187 ■ Professionals 187 7.3 Using ChatGPT for commentaries and annotations 188 Describing the topic 189 ■ Describing the type 191 Setting custom instructions 192 7.4 Using large language models for context 193 Fine-tuning 195 ■ Retrieval augmented generation 203 7.5 Case study: From information to knowledge (part 1) 209 Tailoring the chart to the audience 210 ■ Using RAG to add a commentary 210 ■ Highlighting the period of decrease in sales 212 ■ Exercise 214 8 From information to knowledge: Building the visual context 218 8.1 Emotions: The foundations of visual context 219 8.2 Color 221 Setting colors in Altair 222 ■ Exercise: Setting colors 223 8.3 Size 224 Font size 224 ■ Chart size 225 ■ Exercise: Setting size 225 8.4 Interaction 226 Tooltip 227 ■ Slider 228 ■ Drop-down menu 229 Exercise: Setting interactivity 232 8.5 Using DALL-E for images 232 Adding emotions 233 ■ Generating consistent images 235 Exercise: Generating images 237 8.6 Strategic placement of context 237 Top placement 238 ■ Left placement 239 ■ Within placement 240 8.7 Case study: From information to knowledge (part 2) 241 Setting a negative mood 242 ■ Setting a positive mood 244 Exercise: Making the chart interactive 245 9 From knowledge to wisdom: Adding next steps 248 9.1 Introducing wisdom 249 Transforming knowledge into wisdom: Next steps 250 Using ChatGPT as a source of experience 251 ■ Good judgment: Anchoring the action to an ethical framework 252
Page
15
CONTENTS xiii9.2 Case studies 253 Chapter 1: The dogs and cats campaign 253 ■ Chapter 2: The tourist arrivals 259 ■ Chapter 3: Population in North America 263 ■ Chapter 4: Sport disciplines 265 ■ Chapter 5: Homelessness 269 ■ Chapter 5: Fake news 270 ■ Chapters 6–8: The salmon aquaculture case study 275 9.3 Strategic placement of next steps 277 Title placement 278 ■ Right placement 278 ■ Below placement 280 PART 3 DELIVERING THE DATA STORY..........................283 10 Common issues while using generative AI 285 10.1 Hallucination, bias, and copyright 285 AI hallucinations 288 ■ Bias 289 ■ Copyright 290 10.2 Guidelines for using generative AI 291 10.3 Crediting the sources 293 Under the title or subtitle 294 ■ Under the main chart 294 Under the next steps 294 ■ Sideways 295 ■ Implementing credits in Altair 296 11 Publishing the data story 300 11.1 Exporting the story 301 11.2 Publishing the story over the web: Streamlit 302 11.3 Tableau 304 11.4 Power BI 305 11.5 Comet 310 11.6 Presenting through slides 314 11.7 Final thoughts 317 appendix A Technical requirements 319 appendix B Python pandas DataFrame 325 appendix C Other chart types 331 index 353
Page
16
(This page has no text content)
Page
17
preface Since ancient times, humans have told stories to transmit values and communicate with their fellow humans. The power of a story is enormous. It involves those who lis- ten and excites those who tell. A very strong bond is established between the narrator and the listener, which goes beyond the content of the story itself. For this, every story- teller should know their audience and adapt stories according to their audience’s val- ues, traditions, and culture. The power of a data-based story should be even stronger because it is anchored in the evidence provided by the data. It’s impossible to disbelieve a story based on data. Personally, I have always loved stories, although to tell the truth, when I was little, my mother didn’t tell me many. It was my grandmother who told me ancient stories of imaginary characters—some even terrifying—who invited my childhood imagination to create a world entirely apart. I have always had a passion for stories, and I started writing them when I was very young. I remember my first story, written when I was seven. Over the years, I have continued to write short stories, poems, and short novels. More than 15 years ago, I began my adventure in software development and data sci- ence applied to research and university education of students. I developed a great love for data and the people behind it, as well as a passion for software development. At the same time, I continued to write stories, poems, and short stories in the brief moments I found to dedicate to my hobbies and passions. Then, a few years ago, I had a flash of genius, thanks to my boss, Andrea Marchetti. I will never tire of thanking him for helping me combine data with stories. From this discovery, my passion for data storytelling was born. In my own small way, I had thexv
Page
18
PREFACExvisame experience as Steve Jobs, who, as a young man, had learned the art of calligra- phy in a university course and then put it aside. Ten years later, it all returned to him: Jobs designed the first Macintosh and used the information he learned in calligraphy class to design the first computer with beautiful typography. Jobs said, “You can’t con- nect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future.” (Excerpt from Jobs’s speech on June 12, 2005, to the recent graduates of Stanford University.) And so it happened to me, too: I combined my passion for data with my passion for stories. My hope is that you, too, can connect your dots and that by reading this book, you can find ideas for tackling your daily work in a new and exciting way.
Page
19
acknowledgments First, I would like to thank my husband, Andrea. His patience and support have helped to bring this book to life. I am also grateful to my two children, Giulia and Antonio, for their smiles and love. A big thank you goes to my father, Angelo, for teaching me to never give up, even in the face of difficulties. I would also like to thank my sweet mother for her tenderness and encouraging cuddles. Mum, I miss you. This book wouldn’t have seen the light of day without the incredible work of the fantastic team at Manning: Andy Waldron, acquisitions editor; Ian Hough, develop- ment editor; Ninoslav Čerkez, technical editor; and the rest of the production team, who worked diligently to get this book to press. Also, I thank all the reviewers: Alain Couniot, Ali Shakiba, Andre Weiner, Ankit Anchlia, Bin Hu, Daniel Paes, David Cronkite, George Carter, Greg Grimes, João Marcelo Borovina Josko, Jeremy Chen, Joel Kotarski, Jose San Leandro, Karan Gupta, Kaushik Kompella, Keerthivasan Santhanakrishnan, Krishnamurthy TV, Madiha Khalid, Manos Parzakonis, Maxim Volgin, Mikael Dautrey, Monica Guimaraes, Nadir Doctor, Oliver Korten, Peter Henstock, Radhakrishna MV, Richard Vaughan, Sarang S. Brahme, Scott Chaussee, Shaurya Khurana, Shiroshica Kulatilake, Sriram Macharla, Subhasis Ghosh, Thomas Joseph Heiman, Vidhya Vinay, and Xiangbo Mao. Without their suggestions and expertise, this book would not be what it is. A final thank-you goes to you, reader, who, by purchasing this copy of the book, has placed your trust in me. I hope I don’t disappoint your expectations. Many thanks to all.xvii
Page
20
about this book I wrote Data Storytelling with Altair and AI to help you improve your skills in communi- cating data exploration and analysis results. Although many tools exist to build data stories, such as Tableau and Power BI, there is a need for Python data scientists to build stories directly using their preferred programming language. In fact, if they per- form all data exploration and analysis in Python, it should be natural to communicate data results using the same programming language. This book combines theoretical concepts, such as the data, information, knowledge, wisdom pyramid, and practice (Python, Altair, and generative AI) to make you aware of the potential of data story- telling while programming. Who should read this book Data Storytelling with Altair and AI is for Python data scientists or analysts and developers looking to learn the topic of data storytelling. Both beginners and experienced “Python- ists” will learn to use Python and generative AI for data storytelling. Although many blog posts and online resources exist, this book organizes concepts progressively so that the reader can start learning a new concept after they have assimilated the previous one. How this book is organized: A road map This book comprises three parts covering 11 chapters and 3 appendixes. Chapters 1–4 introduce the topic and explain the logic followed in the book: how to combine data storytelling, the DIKW pyramid, Altair, and generative AI. Chapter 5 dives deep into the DIKW pyramid, and chapters 6–9 analyze each step of the DIKW pyramidxviii