{"id":634,"date":"2026-03-02T22:28:26","date_gmt":"2026-03-02T22:28:26","guid":{"rendered":"https:\/\/ffritze.de\/?p=634"},"modified":"2026-06-02T10:18:15","modified_gmt":"2026-06-02T10:18:15","slug":"k-means-clustering-entwicklung-mit-scratch","status":"publish","type":"post","link":"https:\/\/ffritze.de\/en\/k-means-clustering-entwicklung-mit-scratch\/","title":{"rendered":"K-means-clustering \u2013 development with Scratch"},"content":{"rendered":"<p class=\"wp-block-paragraph translation-block\">K-Means Clustering is a popular algorithm in the field of unsupervised learning, which is used to divide data into groups or clusters. This approach makes it possible to identify patterns in data without requiring prior labels. For an introduction to the topic and a fun board game to illustrate the concepts of unsupervised learning, you can find information on <a href=\"https:\/\/computingeducation.de\/proj-snaip-B\/\" target=\"_blank\" rel=\"noreferrer noopener\">this page<\/a>. There you will find materials that help you better understand the basics of K-Means Clustering and apply them in practice.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Create prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the first step of the development of the K-Means clustering program in Scratch, we create the basic prerequisites for the process. First, we choose the number of cluster centers, which is set to 3 in this example. This decision allows us to keep the problem small and still make interesting observations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then we set the data points that are to be clustered randomly on the playing field. Ideally, we choose the random conditions in such a way that three loose groups are already formed. This facilitates later clustering and makes the results more vivid.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-baaca9a6 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p class=\"wp-block-paragraph\">After the data points are placed, we randomly distribute the cluster centers on the field. To improve visualization, we use three different colors for the cluster centers so that they stand out from each other. These steps lay the foundation for further developments of the K-Mean Clustering Algorithm in Scratch.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-bc18b00a wp-block-group-is-layout-flex\">\n<div class=\"responsive-iframe\">\n<iframe src=\"https:\/\/scratch.mit.edu\/projects\/1285662675\/embed\" allowtransparency=\"true\" frameborder=\"0\" scrolling=\"no\" allowfullscreen><\/iframe>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-102bd792 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/scratch.mit.edu\/projects\/1285662675\" target=\"_blank\" rel=\"noreferrer noopener\">\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-1212b163 wp-block-group-is-layout-flex\" style=\"margin-top:0;margin-bottom:0;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0\"> <figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" srcset=\"https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com.png 48w, https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com-12x12.png 12w\" sizes=\"auto, (max-width: 48px) 100vw, 48px\" class=\"wp-image-638\" src=\"https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com.png\" alt=\"\"> <\/figure> <div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-81b84b2d wp-block-group-is-layout-flex\"><p class=\"has-small-font-size wp-block-paragraph\">Show the<\/p> <p class=\"has-medium-font-size wp-block-paragraph\">code<\/p> <\/div> <\/div>\n<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n\n\n<div class=\"wp-block-group has-custom-hellblau-transparent-background-color has-background has-medium-font-size has-global-padding is-layout-constrained wp-container-core-group-is-layout-c9a371c5 wp-block-group-is-layout-constrained\" style=\"border-width:1px;border-top-left-radius:25px;border-top-right-radius:25px;border-bottom-left-radius:25px;border-bottom-right-radius:25px;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--30);box-shadow:var(--wp--preset--shadow--natural)\">\n<p class=\"wp-block-paragraph\"><strong>Hint:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"translation-block\"><strong>Data Storage:<\/strong>\u00a0The coordinates of the points and cluster centers as well as the coloring should be stored in lists. This allows for easy management and updating of the data during the clustering process.<\/li>\n\n\n\n<li class=\"translation-block\"><strong>Graphical representation:<\/strong> The graphical representation of the points and cluster centers is done by means of cloning in Scratch. By cloning, multiple instances of objects can be created, which facilitates the visualization of the clusters and their centers and allows dynamic interaction.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This approach ensures efficient processing and a clear presentation of the results in the K-Means clustering algorithm.<\/p>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Assignment of the points to the cluster centers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the second step of the K-Means clustering program, we take care of the assignment of the data points to the cluster centers. Once the cluster centers are defined, we activate this process by a button labeled \"color\".<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the \"color\" button is pressed, the points are assigned to the nearest cluster centers. Each point is analyzed on the basis of the Euclidean distance to the cluster centers. The point is assigned to the cluster center that is closest to it.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-baaca9a6 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-bc18b00a wp-block-group-is-layout-flex\">\n<div class=\"responsive-iframe\">\n<iframe src=\"https:\/\/scratch.mit.edu\/projects\/1285663047\/embed\" allowtransparency=\"true\" frameborder=\"0\" scrolling=\"no\" allowfullscreen><\/iframe>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-102bd792 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-vertical is-content-justification-stretch is-layout-flex wp-container-core-buttons-is-layout-08d6b6d8 wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/scratch.mit.edu\/projects\/1285663047\" target=\"_blank\" rel=\"noreferrer noopener\">\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-1212b163 wp-block-group-is-layout-flex\" style=\"margin-top:0;margin-bottom:0;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0\"> <figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" srcset=\"https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com.png 48w, https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com-12x12.png 12w\" sizes=\"auto, (max-width: 48px) 100vw, 48px\" class=\"wp-image-638\" src=\"https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com.png\" alt=\"\"> <\/figure> <div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-81b84b2d wp-block-group-is-layout-flex\"><p class=\"has-small-font-size wp-block-paragraph\">Show the<\/p> <p class=\"has-medium-font-size wp-block-paragraph\">code<\/p> <\/div> <\/div>\n<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\">After the assignment, we color the dots according to the color of their respective cluster center. This visual representation helps to clarify the grouping of the data points and shows how the K-Means algorithm works. The dots that belong to a cluster appear in the same color, which makes it easier to identify the clusters.<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-custom-hellblau-transparent-background-color has-background has-medium-font-size has-global-padding is-layout-constrained wp-container-core-group-is-layout-c9a371c5 wp-block-group-is-layout-constrained\" style=\"border-width:1px;border-top-left-radius:25px;border-top-right-radius:25px;border-bottom-left-radius:25px;border-bottom-right-radius:25px;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--30);box-shadow:var(--wp--preset--shadow--natural)\">\n<p class=\"wp-block-paragraph\"><strong>Hint:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"translation-block\"><strong>Use your own blocks:<\/strong> Creating \"own blocks\" in Scratch helps to make the code clearer and more structured. This makes it easier to understand and maintain the program, especially for more complex processes such as assigning and coloring the points.<\/li>\n\n\n\n<li class=\"translation-block\"><strong>Coloring by broadcast messages:<\/strong> Changing the color of the dots can be realized by broadcast messages. These messages make it possible to synchronize different parts of the program and to efficiently update the coloring of the points according to their cluster assignment.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These tips help optimize the programming of the K-Means clustering algorithm in Scratch and increase user-friendliness.<\/p>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Updating the cluster centers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the third step of the K-Mean Clustering program, we calculate the new coordinates of the cluster centers. To do this, we take the coordinates of the points that are assigned to a certain cluster and form the mean value of these coordinates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For each cluster, the average value of the x and y coordinates of the associated points is determined. These calculated values represent the new positions of the cluster centers and are used to move the cluster centers to the calculated coordinates.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-baaca9a6 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p class=\"wp-block-paragraph\">The activation of this process is done by the button with the label \"Means\". As soon as this button is pressed, the new cluster centers are positioned. After that, the coloring and the process of assigning the points to the cluster centers can be repeated again and again until the clusters have formed.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-bc18b00a wp-block-group-is-layout-flex\">\n<div class=\"responsive-iframe\">\n<iframe src=\"https:\/\/scratch.mit.edu\/projects\/1285662340\/embed\" allowtransparency=\"true\" frameborder=\"0\" scrolling=\"no\" allowfullscreen><\/iframe>\n<\/div>\n\n\n\n<div class=\"wp-block-buttons is-vertical is-content-justification-stretch is-layout-flex wp-container-core-buttons-is-layout-08d6b6d8 wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/scratch.mit.edu\/projects\/1285662340\" target=\"_blank\" rel=\"noreferrer noopener\">\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-1212b163 wp-block-group-is-layout-flex\" style=\"margin-top:0;margin-bottom:0;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0\"> <figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" srcset=\"https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com.png 48w, https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com-12x12.png 12w\" sizes=\"auto, (max-width: 48px) 100vw, 48px\" class=\"wp-image-638\" src=\"https:\/\/ffritze.de\/wp-content\/uploads\/2026\/03\/scratch-svgrepo-com.png\" alt=\"\"> <\/figure> <div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-81b84b2d wp-block-group-is-layout-flex\"><p class=\"has-small-font-size wp-block-paragraph\">Show the<\/p> <p class=\"has-medium-font-size wp-block-paragraph\">code<\/p> <\/div> <\/div>\n<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Through this iterative approach, the algorithm is continuously optimized until the cluster centers stabilize and the assignment of the points no longer changes. This step illustrates how the K-Means algorithm learns and adapts to the structure of the data to achieve the best possible grouping.<\/p>","protected":false},"excerpt":{"rendered":"<p>K-Means Clustering ist ein beliebter Algorithmus im Bereich des un\u00fcberwachten Lernens, der dazu verwendet wird, Daten in Gruppen oder Cluster zu unterteilen. Dieser Ansatz erm\u00f6glicht es, Muster in Daten zu erkennen, ohne dass vorherige Labels ben\u00f6tigt werden. F\u00fcr eine Einf\u00fchrung in das Thema und ein unterhaltsames Brettspiel zur Veranschaulichung der Konzepte des un\u00fcberwachten Lernens findest [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":636,"comment_status":"open","ping_status":"open","sticky":false,"template":"seite-thoughts-beitrag","format":"standard","meta":{"footnotes":""},"categories":[19,7,9],"tags":[49,36,48,47],"class_list":["post-634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithms","category-programs","category-scratch","tag-k-means-clustering","tag-maschinelles-lernen","tag-unsupervised-learning","tag-unueberwachtes-lernen"],"_links":{"self":[{"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/posts\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":0,"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/media\/636"}],"wp:attachment":[{"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ffritze.de\/en\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}