Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Narrator: Remember the first lecture of this section?
We gave an example about six countries,
USA, Canada, France, UK, Germany, and Australia.
Well, guess what!
This was not just for illustrative purposes.
In fact, we are going to cluster these countries
using K-Means in Python.
Plus, we'll learn a couple of nice tricks along the way.
Cool.
Let's import the relevant libraries.
They are pandas, numpy, matplotlib.pyplot, and seaborn.
As usual, I will set the style of all graphs
to the seaborn one.
In this course, we will rely on scikit-learn
for the actual clustering.
Let's import k-means from sklearn.cluster.
Note that both the K and the M in KMeans are capital.
Next, we will create a variable called data
where we will load the csv file,
3.01 Country clusters.
Let's see what's inside!
We've got Country, Latitude, Longitude, and Language.
Let's see how we gathered that data.
Country and Language are clear.
What about the latitude and longitude values?
These entries correspond to the geographic centers
of the countries in our data set.
That is one way to represent location.
I'll quickly give an example.
If you google geographic center of US,
you'll get a Wikipedia article indicating it to be
some point in South Dakota
with a latitude of 44 degrees and 58 minutes north
and a longitude of 103 degrees and 46 minutes west.
Then, we can convert them to decimal degrees
using some online converter
like the one provided by LatLong.net.
It's important to know that the convention is such
that north and east are positive
while west and south are negative.
Okay.
So, that's what we did.
We got the decimal degrees of the geographic centers
of the countries in the sample.
That's not optimal as the choice of South Dakota
was biased by Alaska and Hawaii,
but you'll see that that won't matter too much
for the clustering.
Right.
Let's quickly plot the data.
If we want our data to resemble a map,
we must set the axis to reflect the natural domain
of latitude and longitude.
Done!
If I put the actual map next to this one,
you will quickly notice that this methodology,
while simple, is not bad at all.
All right, let's do some clustering.
As we did earlier, our inputs will be contained
in a variable called X.
We will start by clustering based on location.
So, we want X to contain the latitude and the longitude.
I'll use the pandas method, iloc.
We haven't mentioned it before
and you probably don't know that
but iloc is a method which slices a data frame.
The first argument indicates the row indices
we want to keep while the second, the column indices.
I want to keep all rows, so I'll put colons
as the first argument.
Okay.
Remember that pandas indices start from zero.
From the columns, I need latitude and longitude
or columns one and two.
So, the appropriate argument is 1:3.
This will slice the first and the second columns
out of the data frame.
Let's print X to see the result.
Exactly as we wanted it.
Next, I'll declare a variable called kmeans.
Kmeans is equal to capital K, capital M,
and lowercase eans, brackets two.
The right side is actually the KMeans method
that we imported from sk-learn.
The value in brackets is the number of clusters
we want to produce.
So, our variable KMeans is now an object
which we will use for the clustering itself.
Similar to what we've seen with regressions,
the clustering itself happens using the fit method.
Kmeans.fit of x.
That's all we need to write.
This line of code will apply k-means clustering
with two clusters to the input data from x.
The output indicates that the clustering
has been completed with the following parameters.
Usually though, we don't need to just
perform the clustering, but are interested
in the clusters themselves.
We can obtain the predicted clusters
for each observation using the fit predict method.
Let's declare a new variable called
identified_clusters equal to kmeans.fit_predict
with input x.
I'll also print this variable.
The result is an array containing the predicted clusters.
There are two clusters indicated by zero and one.
You can clearly see that the first five observations
are in the same cluster, zero,
while the last one is in cluster one.
Okay.
Let's create a data frame so we can see things more clearly.
I'll call this data frame data with clusters
and it will be equal to data.
Then, I'll add an additional column to it
called cluster equal to identified_clusters.
As you can see, we have our table
with the countries, latitude, longitude, language,
but also cluster.
It seems that the USA, Canada, France,
UK, and Germany are in cluster zero,
while Australia is alone in cluster one.
Cool!
Finally, let's plot all this on a scatter plot.
In order to resemble the map of a world,
the y-axis will be the longitude
while the x-axis, latitude.
But that's the same graph as before, isn't it?
Let's use the first trick.
In matplotlib, we can set the color to be determined
by a variable.
In our case, that will be cluster.
Let's write c=data_with_clusters of Cluster.
We have just indicated that we wanna have
as many colors for the points as there are clusters.
The default color map is not so pretty.
So, I'll set the color map to rainbow.
Cmap equals rainbow.
Okay.
We can see the two clusters.
One is purple and the other is red.
And that's how we perform KMeans clustering!
What if we wanted to have three clusters?
Well, we can go back to the line where we specified
the desired number of clusters
and change that to three.
Let's run all cells.
There are three clusters, as wanted.
Zero, one, and two.
From the data frame, we can see that USA and Canada
are in the same cluster.
France, UK, and Germany in another.
And Australia is alone, once again.
What about the visualization?
There are three colors representing the
three different clusters.
Great work!
It seems that clustering is not that hard after all.
In the next lesson, we will cluster the observations
based on a categorical feature.
Thanks for watching!
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.