Skip to main content
Colin Jaffe/4 min read

Tuples: Immutable Collections in Machine Learning

Tuple Use Cases in ML

DataFrame Shapes

df.shape returns (rows, cols) tuple — immutable and reliable.

Image Sizes

(height, width, channels) — common for image processing.

Train/Test Split

train_test_split returns 4-tuple of arrays.

Hashable

Tuples can be dict keys; lists cannot — useful for caching results.

Master Machine Learning at Noble Desktop

Noble Desktop's Python Machine Learning Bootcamp covers scikit-learn, Keras, and applied ML.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

Use tuple unpacking in Python to extract multiple values from immutable tuples efficiently. Watch this tutorial to learn the key concepts and techniques.

Let's talk about tuples. We'll see this kind of value occasionally in machine learning, because sometimes it's a useful kind of value to get back, and we need to know how to work with it. So a tuple, which is what the mode returns to us, this mode result value here, a tuple is an immutable collection of numeric values in parentheses.

So it's actually two values, right? It's our mode number, in this case, 85. And it's our count number, in this case, two. And it's pretty useful when something wants to give you multiple values.

We don't want to just know the mode number. We also want to know how often it's occurring. So let's take a look at what that looks like in real life.

If you want to create a tuple, which you don't do very often unless you yourself are writing a library, or perhaps a complex function, to return a tuple, to create a tuple, you might say instructor, I'm just going to write some, make some tuples here as an example. Instructor info equals a string, "Colin Jaffe, " and parentheses allow us to put multiple values in here, similar to the square brackets for a list. And then we can put my age, 44.

Now, if I look at instructor info, instructor info, it looks just like what I, what I put in there. But we can also look at instructor info at index zero, just like with a list. It's a string colon Jaffe.

And index 1,44. Now, one of the special abilities, the reason it's immutable, we can't actually change a tuple. And that actually is because most of the time, when somebody is, oh, I'm accidentally editing this.

I'll delete it. I'll fix it in post. When somebody is giving you back a tuple, you're not really there to work with that tuple.

It's just to give you back a couple of different values. So, it's good that it's immutable, so you don't accidentally change the value that this thing gave you. We don't want to change the mode, right? Why would we change the mode? So, it's protected from that by being a tuple.


So, even though I can evaluate instructor info one, I can't change my name, my age. Oh, if only I could go back to the young age of 35. We get an error if I try to do that.

'tuple' object does not support item assignment. So, that's the way in which a tuple is immutable. Now, if we want to access it, it's kind of annoying to do something like this.

If we wanted to say, like, name equals instructor info at index zero, and age equals instructor info at index one. I mean, we could do that. That's how we could save these values.

But it's kind of annoying. And you have to sit there and count the indexes. So, Python gave us a great way to handle this, which is called tuple unpacking.

I could say name comma age equals instructor info. And that will separate out the two values from instructor info into the variables name and age. And we can see that print name, print age.

Colin Jaffe 44. And the order matters here, of course. If I switch these values, age and name, well, now name is 44.

And age is Colin Jaffe. That's incorrect. So, we want to make sure that we're doing the order correctly.


And there we go. Now, let's unpack the values from our mode in the same way. We could say mode and mode count, perhaps, equals stats.mode of our grades.

Remember, stats.mode returns this tuple. Just like when you looked at mode grade up here from stats.mode grades. It gave us that tuple.

And we'll unpack it into the two values mode and mode count. We can print out mode is our mode. Nope, not mod.

Print "count is", mode_count. And if I run that, mode is 85, count is 2. We've successfully unpacked from that tuple. We'll be using tuple unpacking a couple of times in this course.

A few times, actually. One of our main ways of working with data will be to split it into different parts. And when we split it into different parts to work with different parts in different ways, we'll be getting back tuples to unpack.

So, it's good to understand this.