In this lesson we're gonna be looking at variables again, but only this time these variables are going 1
to be containing lots of data, instead of just a single number or a single piece of text. 2
In other words, we're going to introduce you to collections. And by by collections we mean lists, arrays, 3
data frames and series. 4
In the last lesson we learned about how to store data inside variables and we learned about data types. 5
In machine learning and data science, 6
we often need to work with, 7
well a lot of data. 8
As such it would be silly to put each piece of data inside a separate variable. 9
The alternative to assigning a single value to a single variable, like this, is that we have a single 10
variable that actually contains lots of data and that variable will represent a collection. One of the 11
most common types of collections that you'll encounter is the list. 12
Lists are pre-built into Python and they're part of the language. 13
Let me show you what a list looks like. 14
I'm going to create a list called 'primeNumbers' and set it equal to a collection of 5 primes. 15
And the way to do that is by using the square brackets syntax. 16
I'm going to open a square bracket and then give my first value - 3, then put in a comma, give my second 17
value - say 7, 18
another comma, 19
third value - 61, comma 29 and 199. And, just like that, we've created 20
a variable called primeNumbers that holds onto a collection of five values. 21
Now, just out of curiosity, let's take a look at what the type of the prime numbers variable is. 22
type(primeNumbers), Shift+Enter and we see that it's a list. 23
Now one thing that I'll mention is that a list in Python, you know, doesn't have to be numbers, right. 24
A list can contain all sorts of data. 25
For example, here's what a list of strings would look like. 26
coolPeople = [] 27
'Jay Z', 'Gandhi', 'me' 28
There we go. 29
The important thing to note is how we substituted Jay-Z for Kevin Spacey in this list. 30
In any case, a list is just a variable that holds on to a bunch of values and it could even hold on to 31
values of different types. 32
Check this out: primesAndPeople = [] 33
'King Arthur', 17, 11, 'Jennifer Lopez' 34
Now even though we've got three different lists; one with whole numbers, one with text and one with a 35
mix of different types of data, all of these three variables are actually of the same type - they're of 36
type list. 37
So now that we've covered the syntax for creating a list, I'm going to show you something even cooler 38
than storing data and lists - and that's retrieving data from a list. So the question is how do you pull 39
out a particular value from one of these lists? 40
Here's the secret. 41
The way we grab a particular item from the list is by that item's index. 42
What do I mean by index? 43
The index is just the item's position in the list. 44
Let me show you what I mean. 45
If I type primeNumbers, and open the square brackets; between these two square brackets I can put a number 46
and that number is the value of the index. 47
So, say I put in the number two and hit Shift+Ente,r I get back the value 61, but 61 is the 48
third item in this list. 49
Why is it the third item? 50
Well it turns out programmers really, really, really, really like to start counting from zero. 51
In other words that first item, number 3, is at position 0, 7 is at position 1 and 61 is at position 52
2. 53
Now oftentimes what we're gonna be doing is storing the value that we pull out from a list in another 54
variable - so we can do something like this" 55
bestPrimeEver = primeNumbers[4] 56
In this case we're pulling out one of the numbers from the list and storing it in a variable called 57
bestPrimeEver. 58
Now, I know you might have a different favorite prime number too, but I'm sorry to say it's not going 59
to be as good as 199. 60
Let me give you another example of a list. 61
Say we had a list of eggs or egg objects and we want to pull out the egg at index 1, which egg 62
would we be pulling out? 63
Which egg is my egg equal to? 64
The answer is we would be grabbing the second egg in the list. 65
The spotted one. 66
Again, this is because programmers start counting from zero. 67
Now the reason I'm belaboring this point so much is because forgetting to count from zero is super common. 68
And these off by 1 errors will also crash your Python program. 69
Check out what happens when we write primeNumbers[5] and hit Shift+Enter. 70
We get an error that reads "index out of range" and that's because we're trying to select an item beyond 71
the length of the list. 72
Our list has five items and we're trying to select the item at position 0, 1, 2, 3, 4, 5. The item at position 73
5 is the sixth one on the list. Since there is no sixth item, our Python program goes down in flames - "index 74
out of range". 75
So if you ever see these cursed words in your crash logs, now you know why. Let's change that five back 76
to a four. Now lists are pretty common in Python and we'll be working with them a lot. 77
However, we'll be encountering quite a few other kinds of collections too and a lot of them are very, 78
very similar to the list but they are classified as a different data type because there are subtle differences. 79
In scientific computing and machine learning, 80
we're going to be working with another data structure very, very often - namely the array. An array is like 81
the list's step brother. 82
He's somewhat similar to its sister, but he's got a different dad. 83
You see the Python list is like a ballerina. 84
She's flexible, friendly, versatile. 85
She can grow and shrink in size and she can hold onto different types of data like numbers and text 86
in the same list. The array on the other hand is a weightlifter. 87
He's strong and he's very good at particular tasks but he's less versatile. 88
He's also so buff that if he tried to punch you he couldn't reach around his own pecs, but that's beside 89
the point. 90
All the data in an array must be of the same type, so an array must be all numbers or all strings, 91
for example. Now, believe it or not, you've actually already encountered an array. 92
Remember how we were looking at the regression intercept when we were estimating the movie revenue? Below 93
the cell, 94
we saw the output array([-7236000]). 95
In this output, 96
you even see the word array so we know that we're working with one. 97
But let's make this more explicit. 98
Let's check the type of 99
regr.intercept_ under school and hit Shift+Enter. Now, if you see this error, 100
just like I do, it's because I haven't actually run any of the Python code in the prior cells so I'm 101
gonna go to Cell > Run All and my notebook will know about the regression intercept so that now it will 102
correctly evaluate this cell and what we discover is that the full name of this data type is 103
numpy.ndarray 104
The cool thing is is that working with arrays is actually very similar to working with lists. 105
In other words we can use that square brackets syntax. 106
So if I write regr.intercept_[0] I'm pulling out the first and only 107
value from our array. Hitting Shift+Enter, 108
I see this value printed below the cell. Up here, 109
in contrast, I was looking at the entire array, but in this cell I'm just looking at one of the values 110
inside of this array and I'm accessing this value with the square brackets notation providing an index 111
at value 0. 112
Okay, so we learnt about lists and arrays in Python. Both of them use the square bracket notation and 113
through both of them we can access individual items using an index. 114
The index is just the position of the item in the list starting from zero. 115
And this brings me to a very, very important point - namely do you know why the programmer quit his job? 116
It's because he didn't get arrays...