You likely know that Python is one of the fastest growing languages for data science.
This is a discipline that combines the scientific inquiry of hypotheses and tests,
the mathematical intuition of probability and statistics, the AI foundations of machine learning,
affluency in big data processing, and the Python language itself. That's a very broad set of skills
you'll need to be a good data scientist, and yet each one is deep and often hard to understand.
That's why I'm excited to speak with Joel Gruse, a data scientist from Seattle. He wrote a book to
help us all understand what's actually happening when we employ libraries such as scikit-learn or
numpy. It's called data science from scratch, and that's the topic of this week's episode.
This is Talk Python to me, episode number 56, recorded April 19th, 2016.
Welcome to Talk Python to me, a weekly podcast on Python, the language, the libraries, the
ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter where I'm at
mkennedy. Keep up with the show and listen to past episodes at talkpython.fm, and follow the show on
Twitter via at talkpython. This episode is brought to you by SnapCI and Hired. Thank them for supporting
the show on Twitter via at snap underscore CI and at Hired underscore HQ. Hey, everyone. It's great to be
back with you. I have something to share before we get to the main part of the show. I had a chance to be on the
Partially Derivative podcast this week, where we did a short segment on programming tips for data scientists,
relevant to this episode, wouldn't you say? I talked about using generator methods and the yield
keyword to dramatically improve performance while processing lots of data in a pipeline.
If you want to hear more about that kind of stuff, check out the link to the Partially Derivative episode
in the show notes. Now, let's talk to Joel. Joel, welcome to the show. Thanks. I'm glad to be here.
Yeah, I'm really looking forward to doing data science, but from scratch today.
I know. That's my book. I'm the right person to be here for that topic.
Fantastic. So we're going to dig into data science. And I think your idea of
taking it from the fundamentals and building something that's not so complicated or so well
polished and optimized that you can really understand what you're doing is great. But before we get into
that, let's just sort of start from the beginning. How did you get into programming in Python?
So if you go back about 10-ish years or so, I was in grad school for economics. I took a class called
probability modeling, which was a lot of simulating Markov chains and doing Monte Carlo simulations and
things like that. And the class was actually taught in MATLAB. School had a site license for MATLAB that
only worked on campus. And so that meant that if I went home and worked at home, which I like to do,
I couldn't use MATLAB. And so as an alternative, I found that I could use Python and NumPy to basically
do all the MATLAB-y things without that site license. So that's actually why I started being
into Python. And then I just kind of liked it and stuck with it.
Yeah, I can definitely see how you would like working in Python more than MATLAB. I've
spent my fair share of time in the .im files and it's all right, but it's not Python.
You were studying economics and that's kind of how you got interested in data science,
trying to answer these big questions. The subject of economics or some other way?
I followed sort of a convoluted path. My background was math and economics.
And I actually started off doing quantitative finance, options, pricing, mathematics of financial
risk. I worked at a hedge fund. And then when the hedge fund went out of business, I just kind of
lucked into this job at a startup called Faircast, where I was doing a lot of kind of BI work,
writing SQL queries, building dashboards. And over the years, I just sort of moved more and more in a
data science kind of direction and eventually became a data scientist and software developer.
And certainly the math and economics training was helpful for that. But it was
it was a career that I sort of ended up in by accident rather than through a deliberate plan.
That's a little bit like my my career path as well. Studied math and just sort of had to learn the
programming and stumbled into it. That's cool. What do you do day to day now?
So I just started a new job. I work at a nonprofit. It's called the Allen Institute for Artificial
Intelligence. It was founded by Paul Allen, who was one of the Microsoft founders. And we're
basically doing kind of fundamental AI research. I can't tell you that much about what I do. So
it's really my second week and I'm still kind of just learning what goes on. I'm learning my way
around. But I work on a team called Aristo, which is basically building AI to take science quizzes and
to understand science. Oh, very interesting. Yeah, that sounds like a fascinating place to work.
It's really neat. Yeah. A lot of smart people, interesting problems.
Would you say it's different than working in a hedge fund or is it surprisingly similar?
No, it's totally different. I mean, it's similar in that there's a lot of smart people,
but it's not really similar in any other way. Yeah. The goals are not necessarily the same,
are they? The goals, the incentives, the sort of day-to-day stress levels, it's all different.
Yeah. Yeah. Sounds great. So let's talk about your book a little bit. It's called Data Science
from Scratch. And I think that's a really cool way to approach it. We have all these super polished
data science tools. You know, we have them in Python, we have them in R and other languages as well.
Why from scratch, rather than just grabbing one of these libraries or a set of these libraries and
talking about how to use them? So a couple of reasons. One is, as I mentioned,
I come from a math background and the math way of doing things is you can't really use something
until you can prove it. I once had a teacher who came in and he looked at the syllabus for the
previous semester and he said, oh, good, you proved this theorem. That means I can use it in this class.
And so there's this real rigor around, you can't use things unless you understand them.
And that approach always kind of resonated with me. And so that, to a large degree,
that's the approach I took in the book. At the same time, you also have all these really powerful,
really easy to use libraries like Psych and Learn, where you can go in and basically copy and paste,
you know, five lines of code. And you've built a decision tree or you've built a regression model.
And it's very easy to not know what you're doing. And you can pick up books and they'll tell you what
commands to type. But you can also type those commands and again, not know what you're doing.
And so I thought the book I would like, the book that I would have found valuable would be,
here's what these models are actually doing behind the scenes. And so then when it's time you apply
them, you kind of understand the principles, you understand where they go wrong, where they go right,
what they're good for, and what they're actually doing.
Yeah, I think that's, that's really interesting. And that is very much the mathematical way of,
if you can't prove it, you can't use it, sort of a way of thinking, which is not that common
in programming, you know, if there's an API, hey, just grab it and use it. But I think it's really
important in data science, because you have all these different disciplines and backgrounds coming
together to make it work, right? You're not just a pure programmer, and you're not, you know, a
mathematician or some sort of domain expert, right? You kind of have to blend these together.
Yep. And the other thing that I think ended up working pretty nicely about this approach was,
there's a lot of really mathy books about data science and machine learning, that's like,
here's an equation, here's an equation, here's an equation. But what I ended up doing was a little bit
of math. But then when it came time to, here's how something works, do it in working Python code.
So that it's rigorous in terms of here's all the steps laid out. But it's also, that's the code you
can run and sort of follow along.
Yeah, absolutely. Why Python and not something like R or some other language?
So the short answer is that, for whatever reason, R is not sympathetic with the way my brain works.
And whenever I try to sit down and start writing things in R, I just find that it doesn't work the way
I expect it to. The joke is that R is a language designed by statisticians for statisticians.
There's some truth to that, some unfairness to it. But for whatever reason, Python is much
friendlier to the way that my brain works and the way that I solve problems. Actually, the first draft
of my book was a lot harsher against R. And then some of the earlier reviewers didn't like that.
They really don't like that. So I kind of revised it to be more gentle.
Yeah, I think one of the things that's nice about Python is if you invest to learn data science
through Python, or the Python data science tools, rather, and then you want to build sort of a more
working application, you don't have to, you know, convert that to some other language, right? You're
already in Python. It's sort of a full stack thing you keep working with, right?
That's one important aspect. I think another important aspect is that from my perspective,
Python code, if written well, is super readable. And so you don't necessarily have to be a Python
expert to take a well written piece of Python code and understand what it's doing. Whereas other
languages can be a lot more impenetrable. And so I find it a nice teaching language from that
perspective as well.
Yeah, I would say, you know, most of the universities in the US came to that same conclusion,
right? With Python being the most common CompSci 101 course these days, as opposed to like Java or
Scheme, which I took.
Yeah, I mean, when I took, I also did Scheme when I took CompSci 101 way back in the day. And I actually
thought that was a really nice language for learning computer science. But I can see why Python would be
a better language for, you know, learning programming as opposed to computer science.
So when you start off your book, you actually have a few sections that are just about,
I'm going to teach you just enough Python to get started so that you can understand the data
science tools and what we're doing. What kind of stuff do you put in there? Like, what do you
consider fundamental for doing data science? And what can be sort of learned later?
The most important things I put in were obviously functions, you want to do any kind of
analytic work or Python functions. The other thing I really tried to emphasize,
was writing kind of clean Pythonic code. So I also did a lot of list comprehensions,
as well as understanding all the basic data structures. So lots of dicks and sets and some
of the less common ones like default dicks and counters, I also make quite heavy use of.
So basically, anything you would need to understand, here's how to write out an algorithm in Python,
here's how to use the correct data structures in Python, I kind of baked into that initial
introduction. Whereas more advanced things like really specialized data structures like queues,
I just kind of introduced as needed later in the book, some of the more esoteric math functions,
I just kind of introduced when they came up.
I think obviously, nicer dictionaries, things like default dict and so on,
played a really important role in the subsequent pieces where you're trying to make concise,
clean statements. I thought your use of sort of list comprehensions and generator expressions,
along with the other functional pieces like zip and counter and so on, were really,
really interesting. You write some very concise code without being unreadable. I thought that was
nice.
That's one of the things I aim for. And as much time as you spend sort of revising the text of the book,
I also spent probably just as much time revising the code of the book. I went back over it many,
many times thinking, can I make this simpler? Can I make it cleaner? Can I make it easier to
understand? And I try and do that. I'm kind of a stickler for code elegance. It's kind of a
personal fault, maybe.
But also, you come out with very polished codes. I found it very easy to follow. And I thought the
focus on this functional style programming was really neat. So one of the things you talk about
in the beginning is about visualizing data, because you have all these numbers. And obviously,
to understand them, putting a picture to it is very nice. And what is that? Matt Plotlib?
Yes. In the book, I only use Matt Plotlib. I was just having a conversation with some of my data
science buddies about a week ago, where they were, as always, complaining that the data visualization
story in Python is still not very good.
Yeah. So you said that Matt Plotlib was starting to show its age in your book. What did you mean by
that?
When I first started learning Python, what I said, more than 10 years ago, as a Matlab replacement,
like Matt Plotlib was around back then, and it had the same features and the same interface, and it
produced the same not particularly attractive plots. So it hasn't kind of evolved as the rest of the
language has evolved, say. And there's been some projects like Seaborn that try and put some prettiness
on top of it. And there's been some other attempts to kind of bring the R-style GG plot into Python.
But from my perspective, none of them has really like won the mindshare and become the solution.
So everyone kind of does something different.
Yeah. You also talked about Boku, which is from the Continuum guys. I had them on show 34. Can you
maybe talk about that really quickly? Just what is it? What's it used for? That sort of thing.
It is another visualization library. And I have to be honest with you, I haven't really
checked it out since I was writing the book like more than a year ago. So I seem to remember that it
seemed to have some facility for building in interactive plots and things like that.
It's kind of D3, fancy graphics in the browser type stuff, right?
I believe so. But I haven't spent much time playing with it.
Yeah.
I haven't been doing a lot of, probably doing actually more D3 than Python visualization recently.
Right, right. Tell everyone what D3 is. I don't think we've talked too much about that on the show.
Oh, sure. So D3 is actually a JavaScript library for building interactive data visualizations.
It brings sort of an interesting model where you bind your data to DOM elements, which are quite often
SVG elements. And so what that makes it easy to do is to have a data set. And when you add new data to it,
your plot updates, and it allows a lot of really interesting interactivity. If you check out the
D3 website, they have a gallery of all sorts of amazing visualizations where you look at and think,
how on earth did they do that?
Yeah. Whenever I go to the D3 website, I'm like, wow, I want to use this. I don't have a use for it,
but it's fantastic. How can I just build stuff that looks like this?
Well, I mean, so I have a joke, which is that good data scientists copy from the D3 gallery and great
data scientists steal from the D3 gallery. And they give you the code for all of them. So I would say
probably close to 100% of the D3 visualizations I've ever built have been something I found in the D3
gallery and kind of tweaked until it fit my data.
Yeah. Yeah. Nice. So the next section you talked about was like the fundamentals of the math and
science that you need to know in order to be a data scientist. And I like the way that you put it.
You said these are like the cartoon versions of big mathematical ideas. How much math do you need to
know to call yourself a data scientist? Like if I studied just programming, I'm not a data scientist.
So I studied just math. I'm not a data scientist. Like what's the story there?
Data science is a funny thing in that there are as many different jobs called data science as there
are data scientists. So there are people who will call themselves data scientists and they write SQL
queries all day. There are people who call themselves data scientists and they do cutting edge machine
learning research all day. There are people who do data scientists who convince people to click it at.
Like you could know almost no math and still get a job where you call yourself a data scientist.
You could know very little programming and get a job where you call yourself a data scientist.
It's a field that's still kind of figuring itself out. And there's just such a breadth of the different
roles that are all calling themselves data science. In an ideal world, you would know, you know,
linear algebra, you would know probability, you would know statistics. But among people who are data
scientists, some people know that stuff really well. Some people know that stuff not so well.
Yeah, I can imagine. It's really about you've got a lot of data or some specific type of data and you
want to answer questions about it or even discover the questions you could ask that nobody's asked, right?
SnapCI is a continuous delivery tool from ThoughtWorks that lets you reliably test and deploy your code
through multi-stage pipelines in the cloud without the hassle of managing hardware. Automate and visualize
your deployments with ease and make pushing to production an effortless item on your to-do list.
Snap also supports Docker and M browser debugging, and they integrate with AWS and Heroku.
Thanks SnapCI for sponsoring this episode by trying them with no obligation for 30 days by going to
snap.com/talkpython
Sometimes it's actually the opposite, which is, I have a question I want to ask. Where can I find some
data that will allow me to answer that question? So, you know, sometimes you start with the data and
go to the questions. Sometimes you start with the questions and then you got to find the data.
Right. Okay. Very interesting. So that actually brought you to your next section that you talked
about in your book, which was getting data. And so what are the common ways that you talk about there?
Nowadays, there's a lot of people who just like post interesting data sets. So if you go to like
Kaggle, which is a site that does data science competitions, every one of their competitions has
a data set, which might be interesting in ways that are not related to the competition. The government websites
publish all sorts of data, some of which is potentially interesting. If you like weather,
they publish weather data. If you like economics, they publish economic data. There's a lot of
Python libraries for scraping websites. So even if a data set is not available, you can always go out and
try and scrape it and collect it yourself and clean it. My go-to source is always Twitter. I always build
things using Twitter data. So Twitter and all the other sites will have APIs where you can just make
restful calls or even use libraries. Python has some Twitter libraries that abstracts all that away. And you can,
you know, collect tweets on a given topic or collect tweets from certain users and
collect your own tweets and do analysis on those.
Yeah. And you had a section about that in the book. What was the package you were using for that?
In Python, the package I usually use is called Twython. There's a bunch of them. That's the one that I
got to work the easiest, but I think highly of it.
I don't think I believe any other ones.
Yeah. Yeah. Okay, cool. Yeah. I think Twitter is fairly special among the social networks.
To me, Twitter is the social network of ideas more than it is of friends or family or whatever. So
yeah, pretty cool. I love to get data from Twitter as well.
Yeah. It's just that they won't give you the fire hose, but for most things, they'll give you
more tweets than you can handle anyway. So if you want to find out what people are saying on a certain
topic or just what people are talking about in general, it's an awesome source for that.
Yeah. I saw some study or heard about some study a few years ago where people were studying, they were
trying to do sentiment, mood analysis on Twitter, and then trying to tie that back to the stock market
and predict how people were feeling on Twitter to near-term changes like in the next hour in the
stock market, which I thought was a pretty interesting project.
So ideas you can come up with using that data are pretty much endless. Every month, I think I'm done
with it and then I think of something else to do.
Nice. So then you kind of get into the topics that I think of as traditional data science,
machine learning, neural networks, network analysis, that kind of stuff. And I thought maybe we could go
through each section and you could tell me what it is that you need to sort of fundamentally
understand what are the from scratch basic pieces of each part of this data science and then maybe
some examples of problems you might solve with it.
Sure.
Yeah. So the first one you talk about is machine learning. And when you said machine learning
and later neural networks from scratch, I'm like, wow, that's a pretty big thing to take on from
scratch. What's the story there?
Machine learning at a high level is just learning some kind of model from data rather than sitting
down and writing out the model yourself by hand. And so if you have a small amount of data, then
you know, coming up with an algorithm that's going to learn a model from it is actually a pretty
reasonable thing to do if you go with a simple model and don't add too many bells and whistles.
It's only when you want to, you know, start producing recommendations at Netflix scale
or when you want to start, you know, building something to recognize speech patterns from audio files
that your beautiful handcrafted Python is probably not going to be up to the task.
Yeah. So I suppose the types of things you solve with machine learning is fairly unbounded. There's
a lot of problems that machine learning answers. What are some of your favorite examples?
Everything out there is machine learning these days. But if you want to talk about like projects that
I've worked on for fun, one time I built a classifier to predict or to identify hacker news articles that I would be interested in or not
interested in. So it would take kind of the feed of new hacker news stories and come up with a score between zero and one about how interested, you know, it thought I would be based on some initial seed values that I leave.
How did you teach it?
I was for a few other kind of idiosyncratic features I threw in, like, you know, is it an ask hacker news? Is it a show hacker news? Does it have a dollar
amount in there? Because there are a lot of kind of bad stories that's like, here's how to make $5,000, I think. So that was a good negative signal.
It's sort of a spammy signal.
Yeah. So, and then it turned out I wrote a blog post about it and someone posted the story to hacker news and then
the hacker news community got very angry that someone would think that not every story there was worth
reading. Because of course every story there is worth reading. So why would I want to filter some of them out?
They accused me of wanting to live in a bubble.
And yeah, they can be fairly critical on there. But that's funny. So the big question is, did your system
like your article, your blog post?
That's a good question. I don't know that I actually checked. I should go back and try and find that.
It'd be funny if it recommended it to you. Or not. Either way, it would be funny to know.
Well, you know, it's funny speaking of this. So, you know, I have an Android phone and so
Google now will sometimes recommend me articles of things I'll be interested in.
And occasionally it will recommend to me like my own blog posts, which I guess means doing a good job.
You know, one of the things I did with machine learning was I have a five year old daughter and I do her clothes shopping.
And I noticed that little boys clothes were very interesting and little girls clothes tend to be
kind of like really boring. The boys clothes have like dinosaurs and rockets and robots and
girls clothes have like hearts and flowers. And so I built a machine learning model to take an image of a
children's t-shirt and predict whether it was a boy shirt or a girl shirt.
Awesome. Yeah. And I agree with the classification there as well. I have three daughters and we still
bought a fair number of boy clothes for them.
Yeah, I did the same.
Nice. So the next topic that you covered was nearest neighbors. And I mean, conceptually,
I kind of know what nearest neighbors are pretty easily, but it's a computation hard problem,
especially in higher dimensions. And so what kind of stuff do you do? What kind of problems do you solve with
this?
One thing you can do is when you don't have like a great parametric model for what's going on.
So for instance, one place where I've seen this applied is when I have some kind of like
time series type signal, and I want to know if it represents some kind of bad anomaly. And it might be,
you know, like thousands of points in some weird shape. And I don't have a great way to classify it.
But one thing you can do is if you have a bunch of labeled signals, you know, some are good or some
are bad, you just say, I'm just going to take my set of reference signals and figure out which are
the ones it's closest to. And that way, I don't need to necessarily have a model of what's a good
signal or what's a bad signal. I can just say, I have some labeled data, that's enough. And so I
think that's kind of the situation where it can be useful.
Yeah, nice. When you're still trying to explore things, and maybe you don't, you don't have a model
you're trying to fit it to, you're just trying to understand it, right?
Yeah. Or think about where like, you have some weird kind of shapes, or weird kind of patterns
that you can't really put math behind. What's a good pattern? What's a bad pattern? But you have
some labels, then you could use nearest neighbors to basically classify without having to have a
mathematical model behind it.
Okay, yeah, very cool. So then you get to another topic called Bayesian
Bayesian analysis, which the context I know this is around kind of determining spam and
filtering and stuff like that. But what's, what's the story of Bayesian analysis?
So it's named after Bayes' rule, which is just a theorem in statistics, having to do with ways of
reversing conditional probabilities. So at a high level, if you have some knowledge about
what is the probability of seeing certain features in email, given that an email is spam, you can use that
data to produce estimates of what is the probability that an email is spam, given that you see those
features. And so typically, if you have a lot of, you know, spam and non-spam, you can make estimates
of, okay, given that an email is spam, it's likely that I'll see Viagra, and it's likely that I'll see
Gary Twick. And then basically just a classifier that turns that around and allows you to now look for
these features. And in a math-manipede rigorous way, come up with the probability, okay, here's the
probability that this is spam. Okay. Yeah. Very interesting. What other types of problems?
Spam is certainly easy to understand, but you know, hopefully that's solved by Gmail and other people
for us, right? I think it was Paul Graham who wrote a pretty influential article about this approach,
probably about 15 years ago now. And so I think a lot of the tools that, I don't know if it's what
Gmail uses, but that a lot of these BAM assassin and some of the other mail providers used are still
based on this principle of naive base. But basically the, anything where you have kind of big clumps of
say text, and you want to classify it into one or more classes, like two or more classes, and you're
willing to make this really big kind of technical assumption that the words in it are kind of independent
of each other. So you're treating them kind of like a bag of words rather than as a sequence of words,
then that's where you would use this kind of model. Okay. Yeah, cool. So maybe the opposite of that,
where you treat words sort of as, as having more meaning is in natural language processing, which is
another thing you talk about, right? Natural language processing is, is a huge field. Like people,
there are textbooks about it. So in fact, there are Python books about it. So cram it into a chapter of a
book is really just kind of, here's a really high level overview. And here's, you know, a couple of
examples to give you a flavor. Natural language processing is, it's actually relevant to this sort of
problems that I'm getting into in artificial intelligence. It gets used a lot by all the voice recognition in
your phone, and a lot of these new, basically reading texts, and having a computer extract information from it. So it's a
pretty important, pretty, pretty rich field. Yeah. And there's some pretty decent Python libraries for
doing that, right? Yeah. So there's a, so the big one is called NLTK, the natural language toolkit,
I think it stands for. And so it has its own book just about natural language processing in that library.
And I think that's actually free on the web. But yeah, that would be a great place to start it. If you
want to come to understand more deeply, there's also a nice Coursera course on natural language processing.
It took several years back that I liked. Oh, okay. Yeah, excellent. So let's see. Another topic you
covered were decision trees. And what's the story of these? A decision tree is kind of what it sounds
like. Yeah. You know, intuitively, you might have a model of making decisions based on a tree. So I have,
I want to know whether to buy a certain car or not. So, you know, I might start asking a question. Okay,
is it an American car or a Japanese car? American car. Okay. Go to the next question. Does it have
four doors or two doors? It has two doors. Okay. Is it gas or diesel? Diesel. And then you just have a
sequence of questions to ask and you classify it based on those questions. Okay. It's diesel. That
means, yes, I want to buy it. And so conceptually, you know, a tree like that is not that complicated.
Interesting part is given some set of data, how do I build such a tree? And there, there's a variety of
algorithms, but the one that we talk about in the book, it is a pretty simple one, which is basically
around, okay, I have a bunch of data. It has a bunch of attributes. I can split my data on each
attribute. So I could say, okay, at this stage, I want to look at two door versus four door. I want to
look at Japanese versus American, or I want to look at gas versus diesel and which of those choices is
going to allow me to kind of really separate the good buys from the bad buys the most. So if gas versus
diesel totally splits where diesel is the buys and gas is the don't buys, that's a great thing to choose.
If gas versus diesel splits where, you know, on gas, I want to buy half and not by half and on diesel,
diesel and by half and up by half. That's not a good split to choose because it doesn't really
help me at all. And so the mathematics here are just around kind of making this precise with this
entropy and building these trees from the data. Okay. Yeah, that's very cool. The way where you
build them knowing the data intimately, you know, that, that makes a lot of sense. And you don't really
necessarily need data science to do that, right? That's just helping, you know, asking a few questions
and making a decision. But the reverse, I think is pretty interesting.
They build some of these expert systems in this kind of way. I think they apply them sometimes in like,
uh, medical diagnosis and sometimes they do better than doctors.
Wow. Okay. Sometimes just let the data talk, huh? Rather than intuition.
This episode is brought to you by hired hired is a two sided curated marketplace that connects the
world's knowledge workers to the best opportunities. Each offer you receive has salary and equity presented
right up front, and you can view the offers to accept or reject them before you even talk to the company.
Typically candidates receive five or more offers within the first week, and there are no obligations
ever. Sounds awesome, doesn't it? Well, did I mention the signing bonus? Everyone who accepts a job
from hired gets a thousand dollars signing bonus. And as talk Python listeners, it gets way sweeter.
Use the link hired.com/talkpythontome and hired will double the signing bonus to $2,000.
Is knocking. Visit hire.com/talkpythontome and answer the call.
Somewhat related to that maybe is, you talked about neural networks as well.
Yeah. So neural networks are a super hot topic right now, because I'm sure you've probably heard
that all the buzz about, or about deep learning, right? And deep learning tends to have neural networks
at the root of it. So neural networks are basically a way of building up kind of layers and layers of
representations. And the deep learning, this book doesn't really go into deep learning because it's,
the single neural network is hard enough to do by hand or from scratch. But basically it's a way to
kind of build a classifier that works similar to how a toy model of a brain might work. So you have
artificial neurons, and each neuron has a bunch of inputs that go into it with weights. If the weighted
sum of the inputs exceeds some certain threshold, the neuron fires. And if it doesn't exceed that
threshold, the neuron doesn't fire. So you present an input, which could be like a, an image. So basically
a bunch of zeros and ones, and that causes some neurons to fire. And that propagates through this
network. And in the end it will spit out, you know, I think you should be an image of a cat,
or I think you should be an image of a dog. And you train it by showing that a lot of labeled images
and adjusting the weights based on how you got them wrong.
Yeah. Interesting. So you basically just say, these are the inputs and the decisions that you feed it
known data. You say, you know, like, here's a cat, here's a cat, that's a dog. It's just a cat or a dog,
right? And then you ask that question. I was gonna say, well, one of the problems that I sort
of remember from neural networks is that they, as you try to get them more accurate,
they get over-trained to just only do little bits of stuff. And so how does things like decision
forests or these things and this deep learning, how does it deal with that?
So random forests are basically taking multiple decision trees and combining them. So there's this
kind of general principle where a lot of times rather than building one model, it's like super predictive.
I take a lot of much less predictive models and just kind of let them vote on things or average
the results or whatever. So you're right that it's really a decision tree. And I included like all
hundred features in my data set that there's a good chance I'm going to like overfit and
overlearn my training data and not generalize outside of that. So one thing that happens with decision
trees is people often don't use the bare decision trees. They use the random forest where they'll build
a bunch of smaller decision trees, each of which is really restricted to a small subset of features
so that each one is individually less powerful. But then when you combine them, they do well in the aggregate
and they don't have necessarily the same overfitting problem that single decision tree with lots of features would.
So in neural networks, especially in deep learning, they're not exactly the same techniques, but other
techniques where you will zero out some of your weight sometimes and train without them to make
sure that they don't learn too much. And there's a lot of other techniques that get used in order to
make sure you're not overfitting. There's something that, you know, data scientists and machine learning
people worry about a lot.
It's a really hot field, like you said right now. That's awesome. So one more section I'd like to touch
on is you talked about this thing called recommender systems.
So mobile, they're just what they sound like. Anyone who's used the internet has a pretty good experience
where you go to Netflix and it says movies for Joel and it's trying to predict what it thinks I'll like.
Or you go to Amazon and it will recommend items for me. And you can go a little further where you have
all these startups like Stitch Fix is a very hot data science startup where you tell what kind of clothes
you like and they'll send you a box of clothes every month that they think you'll like a lot.
And so generating these kind of recommendations is a pretty popular task within data science. A lot of data scientists
work on these kind of problems. There's a lot of data scientists work in this job. So they have to sell you stuff
and they always want to sell you stuff that they think you'll like.
You convert better if you figure out what people actually might want, right?
Yeah, I mean, well, if Amazon sends me an email, "Hey, Joel, you know, these five things are on sale
and they're five things that I really want to buy," then that's much better for them and potentially
for me than if they send me a random email that's like, "These are the five most popular things on
Amazon today." Yeah, it definitely is better. I really like going to my Kindle and looking at
the recommended things based on what I've been reading. But the Netflix recommendations, I know they
do really great work, but it just doesn't work for me because my seven-year-old daughter watches
Strawberry Shortcake and other random things. I get a lot of kid shows recommended to me.
They have a "Who's watching" button that you can click and say "A kid is watching."
I know, but my daughter won't use it. She'll just randomly pick one.
Yeah, so you know, it's funny. I use that button pretty well, but my Netflix recommendations are also
not that good. But I think it's just because I don't like anything on there. So no matter what they're
telling me, I'm not going to like it. Yeah, I hear you. I feel like you covered a pretty
wide swath of data science from scratch. Some of the topics were really accessible. Some of them
required more math and more thinking, but it was all a really nice presentation. What do you feel like
you left out? There aren't any topics that, if anything, I kept adding stuff while I was writing it.
So I don't feel like there were any huge topics that I necessarily left out. But in terms of coverage,
probably my biggest regret is that I used Python 2 instead of Python 3.
Yeah. Okay. And I saw on your GitHub repo, you had something about Python 3 in there. Is that right?
Yes. So pretty much as soon as the book came out, one, I got a lot of emails from people saying,
"Hey, why do you not use Python 3?" And then I also got a number of emails saying,
"I would like to use Python 3. Will the code work?" And so I wrote them back and I said,
"Yeah, you know, I don't feel like the code shouldn't work. Give it a try. I bet it works
with probably a few changes, add some parentheses, print statements and so on." And then eventually one
guy wrote me back and he said, "It doesn't work." I said, "Okay." So I sat down and I said, "I'm going to
convert the code to Python 3." And it took me about, I'd say four to five hours. And that's with me
knowing the code intimately. So it would have taken someone who hadn't written the code in the first
place a lot longer than that. And then I felt kind of guilty that I've been telling all these people that
it was so easy to do when it wasn't.
Just spend a week. It'll be fine.
Yeah. But so yeah, I have the Python 3 versions of the code up on the GitHub. I sort of regret not
having just done it that way in the first place.
Yeah, sure. So at the end, you talked about data science, not from scratch, and you pointed out
a lot of the libraries you might actually use, like NumPy and so on. Do you want to talk about that
really quick? Like what's the real data science versus the from scratch data science comparison?
Yeah. So I would say that NumPy is pretty fundamental. That's basically the linear algebra
library for Python. So it provides you matrices, matrix algebra, high performance arrays, things
like that, you don't get just built in. And you might not use it directly, but a lot of the other
libraries are really built on top of it. So kind of the most broadly accessible machine learning
library for Python is called Scikit Learn. And it has really nice documentation and really nice tutorials
and a fairly standard API for building machine learning models. Anything you want to build a
regression model or a random forest model or any kind of classifier, that's probably the place to go.
There's also Pandas, which is the data frame library, which is good if you're working with tabular data. So
not necessarily the machine learning side of data science, but more of the, I have kind of a
spreadsheet data set and now I want to clean it and aggregate it and pivot it and look for kind of data
analysis type insights.
Yeah. If you're exploring, like you said, tabular data and you kind of kind of load it up and clean it,
Pandas seems really fantastic for that.
It's a really nice library. And then kind of the new kid on the block is a TensorFlow, which is
Google's deep learning library. And it's only was released, you know, a few months ago and it's gone
through. It's not 1.0 yet, but it seems like people are sort of converging around it as that's how they're
going to do deep learning in Python. Now there are other sort of previous libraries that some people
used and still use, but TensorFlow seems to be gaining a lot of mindshare.
Okay. Yeah, that's really cool. And I've definitely seen TensorFlow talked about a lot in this context,
but it's just a library. You can download that and run it locally. It's not like a cloud type thing,
right?
Yes. So currently it's a local library and I'm not sure if they've come out with a version that you can
kind of do your own cloud, like on various AWS instances. But, but I know they're definitely going
in that direction because a lot of this stuff, these deep learning models take a really long time to
train. And so you want to go to use them for anything serious. You want to distribute them and
throw them in high-powered machinery and not just run them on your laptop.
Yeah, of course. If you have tons of data, maybe it's better to get a bunch of machines
for an hour. So what do you think about these cloud learning or data science platforms? I'm thinking like
Azure machine learning, or, you know, I just had SigOpt on the show a few shows ago. I'm not really sure
what else is out there in terms of like go out to the cloud and grab some data science stuff.
What do you think about that?
I haven't spent much time looking at any of those. I think they can add value in terms of either one,
if you don't have the data science or machine learning expertise in house to do whatever you need to do.
Or two, if you have some kind of model that you've built, but you need help either putting it into
production or operationalizing it somehow, that they can build a pretty good role there. But my sense is
that most people doing data science are aligned more on running the libraries and running the models
themselves. But that could be just my bias sample of the people who I talk to.
Yeah, of course, of course. Cool. So another thing that you said you're into is taking some of the
ideas from Haskell and thinking about how those might manifest in Python. What do you went up to there?
Haskell, if you're not familiar with it, is kind of the purest of the pure functionally typed
languages with strong types and lazy evaluation and things like that. And so once I spent some
time in that world, I spent a lot of time thinking about how can I bring some of these concepts back
into Python. And in Python 3, lazy evaluation plays a much bigger role in the sense that like range
is a generator instead of a list and all the map and filter and things like that also are generators
instead of lists. But I started getting into the iter tools library, which starts giving you tools for
generating basically infinite sequences and just trying to see how far I could go using
pure functions and infinite sequences and avoiding mutable variables and other things that you try not
to do when you're working in a Haskell-like language.
Yeah. And how did you feel like it came out in the end? Do you feel like you were able to bring a lot
of those ideas over?
I was, and I ended up producing code that was really neat and really impenetrable. The lazy
infinite sequences stuff, that was more almost academic in terms of like, yes, I managed to do it. Like,
this is mathematically interesting and it works well, but it's not readable at all.
So imagine you wanted to represent, let's say a binary tree in Python. Kind of the two,
I would say obvious approaches would be one to make some kind of like class where it has a,
you know, a value element and a left element and a right element. And then you also might just use
a dictionary to represent it where it had those keys. In a language with algebraic data types,
like Haskell, you would just basically represent that kind of tree as a product type where it just
has like three elements. And so I said, you know, what if I just represented a tree as a tupple with
three elements where first element is the left subtree, the second element is the value,
third element is the right subtree. And similarly, if you want to do like linked lists in Python,
which you probably don't want to do, but if you did want to do linked lists in Python, you just treat
them as a tupple. First element is the element and the second element is the tail linked list. And so,
I actually found that I was able to write some pretty nice code using those kinds of ideas.
And I did some coding interviews that way. I'm not sure the interviewers appreciated it.
What is this guy talking about? Yeah, cool. So what question given your work at the AI Institute and
your background in data science and so on, I wanted to ask is the last year or so, there's been a lot of
news items and people coming out saying that artificial intelligence is a danger to humanity.
What's your thought? Is like AI something we should be super excited about? Or is it something we should
be maybe cautious about? I would say I probably fall in the middle. I mean, I'm excited about it because
this is my job, but I don't go around encouraging everyone else that they have to be excited about it
because I don't know that that's necessarily warranted. But at the same token, I don't spend a lot of time
worrying about how dangerous it is. I think we're pretty far off from the time when we have to worry
about that. And I do have some friends who think we should worry about it now before it's too late. But
I think there's a lot more important things to worry about in the world when I read the news.
Yeah, I kind of agree with you on that. And I think there's two, certainly two ways to look at that. On
one hand, if you think about things like self-driving cars, let's just take that as an example. Like,
I believe one of the biggest job categories for men in the United States is some form of driving,
like driving a truck, driving a taxi, those types of things, delivery vehicles and so on. And if self-driving
cars were to like remove all that, like that would have large social effects. But I think, you know,
that's not so much the way that people, at least recently in the news, were talking about it. It's
more like Terminator style, right? And so that I, I'm not too worried about this personally. Who knows?
Yeah. I mean, I personally would pay a lot of money for a self-driving car because I,
I don't like driving that much. And I'd much rather be able to read while I'm going somewhere.
Yeah. Driving is fun until you get stuck on i5 for half an hour inching along. Then you know,
I don't like driving anymore. Exactly.
Awesome. Well, we're kind of coming up near the end of the show. Let me ask you just a
few closing questions. I always ask on my guests, if you're going to write some Python code,
what editor do you open up? So these days I tend to use Adam for pretty much everything,
memory leaks and all. Nice. Yeah. That's from a GitHub, right?
Yeah. It's pretty similar to sublime, but it doesn't nag you for money. So yeah, it's,
it's pretty nice. And I think it's Adam.io. They have a really cool little video about how
it's the editor of the future. It's nice.
Yeah. I don't know if I go that far, but it's the editor of the present at least.
Yeah. It's like a George Jetson sort of like a promo video. It's, it's pretty funny.
It is pretty nice. Isn't it? Does it have good Python support?
You know, I'm never, I'm not someone who leans on like ID functionality a lot. So if good Python
support counts as syntax highlighting, yes, but that's all I tend to use it for. So yeah.
Yeah. Yeah. Okay. Cool. And if you look at on the Python package index, there's,
you know, 75 plus thousand packages and we all have experience with, you know, different parts
of it and there's things that we love and would recommend, like, what is your favorite one you
might recommend that people maybe don't know about?
So the one I recommend that people don't necessarily know about is called Beautiful Soup. It's,
basically a HTML parsing library. And so if you start scraping data from webpages, you're going to get
a big mess of ugly HTML. That's probably not even well formed. most of the time,
most people don't bother to well form their HTML.
Are you telling me that I can't just like load that up as an XML document or something like this?
No, I'm just kidding. Of course it's, it's terrible trying to work directly on the web,
right? And Beautiful Soup is really, I really like it as well.
It's really nice. I mean, you have to spend a little bit of time getting used to
it's API and interface and everything, but it's super handy for getting data out of webpages and
doing anything where you have to do a bunch of scraping.
And you cover that in your book, right? In the getting data,
I use Beautiful Soup a little bit and show people how to use it.
Yeah, I cover it a little bit. It's a nice addition to the data scientist toolkit.
Nice. So Joel, how do people find your book? Amazon? Just a Google search for data science
from scratch? Yeah, it's on Amazon and you can buy it from O'Reilly.com. But yeah,
if you Google data science from scratch, you'll find it.
Cool. And I'll put a link to the GitHub repo where you have all the code examples and so on as well.
It's been really fun to talk about data science. And I think you have a really interesting way of teaching
people to appreciate the tools that we're all fairly familiar with by showing you how to build it from
scratch. So thanks for that.
My pleasure.
This has been another episode of Talk Python to Me. Today's guest was Joel Gruse and this episode
has been sponsored by SnapCI and Hired. Thank you guys for supporting the show. SnapCI is modern,
continuous integration and delivery. Build, test and deploy your code directly from GitHub,
all in your browser with debugging, Docker and parallelism included. Try them for free at snap.ci
slash talk.com. Hired wants to help you find your next big thing. Visit Hired.com slash talkpython to me to get five or
more offers with salary and equity presented right up front and a special listener signing bonus of $2,000.
Are you or a colleague trying to learn Python? Have you tried books or videos that left you bored by just
covering the topics point by point? Check out my new online course, Python Jumpstart by building 10 apps at
talkpython.fm/course to experience a more engaging way to learn Python. You can find links
from this show at talkpython.fm/episodes slash show slash 56. Be sure to subscribe to the show. Open
your favorite podcatcher and search for Python. We should be right at the top. You can also find the
iTunes feed at /itunes, the Google Play feed at /play and the direct RSS feed at /rss on
talkpython.fm. Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
You can hear his entire song at talkpython.fm/music. This is your host, Michael Kennedy.
Thank you so much for listening. Smix, take us out of here.
Stating with my voice. There's no norm that I can feel within. Haven't been sleeping. I've been using lots of rest. I'll pass the mic back to who rocked it best.
I'm first developers.
Developers, developers, developers, developers.
.
Thank you.