Terdapat tiga pendekatan yang berbeza untuk pembelajaran mesin, bergantung pada data yang anda miliki. Anda boleh mengikuti pembelajaran di bawah seliaan, pembelajaran separa penyeliaan, atau pembelajaran tanpa pengawasan.
Dalam pembelajaran yang diawasi, anda telah melabel data, jadi anda mempunyai output yang anda pasti tahu adalah nilai yang tepat untuk input anda. Itu seperti mengetahui harga kereta berdasarkan ciri seperti make, model, style, drivetrain, dan atribut lain.
Dengan pembelajaran separa penyeliaan, anda mempunyai satu set data yang besar di mana beberapa data dilabelkan tetapi kebanyakan tidak.
Ini merangkumi sejumlah besar data dunia nyata kerana dapat menjadi mahal untuk mendapatkan pakar untuk melabel setiap titik data. Anda boleh menyelesaikannya dengan menggunakan gabungan pembelajaran yang diawasi dan tidak diawasi.
Pembelajaran tanpa pengawasan bermaksud anda mempunyai set data yang sama sekali tidak berlabel. Anda tidak tahu sama ada terdapat corak yang tersembunyi dalam data, jadi anda membiarkannya menggunakan algoritma untuk mencari apa sahaja yang ada.
Di situlah algoritma pengelompokan masuk. Ini adalah salah satu kaedah yang boleh anda gunakan dalam masalah pembelajaran tanpa pengawasan.
Apa itu algoritma pengelompokan?
Penggabungan adalah tugas pembelajaran mesin yang tidak diawasi. Anda mungkin juga mendengar ini disebut sebagai analisis kluster kerana cara kaedah ini berfungsi.
Menggunakan algoritma kluster bermaksud anda akan memberi algoritma banyak data input tanpa label dan membiarkannya mencari pengelompokan dalam data yang dapat.
Pengelompokan tersebut dipanggil kluster . Kluster adalah sekumpulan titik data yang serupa antara satu sama lain berdasarkan hubungannya dengan titik data di sekitarnya. Penggabungan digunakan untuk perkara seperti kejuruteraan ciri atau penemuan corak.
Apabila anda memulakan dengan data yang tidak anda ketahui, pengelompokan mungkin merupakan tempat yang baik untuk mendapatkan pandangan.
Jenis algoritma pengelompokan
Terdapat pelbagai jenis algoritma pengelompokan yang menangani semua jenis data unik.
Berdasarkan ketumpatan
Dalam pengelompokan berdasarkan kepadatan, data dikelompokkan berdasarkan kawasan dengan titik data berkepekatan tinggi dikelilingi oleh kawasan titik data dengan kepekatan rendah. Pada asasnya algoritma mencari tempat yang padat dengan titik data dan memanggil kluster tersebut.
Perkara yang hebat tentang ini adalah bahawa kelompok boleh berbentuk apa pun. Anda tidak terkawal dengan keadaan yang diharapkan.
Algoritma pengelompokan di bawah jenis ini tidak mencuba untuk menetapkan outliers ke kluster, sehingga mereka diabaikan.
Berdasarkan pengedaran
Dengan pendekatan pengelompokan berdasarkan pengedaran, semua titik data dianggap sebagai bagian dari kluster berdasarkan kemungkinan mereka tergolong dalam kluster tertentu.
Ia berfungsi seperti ini: ada titik tengah, dan ketika jarak titik data dari pusat meningkat, kebarangkalian ia menjadi bahagian dari kelompok itu berkurang.
Sekiranya anda tidak pasti bagaimana sebaran data anda mungkin, anda harus mempertimbangkan jenis algoritma yang lain.
Berasaskan sentroid
Penggabungan berasaskan centroid adalah yang mungkin paling sering anda dengar. Agak peka terhadap parameter awal yang anda berikan, tetapi cepat dan cekap.
Jenis algoritma ini memisahkan titik data berdasarkan beberapa sentroid dalam data. Setiap titik data ditugaskan ke kelompok berdasarkan jarak kuadratnya dari pusat. Ini adalah jenis pengelompokan yang paling biasa digunakan.
Berasaskan hierarki
Pengelompokan berdasarkan hierarki biasanya digunakan pada data hierarki, seperti yang anda dapat dari pangkalan data syarikat atau taksonomi. Ia membina pokok gugusan sehingga semuanya tersusun dari atas ke bawah.
Ini lebih ketat daripada jenis pengelompokan lain, tetapi sangat sesuai untuk jenis kumpulan data tertentu.
Bila hendak menggunakan pengelompokan
Apabila anda mempunyai sekumpulan data tanpa label, kemungkinan besar anda akan menggunakan semacam algoritma pembelajaran tanpa pengawasan.
Ada banyak teknik pembelajaran tanpa pengawasan yang berbeza, seperti jaringan saraf, pembelajaran pengukuhan, dan pengelompokan. Jenis algoritma tertentu yang anda ingin gunakan akan bergantung pada rupa data anda.
Anda mungkin ingin menggunakan pengelompokan ketika anda berusaha melakukan pengesanan anomali untuk mencuba dan mencari penyimpangan dalam data anda. Ini membantu dengan mencari kumpulan kelompok tersebut dan menunjukkan batas-batas yang akan menentukan sama ada titik data adalah penyimpangan atau tidak.
Sekiranya anda tidak pasti ciri apa yang akan digunakan untuk model pembelajaran mesin anda, pengelompokan menemui corak yang dapat anda gunakan untuk mengetahui apa yang menonjol dalam data.
Pengelompokan sangat berguna untuk meneroka data yang anda tidak tahu. Mungkin memerlukan sedikit masa untuk mengetahui jenis algoritma pengelompokan mana yang paling baik, tetapi apabila anda melakukannya, anda akan mendapat gambaran yang tidak ternilai mengenai data anda. Anda mungkin menjumpai hubungan yang tidak pernah anda fikirkan.
Beberapa aplikasi pengelompokan dunia nyata termasuk pengesanan penipuan dalam insurans, mengkategorikan buku di perpustakaan, dan segmentasi pelanggan dalam pemasaran. Ini juga dapat digunakan dalam masalah yang lebih besar, seperti analisis gempa bumi atau perencanaan kota.
8 Algoritma Pengelompokan Teratas
Setelah anda mempunyai latar belakang mengenai bagaimana algoritma pengelompokan berfungsi dan pelbagai jenis yang ada, kami dapat membincangkan algoritma sebenar yang biasa anda lihat dalam praktik.
Kami akan menerapkan algoritma ini pada set data contoh dari perpustakaan sklearn di Python.
Kami akan menggunakan kumpulan data make_classification dari perpustakaan sklearn untuk menunjukkan bagaimana algoritma pengelompokan yang berbeza tidak sesuai untuk semua masalah pengelompokan.
Anda boleh mendapatkan kod untuk semua contoh berikut di sini.
K-bermaksud algoritma pengelompokan
K-means clustering adalah algoritma pengelompokan yang paling biasa digunakan. Ini adalah algoritma berasaskan centroid dan algoritma pembelajaran tanpa pengawasan termudah.
Algoritma ini cuba meminimumkan varians titik data dalam kluster. Ini juga bagaimana kebanyakan orang diperkenalkan dengan pembelajaran mesin tanpa pengawasan.
K-means paling baik digunakan pada set data yang lebih kecil kerana ia berulang pada semua titik data. Itu bermakna akan memerlukan lebih banyak masa untuk mengklasifikasikan titik data jika terdapat sejumlah besar di dalam kumpulan data.
Oleh kerana inilah k-bermaksud kumpulan data titik, ia tidak skala dengan baik.
Pelaksanaan:
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()
Algoritma pengelompokan DBSCAN
DBSCAN bermaksud pengelompokan ruang berdasarkan kepadatan aplikasi dengan bunyi. Ini adalah algoritma pengelompokan berdasarkan kepadatan, tidak seperti k-means.
Ini adalah algoritma yang baik untuk mencari garis besar dalam set data. Ia menemui kelompok berbentuk sewenang-wenang berdasarkan kepadatan titik data di kawasan yang berlainan. Ia memisahkan kawasan dengan kawasan berkepadatan rendah sehingga dapat mengesan garis pusat antara kelompok berkepadatan tinggi.
Algoritma ini lebih baik daripada k-berarti ketika bekerja dengan data berbentuk aneh.
DBSCAN menggunakan dua parameter untuk menentukan bagaimana kelompok didefinisikan: minPts (bilangan titik data minimum yang perlu dikelompokkan bersama untuk kawasan yang dianggap berkepadatan tinggi) dan eps (jarak yang digunakan untuk menentukan apakah titik data berada di kawasan yang sama dengan titik data lain).
Memilih parameter awal yang betul sangat penting agar algoritma ini berfungsi.
Pelaksanaan:
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()
Algoritma Model Campuran Gauss
Salah satu masalah dengan k-berarti adalah bahawa data perlu mengikuti format bulat. Cara k-means menghitung jarak antara titik data ada hubungannya dengan jalur bulat, sehingga data bukan bulat tidak dikelompokkan dengan benar.
Ini adalah masalah yang diperbaiki oleh model campuran Gauss. Anda tidak memerlukan data berbentuk bulat agar dapat berfungsi dengan baik.
Model campuran Gaussian menggunakan pelbagai pengedaran Gauss untuk menyesuaikan data berbentuk sewenang-wenangnya.
Terdapat beberapa model Gaussian tunggal yang bertindak sebagai lapisan tersembunyi dalam model hibrid ini. Oleh itu, model mengira kebarangkalian titik data tergolong dalam sebaran Gaussian tertentu dan itulah kelompok yang akan jatuh.
Pelaksanaan:
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()
Algoritma BIRCH
Algoritma Balance Iterative Reducing and Clustering menggunakan Hierarchies (BIRCH) berfungsi lebih baik pada set data yang besar berbanding algoritma k-means.
Ia memecah data menjadi ringkasan kecil yang dikelompokkan dan bukannya titik data asal. Ringkasan menyimpan seberapa banyak maklumat sebaran mengenai titik data sebanyak mungkin.
Algoritma ini biasanya digunakan dengan algoritma pengelompokan lain kerana teknik pengelompokan lain dapat digunakan pada ringkasan yang dihasilkan oleh 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.