Learn about K-Means Clustering Algorithm and it's use case in ML, Data Science and Cyber Security
Learn about K-Means Clustering algorithm and it’s use-case in various domains
seen from Türkiye
seen from United States
seen from United States
seen from United States

seen from Canada

seen from Malaysia
seen from China
seen from United Kingdom

seen from United States
seen from Ukraine
seen from United States
seen from Norway
seen from United States

seen from Norway

seen from Italy
seen from Italy
seen from Italy

seen from United States
seen from United Arab Emirates
seen from United States
Learn about K-Means Clustering Algorithm and it's use case in ML, Data Science and Cyber Security
Learn about K-Means Clustering algorithm and it’s use-case in various domains
지도학습, 비지도학습, 강화학습 완벽 정리! 스팸 필터부터 알파고까지, AI가 데이터로 배우는 3가지 방법을 실생활 예시로 쉽게 설명. 분류·회귀·군집화·보상 시스템의 모든 것. #AI학습 #Kmeans #ReinforcementLearning #SupervisedLearning #UnsupervisedLearning #강화학습 #고객세분화 #군집화 #머신러닝 #분류 #비지도학습 #스팸필터 #알파고 #자율주행 #지도학습 #집값예측 #추천시스템 #회귀 Read the full article
En esta actividad se realizó un análisis de conglomerados K-means, un método de aprendizaje automático no supervisado que agrupa observaciones según su similitud. El objetivo es identificar subgrupos dentro de un conjunto de datos basados en patrones observados en varias variables de agrupación.
from sklearn.datasets import load_iris from sklearn.preprocessing import StandardScaler from sklearn.cluster import KMeans import pandas as pd
iris = load_iris() X = pd.DataFrame(iris.data, columns=iris.feature_names)
scaler = StandardScaler() X_scaled = scaler.fit_transform(X)
kmeans = KMeans(n_clusters=3, random_state=42) clusters = kmeans.fit_predict(X_scaled)
X['cluster'] = clusters
print("Centroides:") print(kmeans.cluster_centers_)
print("Cantidad por cluster:") print(X['cluster'].value_counts())
Se usó el dataset Iris, que es el ejemplo clásico para K-means.
Salida:
Centroides:
[[ 0.34 -0.31 0.55 0.58] [-1.28 -1.23 -1.30 -1.22] [ 0.79 0.96 0.72 0.67]]
Tamaño de clusters:
1 62 0 50 2 38 Name: cluster, dtype: int64
El análisis K-means con k = 3 identificó tres subgrupos distintos en el conjunto de datos Iris. Los centroides representan la ubicación promedio de cada cluster en el espacio de características estandarizadas.
Los grupos quedaron distribuidos así:
Cluster 0 (50 flores): valores intermedios, probablemente corresponde a Iris Setosa.
Cluster 1 (62 flores): flores con medidas más pequeñas; corresponde a Iris Versicolor.
Cluster 2 (38 flores): flores con medidas más grandes; corresponde a Iris Virginica.
ابتكار يدمج الجيوفيزياء والذكاء الاصطناعي لتحديد خصائص التربة
🏷 AI Models Explained: Clustering Models (K-Means, DBSCAN)
📖 Clustering models are unsupervised learning algorithms that group similar data points together without needing labelled data. They’re widely used in market segmentation, anomaly detection, image analysis, and recommendation systems — helping AI uncover hidden structures in large datasets.
1️⃣ The Foundations
Clustering means automatically discovering patterns and grouping similar data.
Two popular clustering models:
K-Means: Divides data into k clusters by minimizing within-cluster variance.
DBSCAN: Groups points based on density, identifying noise and outliers effectively.
K-Means is simple and efficient, while DBSCAN handles irregular shapes and noise.
2️⃣ Where It’s Used
Marketing: Customer segmentation and targeted advertising.
Cybersecurity: Anomaly and intrusion detection.
Healthcare: Grouping patients by medical conditions.
E-commerce: Recommending similar products.
3️⃣ Strengths vs Limitations
Strengths
Automatically detects patterns in unlabeled data.
Scales well to large datasets.
Supports exploratory data analysis and insights.
Limitations
K-Means requires choosing the number of clusters k in advance.
DBSCAN struggles with varying densities.
Sensitive to data scaling and initialization.
4️⃣ Pro Tips
Use Elbow Method or Silhouette Score to find the best k for K-Means.
Standardize features before clustering.
Try DBSCAN when clusters have irregular shapes or noise.
Visualize results using PCA or t-SNE for interpretation.
💡 Final Note Clustering is the foundation of unsupervised learning — turning raw, unlabelled data into meaningful insights. Whether you’re segmenting users, detecting fraud, or understanding patterns, clustering models like K-Means and DBSCAN are your go-to tools.
📌 Series Continuation This is Day 10 of the AI Models Explained series 🎉. Next up: Principal Component Analysis (PCA) – Simplifying Data with Dimensionality Reduction.
Stay tuned with Uplatz as we continue exploring AI models, one at a time 🚀
KMeans Clustering Assignment
Import the modules
from pandas import Series, DataFrame import pandas as pd import numpy as np import matplotlib.pylab as plt from sklearn.model_selection import train_test_split from sklearn import preprocessing from sklearn.cluster import KMeans
Load the dataset
data = pd.read_csv("C:\Users\guy3404\OneDrive - MDLZ\Documents\Cross Functional Learning\AI COP\Coursera\machine_learning_data_analysis\Datasets\tree_addhealth.csv")
data.head()
upper-case all DataFrame column names
data.columns = map(str.upper, data.columns)
Data Management
data_clean = data.dropna() data_clean.head()
subset clustering variables
cluster=data_clean[['ALCEVR1','MAREVER1','ALCPROBS1','DEVIANT1','VIOL1', 'DEP1','ESTEEM1','SCHCONN1','PARACTV', 'PARPRES','FAMCONCT']] cluster.describe()
standardize clustering variables to have mean=0 and sd=1
clustervar=cluster.copy() clustervar['ALCEVR1']=preprocessing.scale(clustervar['ALCEVR1'].astype('float64')) clustervar['ALCPROBS1']=preprocessing.scale(clustervar['ALCPROBS1'].astype('float64')) clustervar['MAREVER1']=preprocessing.scale(clustervar['MAREVER1'].astype('float64')) clustervar['DEP1']=preprocessing.scale(clustervar['DEP1'].astype('float64')) clustervar['ESTEEM1']=preprocessing.scale(clustervar['ESTEEM1'].astype('float64')) clustervar['VIOL1']=preprocessing.scale(clustervar['VIOL1'].astype('float64')) clustervar['DEVIANT1']=preprocessing.scale(clustervar['DEVIANT1'].astype('float64')) clustervar['FAMCONCT']=preprocessing.scale(clustervar['FAMCONCT'].astype('float64')) clustervar['SCHCONN1']=preprocessing.scale(clustervar['SCHCONN1'].astype('float64')) clustervar['PARACTV']=preprocessing.scale(clustervar['PARACTV'].astype('float64')) clustervar['PARPRES']=preprocessing.scale(clustervar['PARPRES'].astype('float64'))
split data into train and test sets
clus_train, clus_test = train_test_split(clustervar, test_size=.3, random_state=123)
k-means cluster analysis for 1-9 clusters
from scipy.spatial.distance import cdist clusters=range(1,10) meandist=[]
for k in clusters: model=KMeans(n_clusters=k) model.fit(clus_train) clusassign=model.predict(clus_train) meandist.append(sum(np.min(cdist(clus_train, model.cluster_centers_, 'euclidean'), axis=1)) / clus_train.shape[0])
""" Plot average distance from observations from the cluster centroid to use the Elbow Method to identify number of clusters to choose """ plt.plot(clusters, meandist) plt.xlabel('Number of clusters') plt.ylabel('Average distance') plt.title('Selecting k with the Elbow Method')
Interpret 3 cluster solution
model3=KMeans(n_clusters=3) model3.fit(clus_train) clusassign=model3.predict(clus_train)
plot clusters
from sklearn.decomposition import PCA pca_2 = PCA(2) plot_columns = pca_2.fit_transform(clus_train) plt.scatter(x=plot_columns[:,0], y=plot_columns[:,1], c=model3.labels_,) plt.xlabel('Canonical variable 1') plt.ylabel('Canonical variable 2') plt.title('Scatterplot of Canonical Variables for 3 Clusters') plt.show()
The datapoints of the 2 clusters in the left are less spread out but have more overlaps. The cluster to the right is more distinct but has more spread in the data points
""" BEGIN multiple steps to merge cluster assignment with clustering variables to examine cluster variable means by cluster """
create a unique identifier variable from the index for the
cluster training data to merge with the cluster assignment variable
clus_train.reset_index(level=0, inplace=True)
create a list that has the new index variable
cluslist=list(clus_train['index'])
create a list of cluster assignments
labels=list(model3.labels_)
combine index variable list with cluster assignment list into a dictionary
newlist=dict(zip(cluslist, labels)) newlist
convert newlist dictionary to a dataframe
newclus=DataFrame.from_dict(newlist, orient='index') newclus
rename the cluster assignment column
newclus.columns = ['cluster']
now do the same for the cluster assignment variable
create a unique identifier variable from the index for the
cluster assignment dataframe
to merge with cluster training data
newclus.reset_index(level=0, inplace=True)
merge the cluster assignment dataframe with the cluster training variable dataframe
by the index variable
merged_train=pd.merge(clus_train, newclus, on='index') merged_train.head(n=100)
cluster frequencies
merged_train.cluster.value_counts()
""" END multiple steps to merge cluster assignment with clustering variables to examine cluster variable means by cluster """
FINALLY calculate clustering variable means by cluster
clustergrp = merged_train.groupby('cluster').mean() print ("Clustering variable means by cluster") print(clustergrp)
validate clusters in training data by examining cluster differences in GPA using ANOVA
first have to merge GPA with clustering variables and cluster assignment data
gpa_data=data_clean['GPA1']
split GPA data into train and test sets
gpa_train, gpa_test = train_test_split(gpa_data, test_size=.3, random_state=123) gpa_train1=pd.DataFrame(gpa_train) gpa_train1.reset_index(level=0, inplace=True) merged_train_all=pd.merge(gpa_train1, merged_train, on='index') sub1 = merged_train_all[['GPA1', 'cluster']].dropna()
Print statistical summary by cluster
import statsmodels.formula.api as smf import statsmodels.stats.multicomp as multi
gpamod = smf.ols(formula='GPA1 ~ C(cluster)', data=sub1).fit() print (gpamod.summary())
print ('means for GPA by cluster') m1= sub1.groupby('cluster').mean() print (m1)
print ('standard deviations for GPA by cluster') m2= sub1.groupby('cluster').std() print (m2)
Interpretation
The clustering average summary shows Cluster 0 has higher alcohol and marijuana problems, shows higher deviant and violent behavior, suffers from depression, has low self esteem,school connectedness, paraental and family connectedness. On the contrary, Cluster 2 shows the lowest alcohol and marijuana problems, lowest deviant & violent behavior,depression, and higher self esteem,school connectedness, paraental and family connectedness. Further, when validated against GPA score, we observe Cluster 0 shows the lowest average GPA and CLuster 2 has the highest average GPA which aligns with the summary statistics interpretation.
código: from pandas import DataFrame import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing imp
看看網頁版全文 ⇨ Python中文自然語言處理動手玩 / Learning Natural Language Processing with Python https://blog.pulipuli.info/2023/07/learningnatural-language-processing-with-python.html 這是之前演講投影片「Python中文自然語言處理動手玩」,以及投影片內使用的軟體與實作的連結。 這份投影片除了講述自然語言處理的基本概念之外,也可以在Colab上使用Python來實作自然語言處理。 ---- # 介紹 / Introduction 以往的自然語言處理(Natural Language Processing, NLP)通常必先基於大量的文本資料與冗長的處理過程,而現代最新技術則是結合預訓練模型來降低自然語言處理的技術門檻,並有效提升自然語言處理的正確率與應用範圍,使得文本分群(Text Corpus Clustering)、問答系統(Question Answering, QA)等應用更具實用價值。 本演講將介紹在自然語言處理中運用Google最新的預訓練模型Universal Sentence Encoder的原理與流程,並安排在Python環境下的文本分群與問答系統兩種應用的實機操作,讓學生透過實際動手學習現代最新的自然語言處理技術。 # 投影片 / Slide - Google簡報線上檢視:https://docs.google.com/presentation/d/1yTHcf-vKabXren0fUnR16YcV6rd8cMNuh10PEDdJ9C4/edit?usp=sharing - PowerPoint備份下載:GitHub, Google Drive, One Drive, Mega, Box, MediaFire # 大綱 / Outline - 自然語言處理基本概念 - 語義向量 (Embedding) - 語義向量實作 - 分類應用:問答判斷 - 實作:設計問答資料集 - 分群應用 - 實作:用Python實作分群 - 結語 # 教材 / Materials ## LibreOffice - LibreOffice下載:https://zh-tw.libreoffice.org/download/libreoffice-still/ 最自由的辦公室軟體。 支援Linux、Mac與Windows。 因為Excel預設編碼是Big5,資料科學專家都會選擇預設使用Unicode的LibreOffice Calc。 ## Sentence Encoder - 線上操作:https://pulipulichen.github.io/HTML5-Sentence-Encoder/ ---- 繼續閱讀 ⇨ Python中文自然語言處理動手玩 / Learning Natural Language Processing with Python https://blog.pulipuli.info/2023/07/learningnatural-language-processing-with-python.html