class: center, middle # Artificial Intelligence ### Machine Learning --- # Artificial Intelligence and Machine Learning
--- # Machine Learning What is Machine Learning? -- * In classical AI we write algorithms that solve a problem "intelligently" * In Machine Learning we use **data** to understand relationships * For example, given 10000 pictures of cats and dogs, we learn how to distinguish by learning the relationship between the pixels and the label (animal) --- # Machine Learning
[Source](https://xkcd.com/1838/) --- # Machine Learning - Supervised Learning: `\( \{(x_1, y_1), . . . ,(x_n, y_n)\} \)` Learn a mapping from examples. - Unsupervised Learning: `\( \{x_1, . . . , x_m\} \)` Learn an interesting thing about data. - Semi-supervised Learning: `\( \{(x_1, y_1), . . . ,(x_n, y_n)\} \cup \{x_1, . . . , x_m\} \)` - Reinforcement Learning: Learn what to do in an environment, given feedback information. --- # Machine Learning Say there is a function `\(f(\vec{x}) = y\)` - Supervised Learning: We know x and y, and are trying to find f (more accurately: a probability distribution `\(P(y|x)\)`) - Unsupervised Learning: We know x and are trying to find "interesting" f and y - Reinforcement Learning: We know f*, and are trying to get "the best y" by choosing x .tiny[*: Terms and Conditions may apply] --- class: medium # Supervised Learning: Rote Learning - Say we want to "learn" a function given some x and y (supervised learning) - The simplest thing to do is: Just memorize the values - Computers are good at remembering things! - However, in most interesting applications, we would need to store a lot of data - It also does not generalize: We only know the values we have seen --- class: medium # Supervised Learning? - What we want is to give the computer "some" x and y, and it finds the "general connection" between them - The function that we learn is not just a memorization of the values, but it also gives us a "good" value for y for x that we haven't seen before - Inter- and Extrapolation: From the known data, we can "predict" values for new inputs - But who are x and y? --- 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: Action sequences used by many people, common network traffic - Related data items: Purchases often made together, music listened to by the same people --- class: medium # Why Unsupervised Learning? * If we can find similar players, we can make them play together * Music can be recommended based on what you already listen to * Help new users by suggesting common actions/purchases made by similar users * 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/music/... * How can we determine which vectors belong to the same "class"/"type"? * How many classes are there? * We call the classes *clusters* --- class: small # Clustering
--- class: small # Clustering
--- 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: mmedium # 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: mmedium # 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 a 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 --- # 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 a dataset as a mixture of several Gaussian Distributions. * This is the core idea of Gaussian Mixture Models --- class: mmedium # Gaussian Mixture Models * A Gaussian (normal) distribution in our model has several parameters: 1- A mean μ that defines its centre 2- A covariance Σ that defines its width (in each dimension!)
--- # 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) --- # References * [Foundations of Data Science](https://www.cs.cornell.edu/jeh/book.pdf) * [Rote Learning](http://users.cs.cf.ac.uk/Dave.Marshall/AI2/node133.html) * [Machine Learning 4 All: Guides](https://ml4a.github.io/guides/) * *Pattern Recognition and Machine Learning* (Chapter 9), by Christopher Bishop * [So You Have Some Clusters, Now What?](https://medium.com/@fan_zhang/so-you-have-some-clusters-now-what-4cc39a531e9b)