class: center, middle # Machine Learning ## Unsupervised Learning ### III-Verano 2019 --- class: center, middle # Unsupervised Learning --- class: medium # Unsupervised Learning * In Unsupervised Learning we give our algorithm data, and let it find "something interesting" * Examples: - Clusters of similar data: Similar players, cliques in social networks, similar sounding words - Common patterns of data: Attack or other action sequences used by many players - Related data items: Purchases often made together (with real or in-game currency), quests often chosen together, games played by the same people --- class: medium # Why Unsupervised Learning? * If we can find similar players, we can make them play together * Friends can be recommended based on player type/preference * Help new players by suggesting common actions/purchases made by similar players * Recommend new games to players * If genes "behave" similarly, they must be related or associated in a biological process --- class: center, middle # Clustering --- # Clustering * We are given `n` vectors, representing our players/games/words/... * How can we determine which vectors belong to the same "class"/"type"? * How many classes are there? * We call the classes *clusters* --- class: small # What is a Cluster? * For now, we assume that each of our clusters is defined by a single point, the *center* of the cluster * Each data point is then assigned to a cluster based on which cluster center it is closest to
--- class: small # What is a good Clustering? * Say we are told that we should create `k` "good" clusters * k-center clustering: Minimize the maximum distance of any data point from its cluster center (firehouse placement) * k-median clustering: Minimize the sum of the distances of data points to their cluster center * k-means clustering: Minimize the variance of distances of data points within a cluster (which is the average squared distance from the mean) * Each of these is a measure for how "compact" a cluster is, but that does not necessarily tell us anything about cluster "usefulness", which is application-dependent --- class: medium # k-Means Clustering * k-means clustering puts more weight on outliers than k-median, but is not dominated by them like k-center * Especially for d-dimensional vectors, k-means is usually the first choice * How do we find a k-means clustering? Try all possible assignments of data points to clusters * Finding an optimal clustering is NP-hard :( * Lloyd's algorithm! (Often also just called "k-means algorithm") --- class: small # Lloyd's algorithm * Determine `k` initial cluster centers, then iterate these two steps: - Assign each data point to its cluster based on the current centers - Compute new centers as the mean of each cluster * After "some" iterations we will have a clustering of the data * This may be a local minimum when compared to the k-means criterion, but is often "good enough" --- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- # Lloyd's algorithm
--- class: medium # Generalized Distance Functions * What if our data is not d-dimensional vectors, but e.g. all the data we have about each player * For any two players, we can calculate a distance, but we can't make up an "average value" * In other words, all we have are our data points and pairwise distances, but no vector embedding * We can still cluster, we just have the restriction that each cluster center must be exactly on a data point --- class: medium # k-Medoids Clustering * The cluster centers are called "medoids" * We use a variation of Lloyd's algorithm * The only difference is how we assign new cluster centers * One option: Use the data point that has the lowest sum of distance to the other data points in the cluster * Other option: Choose a new data point as the new cluster center, and check if that new cluster center would result in a better clustering (slower, but more stable) --- # Lloyd's algorithm vs. k-Medoids Clustering
--- # Lloyd's algorithm vs. k-Medoids Clustering
--- # Lloyd's algorithm vs. k-Medoids Clustering
--- class: small # We forgot something ... * We need initial cluster centers from somewhere? * Simplest approach: Just pick data points at random; Problem: Results may be poor/unpredictable * Another idea: Pick a data point at random, then pick the data point that is furthest away from it, then pick the data point furthest away from both, etc.; Problem: Outliers affect the initialization * Another idea: Pick a data point at random, and assign weights to each other data point based on the distance. Then pick the next center using these weights as probabilities, etc. * You can also use the result from any other algorithm/guess/heuristic as an initialization, Lloyd's algorithm will never make the solution worse (as measured by the k-means clustering goal)! --- # Ward's Algorithm * Start with each data point in its own cluster * Merge two clusters until there are only `k` clusters left * Which two clusters do you merge? The two such that the average distance from the cluster centers increases the least * This is basically "greedy" k-means --- # Heirarchical Clustering * A potential issue /disdavantage of k-means clustering is that it requires human input in defining the number of clsuters. * Hierarchical clustering however does not require an initial number of clusters. * The most common type of hierarchical clustering: bottom-up or agglomerative clustering. * It generates a dendrogram starting from the leaves and combining clusters up to the trunk. --- # Bottom-up Heirarchical Clustering * Clustering by constructing a dendogram
--- class: mmedium # HClust Construction * The algiorithm follows this simple process: 1- A dissimilarity measure is defined between each pair of observations, i.e Eclidean distance 2- It then assumes that eac h observation belongs to an individual cluster 3- The 2 most similar clusters are fused/merged so that there are n-1 clusters. 4- The other 2 similar clusters are fused resulting in n-2 clusters. * The process is repeated iteratively until all observations are part of a single cluster. --- class: medium # Dissimilarity * Although a simple algorithm, something was not defined, the dissimilarity measure between clusters * This is achieved with the concept of linkage. * There are 4 main types of linkage: complete, average, simple and centroids --- # Linkage
--- class: mmedium # Whcih one do I use? * Complete, average and centroid are the most commonly used * Single linkage tends to yield unbalanced dendrograms.
--- # Other Measures * Selecting the appropiate dissimiliraty measure is ipmportant. * Besides Euclidean distance there is also correlation-based distance. * This considers two features to be similar if they are highly correlated, meaning that they have similar profiles. --- class: mmedium # Correlation in Practice * In an example, an online retailer is interested in clustering shoppers based on their previous shoppoing history. * The goal is to identify subgroups of similar shoppers and show them ads to entice them to buy stuff. * Using Euclidean distance, low-buying shoppers will be gropued together. This is not ideal. * Using correlation-based distance methods shoppers with similar preferences (they bought items A and B, but not C and D) will be clustered together, even if they have bought of different volume of items. --- # Distribution-Based Clustering * Our representation of clusters as single vectors had the advantage of being simple * However, clusters sometimes have different sizes/distributions * So let's assume our clusters are probability distributions * Let's start with Gaussians --- class: mmedium # Why Gaussians? * Many datasets can be modeled by Gaussian Distribution * It is potentially intuitive to think that the clusters come from different Gaussian Distributions * With this notion it is possible to model dataset as a mixture of several Gaussian Distributions. * This is the core idea of GMM. --- class: mmedium # GMM * A GMM has a series of parameter that can be ilustrated here: 1- A mean μ that defines its centre. 2- A covariance Σ that defines its width. 3- A mixing probability π that defines how big or small the Gaussian function will be.
--- # Gaussian Clusters
image/svg+xml
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_1
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_2
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Cluster_3
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
Noise
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
--- class: mmedium # Gaussian Clusters * Each cluster has a mean (point) and covariance (matrix) * The mean defines where the center of the cluster is * The covariance matrix defines the size and extent * The mean and covariance are the *parameters* of the distribution * Technically, all Gauss distributions extend infinitely; we assign each data point to the cluster for which it has the highest probability (but we could allow membership in multiple clusters!); in other words, each Gaussian *contributes* to each data point with some (non-zero) probability --- class: mmedium # Expectation Maximization (EM) * Similar to k-means, we can determine parameter values for k Gaussians iteratively * Initialize k means and covariance matrices, then iterate: - (**E**xpectation Step) Calculate the current responsibilities/contributions for each data point from each Gaussian - (**M**aximization Step) Use these responsibilities to calculate new means (weighted average of all data points), and covariance matrix * Repeat until the clusters don't change anymore --- # Expectation Maximization
--- class: medium # Expectation Maximization * The general mathematical formulation of EM is actually more powerful * It works for general, parameterized models with latent (inferred) variables * The Expectation step computes the probabilities for these latent variables (which we called the "contribution" of a Gaussian to a data point) * The Maximization step finds new parameters using these probabilities (our parameters were the mean and covariance) that maximizes the likelihood of the data points --- # Density-Based Clustering How do we cluster this?
No matter where we put our cluster centers, we can't cluster it into the inner and outer ring. --- class: small # Density-Based Clustering * We can observe that clusters generally are "more dense" than the regions in between * Let's start with each data point in its own cluster * Single Linkage: We connect two clusters if the *distance* between any two points in them is minimal between all cluster pairs * Repeat until we have `k` clusters * Sometimes there are a few single points that would link two clusters, resulting in undesirable connections * Robust Linkage: Connect two clusters only if there are `t` points in each close to the other cluster --- class: medium # How many clusters? * So far we have kind of ignored how many clusters there are, but how do we get k? * Define "some measure" of cluster quality, and then try `\(k=1,2,3,4,\ldots\)` - Statistical: Variance explained by clusters - Measurements of cluster density, span, etc. - Usefulness in application (!) - etc. * There are also some more advanced algorithms that don't need to be told k explicitly (e.g. DBSCAN) --- class: small # References * [Foundations of Data Science](https://www.cs.cornell.edu/jeh/book.pdf) * *Pattern Recognition and Machine Learning* (Chapter 9), by Christopher Bishop