8 אלגוריתמים מקבצים בלמידת מכונה שכל מדעני הנתונים צריכים לדעת

ישנן שלוש גישות שונות ללמידת מכונה, בהתאם לנתונים שיש לך. אתה יכול ללכת עם למידה מפוקחת, למידה מפוקחת למחצה או למידה ללא פיקוח.

בלמידה מפוקחת סימנת נתונים, כך שיש לך תפוקות שאתה יודע בוודאות שהם הערכים הנכונים עבור התשומות שלך. זה כמו לדעת מחירים של מכוניות על סמך תכונות כמו דגם, דגם, סגנון, רכבת נסיעה ותכונות אחרות.

עם למידה מפוקחת למחצה, יש לך מערך נתונים גדול שבו חלק מהנתונים מתויגים, אך רובם לא.

זה מכסה כמות גדולה של נתונים מהעולם האמיתי מכיוון שזה יכול להיות יקר לגרום למומחה לתייג כל נקודת נתונים. אתה יכול לעקוף את זה באמצעות שילוב של למידה מפוקחת ולא מפוקחת.

למידה ללא פיקוח פירושה שיש לך מערך נתונים שאינו מתויג לחלוטין. אינך יודע אם ישנם נתונים המסתתרים בנתונים, ולכן אתה משאיר לאלגוריתם למצוא כל מה שהוא יכול.

לשם נכנסים אלגוריתמי אשכולות. זו אחת השיטות בהן ניתן להשתמש בבעיית למידה ללא פיקוח.

מהם אלגוריתמי אשכולות?

אשכולות היא משימה שאינה מפוקחת על למידת מכונה. ייתכן שתשמע את זה המכונה ניתוח אשכולות בגלל אופן הפעולה של שיטה זו.

שימוש באלגוריתם אשכולות פירושו שאתה הולך לתת לאלגוריתם הרבה נתוני קלט ללא תוויות ולתת לו למצוא קבוצות בנתונים שהוא יכול.

קבוצות אלה נקראות אשכולות . אשכול הוא קבוצה של נקודות נתונים הדומות זו לזו על סמך יחסן לנקודות הנתונים הסובבות. אשכולות משמשים לדברים כמו הנדסת תכונות או גילוי תבניות.

כשאתה מתחיל בנתונים שאתה לא יודע כלום עליהם, אשכולות עשויים להיות מקום טוב לקבל תובנות.

סוגי אלגוריתמי אשכולות

ישנם סוגים שונים של אלגוריתמי אשכולות המטפלים בכל מיני נתונים ייחודיים.

מבוסס צפיפות

באשכולות מבוססי צפיפות הנתונים מקובצים לפי אזורים בעלי ריכוז גבוה של נקודות נתונים המוקפים באזורים בעלי ריכוזים נמוכים של נקודות נתונים. בעיקרון האלגוריתם מוצא את המקומות הדחוסים בנקודות נתונים ומכנה אותם אשכולות.

הדבר הנהדר בכך הוא שהאשכולות יכולים להיות בכל צורה שהיא. אינך מוגבל לתנאים הצפויים.

אלגוריתמי האשכולות תחת סוג זה לא מנסים להקצות חריגים לאשכולות, ולכן הם מתעלמים.

מבוסס הפצה

בגישת אשכולות מבוססת הפצה, כל נקודות הנתונים נחשבות לחלקים של אשכול בהתבסס על ההסתברות שהן שייכות לאשכול נתון.

זה עובד כך: יש נקודת מרכז, וככל שהמרחק של נקודת נתונים מהמרכז גדל, ההסתברות שהיא תהיה חלק מאותו אשכול פוחתת.

אם אינך בטוח כיצד ההפצה בנתונים שלך עשויה להיות, עליך לשקול סוג אחר של אלגוריתם.

מבוסס על מרכזי המוח

אשכולות מבוססי Centroid הם אלה שכנראה שומעים עליו ביותר. זה קצת רגיש לפרמטרים הראשוניים שאתה נותן לו, אבל זה מהיר ויעיל.

סוגים אלה של אלגוריתמים מפרידים בין נקודות נתונים בהתבסס על מספר מרכזים בנתונים. כל נקודת נתונים מוקצית לאשכול בהתבסס על המרחק הריבועי שלו מהמרכז. זהו סוג האשכול הנפוץ ביותר.

מבוסס היררכי

אשכולות מבוססי היררכיה משמשים בדרך כלל על נתונים היררכיים, כמו שהיית מקבל ממסד נתונים של חברות או טקסונומיות. הוא בונה עץ אשכולות כך שהכל מאורגן מלמעלה למטה.

זה מגביל יותר משאר סוגי האשכולות, אך הוא מושלם לסוגים ספציפיים של מערכי נתונים.

מתי להשתמש באשכולות

כאשר יש לך קבוצה של נתונים ללא תווית, סביר מאוד שתשתמש באיזה אלגוריתם למידה ללא פיקוח.

יש הרבה טכניקות למידה ללא פיקוח, כמו רשתות עצביות, למידת חיזוק ואשכולות. סוג האלגוריתם הספציפי שבו אתה רוצה להשתמש יהיה תלוי איך נראים הנתונים שלך.

ייתכן שתרצה להשתמש באשכולות כשאתה מנסה לבצע איתור חריגות כדי לנסות למצוא חריגים בנתונים שלך. זה עוזר על ידי מציאת אותן קבוצות אשכולות והצגת הגבולות שיקבעו אם נקודת נתונים היא חריגה או לא.

אם אינך בטוח באילו תכונות להשתמש במודל למידת המחשב שלך, אשכול מגלה דפוסים שבהם תוכל להשתמש כדי להבין מה בולט בנתונים.

אשכולות שימושיים במיוחד לחקר נתונים שאינכם יודעים עליהם דבר. זה יכול לקחת קצת זמן להבין איזה סוג של אלגוריתם אשכולות עובד הכי טוב, אבל כשתעשה זאת, תקבל תובנה שלא יסולא בפז על הנתונים שלך. אולי תמצאו קשרים שמעולם לא הייתם חושבים עליהם.

חלק מהיישומים בעולם האמיתי של אשכולות כוללים גילוי הונאה בביטוח, סיווג ספרים בספריה ופילוח לקוחות בשיווק. ניתן להשתמש בו גם בבעיות גדולות יותר, כמו ניתוח רעידות אדמה או תכנון ערים.

שמונה אלגוריתמי האשכולות המובילים

כעת, כשיש לך רקע כלשהו על אופן הפעולה של אלגוריתמי אשכולות ועל הסוגים השונים הזמינים, אנו יכולים לדבר על האלגוריתמים בפועל שתראה בדרך כלל בפועל.

אנו מיישמים את האלגוריתמים הללו על קבוצת נתונים לדוגמא מספריית sklearn בפייתון.

אנו נזכה להשתמש make_classification נתונים להגדיר מספריית sklearn כדי להדגים כיצד שונה באלגוריתמים של קיבוץ אינם מתאימים לכל בעיות באשכולות.

אתה יכול למצוא את הקוד לכל הדוגמה הבאה כאן.

K- פירושו אלגוריתם אשכולות

K- פירושו אשכולות הוא אלגוריתם האשכולות הנפוץ ביותר. זהו אלגוריתם מבוסס צנטרואיד ואלגוריתם הלמידה הפשוט ביותר ללא פיקוח.

אלגוריתם זה מנסה למזער את השונות של נקודות נתונים בתוך אשכול. כך גם מרבית האנשים מתוודעים ללימוד מכונה ללא פיקוח.

שימוש ב- K- פירושו בצורה הטובה ביותר בקבוצות נתונים קטנות יותר מכיוון שהוא מתבטא בכל נקודות הנתונים. כלומר, ייקח יותר זמן לסיווג נקודות נתונים אם יש כמות גדולה כאלה בערכת הנתונים.

Since this is how k-means clusters data points, it doesn't scale well.

Implementation:

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 clustering algorithm

DBSCAN stands for density-based spatial clustering of applications with noise. It's a density-based clustering algorithm, unlike k-means.

This is a good algorithm for finding outliners in a data set. It finds arbitrarily shaped clusters based on the density of data points in different regions. It separates regions by areas of low-density so that it can detect outliers between the high-density clusters.

This algorithm is better than k-means when it comes to working with oddly shaped data.

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.