A gépi tanulásnak három különböző megközelítése van, a rendelkezésére álló adatoktól függően. Mehet felügyelt tanulással, félig felügyelt tanulással vagy felügyelet nélküli tanulással.
A felügyelt tanulás során felcímkézett adatokat, tehát vannak olyan kimenetei, amelyekről biztosan tudja, hogy a bemenetek helyes értékei. Ez olyan, mint az autóárak ismerete olyan jellemzők alapján, mint a márka, a modell, a stílus, a hajtáslánc és más jellemzők.
A félig felügyelt tanulás során nagy adatkészlete van, ahol az adatok egy részét felcímkézik, de a legtöbbet nem.
Ez nagy mennyiségű valós adatokra terjed ki, mert drága lehet egy szakértőt minden adatpont felcímkézése. Ezt kiküszöbölheti a felügyelt és felügyelet nélküli tanulás kombinációjával.
A felügyelet nélküli tanulás azt jelenti, hogy van olyan adatkészlete, amely teljesen fel van tüntetve. Nem tudja, van-e rejtve valamilyen minta az adatokban, ezért hagyja az algoritmusra, hogy megtalálja, amit csak tud.
Itt jönnek létre a fürtözési algoritmusok. Ez az egyik módszer, amelyet egy felügyelet nélküli tanulási probléma esetén alkalmazhat.
Mik azok a fürtözési algoritmusok?
A fürtözés felügyelet nélküli gépi tanulási feladat. Lehetséges, hogy ezt a módszert működése miatt klaszteranalízisnek is nevezi.
A fürtözési algoritmus használata azt jelenti, hogy sok bemeneti adatot fog megadni az algoritmusnak címkék nélkül, és hagyja, hogy bármilyen csoportosulást találjon az adatokban.
Ezeket a csoportosulásokat klasztereknek nevezzük . A fürt olyan adatpontok csoportja, amelyek hasonlóak egymáshoz a környező adatpontokhoz való viszonyuk alapján. A klaszterezést olyan dolgokra használják, mint a funkciótervezés vagy a minta felfedezése.
Ha olyan adatokkal kezd, amelyekről semmit sem tud, a fürtözés jó hely lehet a betekintéshez.
A fürtözési algoritmusok típusai
Különböző típusú fürtözési algoritmusok léteznek, amelyek mindenféle egyedi adatot kezelnek.
Sűrűség-alapú
A sűrűségalapú klaszterezésben az adatokat nagy adatkoncentrációkkal rendelkező területek köré csoportosítják, alacsony környezettel rendelkező adatpontok körzetében. Alapvetően az algoritmus megtalálja azokat a helyeket, amelyek sűrűek az adatpontokkal, és meghívja ezeket a klasztereket.
Ebben nagyszerű, hogy a klaszterek bármilyen alakúak lehetnek. Nem korlátozódik a várható körülményekre.
Az ilyen típusú fürtözési algoritmusok nem próbálnak kiugró értékeket rendelni a fürtökhöz, ezért figyelmen kívül hagyják őket.
Terjesztés-alapú
Terjesztésalapú klaszterezéses megközelítés esetén az összes adatpont egy klaszter részének tekintendő annak valószínűsége alapján, hogy egy adott fürthöz tartoznak.
Ez így működik: van egy középpont, és ahogy az adatpont távolsága a központtól növekszik, csökken annak a valószínűsége, hogy része legyen annak a fürtnek.
Ha nem biztos abban, hogy milyen lehet az adatok eloszlása, akkor fontoljon meg egy másik típusú algoritmust.
Centroid-alapú
A Centroid-alapú klaszterezésről hallasz valószínűleg a legjobban. Kicsit érzékeny az Ön által megadott kezdeti paraméterekre, de gyors és hatékony.
Az ilyen típusú algoritmusok különféle adatpontokat különítenek el az adatokban lévő többféle középpont alapján. Minden adatpont egy fürthöz van hozzárendelve a centroidtól négyzetes távolsága alapján. Ez a klaszterezés leggyakrabban használt típusa.
Hierarchikus alapú
A hierarchikus alapú fürtözést általában a hierarchikus adatokon használják, mint például egy vállalati adatbázisból vagy taxonómiákból. Fürtfát épít, így minden felülről lefelé szerveződik.
Ez szigorúbb, mint a többi fürtözési típus, de tökéletes az adott típusú adathalmazokhoz.
Mikor kell használni a fürtözést
Ha rendelkezik címkézetlen adatokkal, nagyon valószínű, hogy valamilyen felügyelet nélküli tanulási algoritmust fog használni.
Nagyon sokféle felügyelet nélküli tanulási technika létezik, mint például az ideghálózatok, az erősítő tanulás és a klaszterezés. A használni kívánt algoritmus típusa attól függ, hogy milyenek az adatai.
Érdemes a fürtözést használni, amikor rendellenességek felderítését próbálja megkeresni az adataiban található kiemelkedőkről. Segít megtalálni azokat a klasztercsoportokat és megmutatni azokat a határokat, amelyek meghatározhatják, hogy az adatpont kiugró-e vagy sem.
Ha nem biztos abban, hogy milyen funkciókat használjon a gépi tanulási modellhez, a fürtözés olyan mintákat fedez fel, amelyek segítségével kitalálhatja, mi áll ki az adatokban.
A fürtözés különösen hasznos olyan adatok feltárása során, amelyekről semmit sem tud. Időbe telhet annak kiderítése, hogy a fürtözési algoritmus melyik típusa működik a legjobban, de ha mégis megteszi, felbecsülhetetlen betekintést nyújt az adatokba. Találhat olyan kapcsolatokat, amelyekre soha nem gondolt volna.
A klaszterezés egyes valós alkalmazásai közé tartozik a csalások felderítése a biztosításokban, a könyvek osztályozása a könyvtárban és az ügyfelek szegmentálása a marketing területén. Nagyobb problémáknál is alkalmazható, például földrengéselemzésnél vagy várostervezésnél.
A legjobb 8 fürtözési algoritmus
Most, hogy megvan a háttér a fürtöző algoritmusok működéséről és a rendelkezésre álló különböző típusokról, beszélhetünk azokról a tényleges algoritmusokról, amelyeket a gyakorlatban gyakran látni fog.
Ezeket az algoritmusokat a Python sklearn könyvtárának egy példaadatkészletén valósítjuk meg.
A sklearn könyvtár make_classification adatkészletét használjuk annak bemutatására, hogy a különböző fürtözési algoritmusok miért nem alkalmasak minden fürtözési problémára.
A következő példa kódját itt találja.
A K jelentése fürtözési algoritmus
A K-klaszterezés a leggyakrabban használt fürtözési algoritmus. Ez egy centroid alapú algoritmus és a legegyszerűbb felügyelet nélküli tanulási algoritmus.
Ez az algoritmus megpróbálja minimalizálni az adatpontok szórását egy fürtön belül. Ez az, ahogyan a legtöbb ember megismerkedik a felügyelet nélküli gépi tanulással.
A K-átlagokat a legkisebb adatkészleteknél lehet legjobban használni, mert az összes adatponton végbemegy . Ez azt jelenti, hogy több időbe telik az adatpontok osztályozása, ha nagy mennyiségű van belőlük az adatkészletben.
Mivel így jelenti a k-klaszterek az adatpontokat, nem méretezhető jól.
Végrehajtás:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import KMeans # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model kmeans_model = KMeans(n_clusters=2) # assign each data point to a cluster dbscan_result = dbscan_model.fit_predict(training_data) # get all of the unique clusters dbscan_clusters = unique(dbscan_result) # plot the DBSCAN clusters for dbscan_cluster in dbscan_clusters: # get data points that fall in this cluster index = where(dbscan_result == dbscan_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the DBSCAN plot pyplot.show()
DBSCAN fürtözési algoritmus
A DBSCAN az alkalmazások sűrűségalapú térbeli klaszterezését jelenti. Ez egy sűrűségalapú fürtözési algoritmus, ellentétben a k-átlagokkal.
Ez egy jó algoritmus a körvonalak megkeresésére egy adatkészletben. Önkényesen formázott klasztereket talál a különböző régiók adatpontjainak sűrűsége alapján. A régiókat kis sűrűségű területekkel választja el, hogy a nagy sűrűségű klaszterek közötti kiugró értékeket észlelni tudja.
Ez az algoritmus jobb, mint a k-átlag, ha furcsa alakú adatokkal kell dolgozni.
DBSCAN uses two parameters to determine how clusters are defined: minPts (the minimum number of data points that need to be clustered together for an area to be considered high-density) and eps (the distance used to determine if a data point is in the same area as other data points).
Choosing the right initial parameters is critical for this algorithm to work.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import DBSCAN # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model dbscan_model = DBSCAN(eps=0.25, min_samples=9) # train the model dbscan_model.fit(training_data) # assign each data point to a cluster dbscan_result = dbscan_model.predict(training_data) # get all of the unique clusters dbscan_cluster = unique(dbscan_result) # plot the DBSCAN clusters for dbscan_cluster in dbscan_clusters: # get data points that fall in this cluster index = where(dbscan_result == dbscan_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the DBSCAN plot pyplot.show()
Gaussian Mixture Model algorithm
One of the problems with k-means is that the data needs to follow a circular format. The way k-means calculates the distance between data points has to do with a circular path, so non-circular data isn't clustered correctly.
This is an issue that Gaussian mixture models fix. You don’t need circular shaped data for it to work well.
The Gaussian mixture model uses multiple Gaussian distributions to fit arbitrarily shaped data.
There are several single Gaussian models that act as hidden layers in this hybrid model. So the model calculates the probability that a data point belongs to a specific Gaussian distribution and that's the cluster it will fall under.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.mixture import GaussianMixture # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model gaussian_model = GaussianMixture(n_components=2) # train the model gaussian_model.fit(training_data) # assign each data point to a cluster gaussian_result = gaussian_model.predict(training_data) # get all of the unique clusters gaussian_clusters = unique(gaussian_result) # plot Gaussian Mixture the clusters for gaussian_cluster in gaussian_clusters: # get data points that fall in this cluster index = where(gaussian_result == gaussian_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the Gaussian Mixture plot pyplot.show()
BIRCH algorithm
The Balance Iterative Reducing and Clustering using Hierarchies (BIRCH) algorithm works better on large data sets than the k-means algorithm.
It breaks the data into little summaries that are clustered instead of the original data points. The summaries hold as much distribution information about the data points as possible.
This algorithm is commonly used with other clustering algorithm because the other clustering techniques can be used on the summaries generated by BIRCH.
The main downside of the BIRCH algorithm is that it only works on numeric data values. You can't use this for categorical values unless you do some data transformations.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import Birch # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model birch_model = Birch(threshold=0.03, n_clusters=2) # train the model birch_model.fit(training_data) # assign each data point to a cluster birch_result = birch_model.predict(training_data) # get all of the unique clusters birch_clusters = unique(birch_result) # plot the BIRCH clusters for birch_cluster in birch_clusters: # get data points that fall in this cluster index = where(birch_result == birch_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the BIRCH plot pyplot.show()
Affinity Propagation clustering algorithm
This clustering algorithm is completely different from the others in the way that it clusters data.
Each data point communicates with all of the other data points to let each other know how similar they are and that starts to reveal the clusters in the data. You don't have to tell this algorithm how many clusters to expect in the initialization parameters.
As messages are sent between data points, sets of data called exemplars are found and they represent the clusters.
An exemplar is found after the data points have passed messages to each other and form a consensus on what data point best represents a cluster.
When you aren't sure how many clusters to expect, like in a computer vision problem, this is a great algorithm to start with.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import AffinityPropagation # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model model = AffinityPropagation(damping=0.7) # train the model model.fit(training_data) # assign each data point to a cluster result = model.predict(training_data) # get all of the unique clusters clusters = unique(result) # plot the clusters for cluster in clusters: # get data points that fall in this cluster index = where(result == cluster) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the plot pyplot.show()
Mean-Shift clustering algorithm
This is another algorithm that is particularly useful for handling images and computer vision processing.
Mean-shift is similar to the BIRCH algorithm because it also finds clusters without an initial number of clusters being set.
This is a hierarchical clustering algorithm, but the downside is that it doesn't scale well when working with large data sets.
It works by iterating over all of the data points and shifts them towards the mode. The mode in this context is the high density area of data points in a region.
That's why you might hear this algorithm referred to as the mode-seeking algorithm. It will go through this iterative process with each data point and move them closer to where other data points are until all data points have been assigned to a cluster.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import MeanShift # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model mean_model = MeanShift() # assign each data point to a cluster mean_result = mean_model.fit_predict(training_data) # get all of the unique clusters mean_clusters = unique(mean_result) # plot Mean-Shift the clusters for mean_cluster in mean_clusters: # get data points that fall in this cluster index = where(mean_result == mean_cluster) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the Mean-Shift plot pyplot.show()
OPTICS algorithm
OPTICS stands for Ordering Points to Identify the Clustering Structure. It's a density-based algorithm similar to DBSCAN, but it's better because it can find meaningful clusters in data that varies in density. It does this by ordering the data points so that the closest points are neighbors in the ordering.
This makes it easier to detect different density clusters. The OPTICS algorithm only processes each data point once, similar to DBSCAN (although it runs slower than DBSCAN). There's also a special distance stored for each data point that indicates a point belongs to a specific cluster.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import OPTICS # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model optics_model = OPTICS(eps=0.75, min_samples=10) # assign each data point to a cluster optics_result = optics_model.fit_predict(training_data) # get all of the unique clusters optics_clusters = unique(optics_clusters) # plot OPTICS the clusters for optics_cluster in optics_clusters: # get data points that fall in this cluster index = where(optics_result == optics_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the OPTICS plot pyplot.show()
Agglomerative Hierarchy clustering algorithm
This is the most common type of hierarchical clustering algorithm. It's used to group objects in clusters based on how similar they are to each other.
This is a form of bottom-up clustering, where each data point is assigned to its own cluster. Then those clusters get joined together.
At each iteration, similar clusters are merged until all of the data points are part of one big root cluster.
Agglomerative clustering is best at finding small clusters. The end result looks like a dendrogram so that you can easily visualize the clusters when the algorithm finishes.
Implementation:
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import AgglomerativeClustering # initialize the data set we'll work with training_data, _ = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=4 ) # define the model agglomerative_model = AgglomerativeClustering(n_clusters=2) # assign each data point to a cluster agglomerative_result = agglomerative_model.fit_predict(training_data) # get all of the unique clusters agglomerative_clusters = unique(agglomerative_result) # plot the clusters for agglomerative_cluster in agglomerative_clusters: # get data points that fall in this cluster index = where(agglomerative_result == agglomerative_clusters) # make the plot pyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the Agglomerative Hierarchy plot pyplot.show()
Other types of clustering algorithms
We've covered eight of the top clustering algorithms, but there are plenty more than that available. There are some very specifically tuned clustering algorithms that quickly and precisely handle your data. Here are a few of the others that might be of interest to you.
There's another hierarchical algorithm that's the opposite of the agglomerative approach. It starts with a top-down clustering strategy. So it will start with one large root cluster and break out the individual clusters from there.
This is known as the Divisive Hierarchical clustering algorithm. There's research that shows this is creates more accurate hierarchies than agglomerative clustering, but it's way more complex.
Mini-Batch K-means is similar to K-means, except that it uses small random chunks of data of a fixed size so they can be stored in memory. This helps it run faster than K-means so it converges to a solution in less time.
The drawback to this algorithm is that the speed boost will cost you some cluster quality.
The last algorithm we'll briefly cover is Spectral Clustering. This algorithm is completely different from the others we've looked at.
It works by taking advantage of graph theory. This algorithm doesn't make any initial guesses about the clusters that are in the data set. It treats data points like nodes in a graph and clusters are found based on communities of nodes that have connecting edges.
Other thoughts
Watch out for scaling issues with the clustering algorithms. Your data set could have millions of data points, and since clustering algorithms work by calculating the similarities between all pairs of data points, you might end up with an algorithm that doesn’t scale well.
Conclusion
Clustering algorithms are a great way to learn new things from old data. Sometimes you'll be surprised by the resulting clusters you get and it might help you make sense of a problem.
One of the coolest things about using clustering for unsupervised learning is that you can use the results in a supervised learning problem.
The clusters could be your new features that you use on a completely different data set! You can use clustering on just about any unsupervised machine learning problem, but make sure that you know how to analyze the results for accuracy.