All language subtitles for 002 A Simple Example of Clustering_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian Download
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.