1. Home
  2. Explore
  3. Computer Science
  4. Reproducible Data Science

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Reproducible Data Science

Transcript

>> You're not going to want to miss this episode of The AI Show, where a bonafide data science leads us through how data science is different and how we can make it reproducible. Make sure you tune in. [MUSIC] >> Hello and welcome to this episode of The AI Show. We're going to talk a little about reproducible data science with machine learning. I got a special guest, Rafal Lukawiecki. How are you doing my friend? An actual real-life Data Scientist, tell us about yourself, my friend?

>> Well, thank you very much for having me here. It's an honor to be on your show. I have been working as a Data Scientist for actually more than 12, 13 years now. Even though, at first, my job title was a bit different, I was a Data Miner in the early days and subsequently, my job became predictive analytics and eventually through machine learning, I arrived at data science. >> You did data science before it was cool, and so now, you're going to help us a little bit by talking

about reproducible workflows in data science. What makes data science different than regular programming? >> This is a fascinating question and it's only recently that we are starting to clearly understand the differences. You see there is a software development aspect to data science, but it doesn't come until almost the very end of the process. The very end of the process is when you are ready to deploy something to production, at which point, there are similarities between

software development and data science or machine learning. But before we get to that point, there is an awful lot of work and time that we spend researching, finding things out, and making an enormous number of mistakes. Those mistakes in themselves are very valuable, and that is where we need to set ourselves up a little bit differently in terms of the workflow to actually benefit from those mistakes and to make our lives easier. >> I want you to dive in and give us

maybe some thoughts on how we can actually do that. I want to make sure that people get your information, projectbotticelli.com, tecflix.com. I'm putting your slide up here. Let's dive in into how something like this is accomplished. >> Well, thank you very much for mentioning my two companies. I have been teaching this as well as consulting for many, many years at projectbotticelli.com and anybody watching it, you are very welcome to join me on one of

my AI or machine learning or Azure ML courses, which I'm also offering through tecflix.com in self-based online learning form. I have prepared a few slides to introduce this concept. Is it okay, sir, if I go through some of them? >> Absolutely, let's do it right now. >> First things first, modeling is much more like research rather than software development, as we just mentioned a few seconds ago. If you think about the environments that we use for

day-to-day development of machine learning, unfortunately, we still tend to rely on environments which weren't designed for that purpose but were designed for more classical application or app development. If you think about Visual Code, Visual Studio or pretty much any other IDE, that is, integrated development environment out there. They have a lot of advantages. They come, for example, with great debuggers. But they really are designed to figure out what is that final product,

the good stuff that you want to put into production, and they hide all the sweat and errors and the mistakes that you made along the way. As it happens, while you are figuring things out during a typical machine learning project, it is those trials and errors and mistakes that are of particular value. For example, you may be building a clustering model and at one point you arrive to number of clusters that seems to be modeling the problem very well,

then you realize that this is not quite the right way forward, and you want to go back in time and you want to see your old results, your way of thinking, or maybe you are working within a group of people and maybe there are some more advanced and junior members of the team. When I get somebody else's work and they show me beautiful results like, "Look at these rock charts here and the fabulous decision trees that I have built," I want to know what else did they tried that didn't

work that made them think of that particular solution. If I just get the results inside Visuals Studio Code, I don't see that sweat, I don't know how they arrived at this point. That's exactly where the notebooks come in, and as an environment, they are very, very well suited to this research oriented part of machine learning and data science work that we do. >> That's amazing. For those who don't know about notebooks, could you describe them and why they're important for this modeling and exploration? >> Absolutely. A notebook is an interactive console. You can think of it as a console on steroids. An environment where you can enter simple things like 2 plus 2 and press some buttons and you learn that is 4 or not. The only difference is that, in addition to writing your code, you can surround it with explanations, texts, your thinking, and then you can leave the code that has arrived at some results, perhaps the incorrect ones,

you replicate the bit of code and you do it again in a better way, and then you replicate it and you do it again in yet another way. When you hand it over to somebody else, the person studying or reading it will be able to scan through your entire way of thinking rather than arrive at the final results. Now, a notebook can be locally hosted, you can put it on on your laptop, you can install it as part of your IDE even. It's also possible to install

nothing and use a hosted version of the notebook, Microsoft Azure Machine Learning, Azure itself, they come with a variety of hosted notebooks, which also make poaching our work quite easy between environment. I can start working one environment, I can wrap all of my workload into a notebook, I can upload it maybe into my customers Cloud and continue working there using their data without having to share data or unnecessary security boundaries that perhaps would have prevented

me in a more traditional way of working. >> With notebooks, because I always struggle, like if I'm working with other people, they have stuff in there and I have stuff in there and sometimes the output is different, we get merge conflicts, stuff happens, how do you make this all work out? >> That's a very good point. In general, a notebook tends to be more personal. The idea of a notebook which is shared within a team absolutely works too, but you have to componentize.

There are sections of the notebook that are managed by individuals, and perhaps you'll pull your results together into a notebook that represents the results of the whole team. However, what I find is that the simplest approach tends to work better, whereas everybody carries their own notebook and it's perhaps some named after the individual, and is of course, part of the project that we man it using Git. When it comes to the point that we are ready to deploy something into production, that's when we actually throw the notebooks away. At that point, we take those final versions, we take the parameters we arrived at and whatever data preparation steps we had agreed upon. We tend to use a more classical, perhaps even IDE-based development approach, which is most suited towards those final stages of a project, and it's definitely much more suited to maintaining the project when it's already in production, and perhaps it means debugging, updating, and so on.

There's really two mentalities over here. The research part, where we tend to be lords of our own, and there is no issue with Git mergers, because, well, at worse, they are my own matters, and that switch over to the production mentality later on. >> That's an interesting way of looking. This is the part I've struggled with. You're creating a separation in phases between when you're modeling and researching, and when you're like, okay, it's time to move on, then you get into the pure software engineering. I'm I getting this right? >> Absolutely 100 percent. In larger companies, some of the larger customers of mine, there would be a separate team that will be looking after the deployability and the deployment of the model. This natural division of how we work actually slows very nicely into the entire operations lifecycle of whatever it is, we do using machine learning, or data science. >> Let's talk about what the actual, do you have,

like a reproducible workflow, or a set of steps that you suggests we should follow? >> Assuming that you are working in an environment like the RStudio, R MarkDown notebook, or JupyterLab for Python, the process is very similar. There is one important difference around step number 3, I'll explain it in a moment, but in general, you start creating a project which will hold all the source code, including the notebook source, any in-memory data that you wish to save in-between the runs,

and the entire history of everything that you have done. There are different attitudes to whether you want to preserve that in-memory data, or whether it's better to always erase it and start a fresh. But we're not going to get into details of that quite at this stage, that's something you can decide on later. Of course, you have to set up Git, there is no question love it, or otherwise, this is the environment and a tool of choice for versioning and source code control. It works exceedingly well with RStudio, R Markdown notebooks. This is actually one place where it has an advantage over Jupyter and JupyterLab. In Jupyter, you have this invisible layer that is separating what you do from the source, which happens to be JSON files. When you work in RStudio and use an R Markdown notebook, everything's text, just like in C-Sharp, or in Python or Java, or any programming language, text is the notebook, and the notebook simply provide you

a preview of the things that you do. In some ways, using Git in conjunction with R Markdown and RStudio is easier than working with Jupyter and JupyterLab, but those things can be solved for Jupyter too. Step number 3 is the very interesting one, and one where Microsoft definitely has helped us along way in the R Ecosystem. In the R Ecosystem, we have a concept of a snapshot and of time machine. The time machine is provided courtesy of the MRAN, which was Microsoft daily snapshot of CRAN.

CRAN is the repository archive of all the packages and sources of the entire R Ecosystem, 16,500 packages at this point in time. When we start a project, we usually snapshot the state of the world to a particular date. That date very often would be the date when the project commences, but sometimes, we choose a slightly older date. There are sometimes better days to choose when things were a little bit more stable than perhaps just after a major release of an update of

some major part of RStudio or the R Ecosystem. Anyway, now that you've got to that snapshot and you have frozen in time the evolution of the R System, you start working in that notebook. That means that from this point on, even if the world around us changes, at least we are not influenced anymore by the changes of our dependencies. In Python, of course we saw that using virtual environments, and the myriad of tools available to deal with that, or perhaps Anaconda, again,

with its own additional tools and complexity. I think the R world does it in a simpler way, but it's probably unfair comparison, because in some ways the dependencies of the world of Python are more complex. Now that you've been doing your work for a little while in the notebook, you are ready to share and export your findings. This could be as easy as just exporting some lovely PDFs, or HTML, so later, if you want to publish things in an academic journal.

Finally, as we already mentioned earlier, when you reach those final stages that things are good and tested, and you want to put them to production, we swiftly abandon the notebook environment, archiving it for future purposes of course, and we switch to using an IDE. A huge advantage of this is that, if say three months down the line, or six months, or a couple of years down the line, somebody comes to one of my customers and it says, "Can you explain how you arrived at this conclusion, when you were building this system?" I'm not looking at some source code, but I'm going back to the notebook, and I'm showing the whole way of thinking that reached this point, and this is the reproducibility element of this whole thing. Because if someone says, "I think you made a mistake, and I want you to retrace your steps and prove to me that you haven't messed around with the data and light with statistics, or whatever it is people shouldn't be doing, but they unfortunately sometimes do."

It's as easy as replaying part of the notebook. If notebook contains also your data preparation, as in my opinion it should, we can go right from the starting point, right from the early formal data all the way to my conclusions. I take off all my responsibility, my auditability, explainability, and if I fall under the bus, and somebody else has to pick up my work, they pick up again using the notebook from wherever I left off my thought process. I and my customers have found it to be

absolutely extra ordinarily helpful way of working. It's the little things that helped you along the way, for example, the snapshot. >> This is really cool. I feel I want to see some of this in action, do you have something you can show us? >> Absolutely. I prepared a couple of short examples. To start off, I'm showing here how I would be doing things in a fully locally installed R ecosystem. This happens to be running together with Microsoft SQL Server ML Services,

which are used extensively for production purposes because of its performance advantages, but also because it's easy for me to analyze things as close to the data. Since things live, in the real world, very often in traditional relational databases and a lot of our customers use SQL Server, that's a match made in heaven. But if you look carefully, here around line 14, I am issuing this checkpoint call saying, lock me, freeze me to the way the R world was around the 10th of March 2019.

That means that when this was created in its original version, even if any part of the R ecosystem has changed since then, I am not influenced by that. I have reproducibility as long as my data has been frozen in time and as long as the environment has been frozen in time. That second part has been solved by the snapshotting system, which is part of the Microsoft R ecosystem. If you do it, by the way, in the Azure world, you have to do it a little bit differently,

but it's literally a one extra line that you have to add. Where I'm scrolling to right now is, I'm scrolling past the initial analysis of the data, some early descriptive statistics that perhaps I would have done, and I'm getting to the part when I am going to do a little bit of clustering of this data. Using, in this case, the same very data set that's sitting in SQL Server, I am going to train a bog standard k-means clustering model using the R engine,

and I am visualizing the quality of my clusters. To anybody who has ever worked with any form of clustering, their immediate response is, "Oh, dear. Those clusters are so on top of each other, they are so dirty, the overlap is not even unacceptable. There is no point doing clustering this way any further." But I am not going to start changing this code here and there, instead, I want anybody reading it to see the bad results that I've got here and how I have improved this later.

Well, in this case, the results were bad because all of my good data happens to be categorical in nature. In other words, those are not nice numbers than the k-means algorithm requires or prefers, so in my next step, I simply convert the categorical data to data that the k-means algorithm will like more. In this case, I'm using a particular function of the R ecosystem that creates indicator columns. It's so similar to something that in other environments is called one-hot coding and so on.

As soon as I run my better version of clustering, you may be able to see that something is starting to happen over there. Still there is way too much overlap between the clusters, but the shape of the data, represented by the green dots, is starting to, well, look more interesting. There's clearly some shape over there, it's not just one mess of dots on top of each other. In the final part of my thinking in this particular application,

I want to see if I could optimize the way those clusters look by using a little bit of clever automation when it comes to clustering, a technique which I can invoke by calling k-means runs. This is very helpful function in the R ecosystem that comes from yet another package that specializes in clustering. If I run that, you will be able to see that my clusters will acquire at least some separation. At this point, we are looking at the same data.

The green dots are still in the same place as they were before. We still have some overlap between the clusters, but their shape, represented by the so-called principal components, is indicating that we're starting to see some significant variability in terms of their definition. Without going into any details of exactly what is the purpose of this analysis and so on, which is not the purpose of our show, I suppose today, this gives anybody who will

be reading it at a later point a record of my thinking. I can save those results, I can preview them using HTML or whatever environment my boss or my client wishes me to use to prove to them that I guess I've done my job well. But the other advantage of this approach is that once I have done my hard work maybe locally, I can transfer it almost verbatim into any other environments that understands Notebooks. That means, I will be also including the data, I'll be also including my way of thinking.

To show you that thought, I will open my Azure Machine Learning Studio workspace in which one of the features introduced by Microsoft earlier this year, reasonably recently, is this concept of compute instances, which I find very helpful. Compute instance is basically like your virtual machine, except it has pre-installed JupyterLab, Jupyter, and of course, in my case, very important, RStudio. That pre-installed version of RStudio does pretty much everything I can do locally,

subject to the packages, and libraries, etc that I have installed. Small difference, before I can checkpoint my notebook to a particular point in time, since I am not using SQL Server's ML Services this time, I have to pre-install the checkpoint package, but that's just one extra line. Other than that, this is more or less the same setup. Without going to any more detail of how I got this, since you've already seen me do it earlier, there's the results, except this time,

this is all happening through the browser on that compute instance in RStudio. One interesting advantage of doing this here in Azure Machine Learning is that I can share not only access to my notebooks, in this case, this is the notebook that I was showing you, but I can also share access to the underlying data that Azure ML handles for me. Most interesting of them all, if at some point what I am doing needs to grow up to larger, perhaps, some compute requirements and maybe I need

to do some heavy parallelization of machine learning, by having R and RStudio run in the notebook, I can fire off my machine learning jobs within a proper big compute cluster, maybe even R Spark cluster running inside Azure ML without having to do any setup. That's the best part. If I wanted to set all of this up locally, setting up that IDE, setting up all the Docker configurations, this is not for the faint hearted. Certainly, as Data Scientists, we'd rather focus on data science than

Kubernetes and worrying about all of that. So when I saw that Microsoft gave me RStudio together with Azure Machine Learning and it's all pre-installed and just a matter of clicking that button, then I was pretty much sold on the idea. I wish it was a little cheaper. >> I will talk to the people. But this has been impressive because to me, I think the key takeaway that I'm finding is that first of all, there is a sciency part of data science and we shouldn't discard that.

We should think about it in terms of science, and it's okay to leave a record of things that we did wrong so that then moving forward, we know what's going on, and then when it's time to move forward, then we can go into the software engineering. I love that. I also love seeing RStudio locally. It looks like it's RStudio, but in the Cloud basically, is that right? >> That is exactly right, it is RStudio in the Cloud. It works exactly the same way, the only difference is the libraries and packages that you have access to in the two environments. But certainly, when it comes to the open source packages, as long as you're not using anything very proprietary, the setup and what you can do is exactly the same. When you want to do the proprietary stuff, Microsoft has some very fast machine learning available in SQL Server on the Microsoft ML package and the RevoScaleR package. Unfortunately, it's a little bit more tricky to do it in the Cloud at the moment, but with the exception of

the more proprietary Microsoft techniques, it's exactly the same. >> Well, this has been super informative for me. I feel edified by this. It's okay to do science, we need to acknowledge it and do it, I love that part. Where can people go to find out more about you and your work? >> Well, thank you for asking. If anybody is interested in joining me on a live course, I have fairly intense hands-on five-day classes, and projectbotticelli.com is where you can learn about it.

If you want to follow learning using self-based video approach, then I just launched a new business a few weeks ago, it's called tecflix.com, and hopefully it will be appropriate and useful for this new era that we are all in now. >> Awesome. Well, Rafal, thank you so much for spending some time with us. Again, those that are watching, thank you so much for watching. We've been learning all about reproducible data science with machine learning. Rafal, you've been awesome, thanks for being with us. >> Thank you. >> Thank you for watching, and hopefully we'll see you next time. Take care. [MUSIC]

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent