使用python實(shí)現(xiàn)3D聚類圖示例代碼
實(shí)驗(yàn)記錄,在做XX得分預(yù)測的實(shí)驗(yàn)中,做了一個(gè)基于Python的3D聚類圖,水平有限,僅供參考。
一、以實(shí)現(xiàn)三個(gè)類別聚類為例
代碼:
import pandas as pd import numpy as np from sklearn.decomposition import PCA from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler from sklearn.impute import SimpleImputer import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 讀取數(shù)據(jù) data = pd.read_csv('E:\\shujuji\\Goods\\man.csv') # 選擇用于聚類的列 features = ['Weight', 'BMI', 'Lung Capacity Score', '50m Running Score', 'Standing Long Jump Score', 'Sitting Forward Bend Score', '1000m Running Score', 'Pulling Up Score', 'Total Score'] X = data[features] # 處理缺失值 imputer = SimpleImputer(strategy='mean') X_imputed = imputer.fit_transform(X) # 數(shù)據(jù)標(biāo)準(zhǔn)化 scaler = StandardScaler() X_scaled = scaler.fit_transform(X_imputed) # 應(yīng)用PCA降維到3維 pca = PCA(n_components=3) X_pca = pca.fit_transform(X_scaled) # 執(zhí)行K-means聚類 # 假設(shè)我們想要3個(gè)聚類 kmeans = KMeans(n_clusters=9, random_state=0).fit(X_pca) labels = kmeans.labels_ # 將聚類標(biāo)簽添加到原始DataFrame中 data['Cluster'] = labels # 3D可視化聚類結(jié)果 fig = plt.figure(1, figsize=(8, 6)) ax = fig.add_subplot(111, projection='3d') unique_labels = set(labels) colors = ['r', 'g', 'b'] for k, c in zip(unique_labels, colors): class_member_mask = (labels == k) xy = X_pca[class_member_mask] ax.scatter(xy[:, 0], xy[:, 1], xy[:, 2], c=c, label=f'Cluster {k}') ax.set_title('PCA of Fitness Data with K-means Clustering') ax.set_xlabel('Principal Component 1') ax.set_ylabel('Principal Component 2') ax.set_zlabel('Principal Component 3') plt.legend() plt.show() # 打印每個(gè)聚類的名稱和對應(yīng)的數(shù)據(jù)點(diǎn)數(shù)量 cluster_centers = kmeans.cluster_centers_ for i in range(3): cluster_data = data[data['Cluster'] == i] print(f"Cluster {i}: Count: {len(cluster_data)}") # 評(píng)估聚類效果 from sklearn import metrics print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X_pca, labels))
實(shí)現(xiàn)效果:
二、實(shí)現(xiàn)3個(gè)聚類以上,以9個(gè)類別聚類為例
import pandas as pd import numpy as np from sklearn.decomposition import PCA from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler from sklearn.impute import SimpleImputer import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 讀取數(shù)據(jù) data = pd.read_csv('E:\\shujuji\\Goods\\man.csv') # 選擇用于聚類的列 features = ['Weight', 'BMI', 'Lung Capacity Score', '50m Running Score', 'Standing Long Jump Score', 'Sitting Forward Bend Score', '1000m Running Score', 'Pulling Up Score', 'Total Score'] X = data[features] # 處理缺失值 imputer = SimpleImputer(strategy='mean') X_imputed = imputer.fit_transform(X) # 數(shù)據(jù)標(biāo)準(zhǔn)化 scaler = StandardScaler() X_scaled = scaler.fit_transform(X_imputed) # 應(yīng)用PCA降維到3維 pca = PCA(n_components=3) X_pca = pca.fit_transform(X_scaled) # 執(zhí)行K-means聚類 # 假設(shè)我們想要9個(gè)聚類 kmeans = KMeans(n_clusters=9, random_state=0).fit(X_pca) labels = kmeans.labels_ # 將聚類標(biāo)簽添加到原始DataFrame中 data['Cluster'] = labels # 3D可視化聚類結(jié)果 fig = plt.figure(1, figsize=(8, 6)) ax = fig.add_subplot(111, projection='3d') unique_labels = set(labels) colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'orange', 'purple'] for k, c in zip(unique_labels, colors): class_member_mask = (labels == k) xy = X_pca[class_member_mask] ax.scatter(xy[:, 0], xy[:, 1], xy[:, 2], c=c, label=f'Cluster {k}') ax.set_title('PCA of Fitness Data with K-means Clustering') ax.set_xlabel('Principal Component 1') ax.set_ylabel('Principal Component 2') ax.set_zlabel('Principal Component 3') plt.legend() plt.show() # 打印每個(gè)聚類的名稱和對應(yīng)的數(shù)據(jù)點(diǎn)數(shù)量 cluster_centers = kmeans.cluster_centers_ for i in range(9): cluster_data = data[data['Cluster'] == i] print(f"Cluster {i}: Count: {len(cluster_data)}") # 評(píng)估聚類效果 from sklearn import metrics print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X_pca, labels))
實(shí)現(xiàn)效果;
到此這篇關(guān)于使用python實(shí)現(xiàn)3D聚類圖的文章就介紹到這了,更多相關(guān)python 3D聚類圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python使用樹狀圖實(shí)現(xiàn)可視化聚類詳解
- Python基于紋理背景和聚類算法實(shí)現(xiàn)圖像分割詳解
- python 層次聚類算法圖文示例
- Python K-means實(shí)現(xiàn)簡單圖像聚類的示例代碼
- Python使用OpenCV和K-Means聚類對畢業(yè)照進(jìn)行圖像分割
- Python實(shí)現(xiàn)K-means聚類算法并可視化生成動(dòng)圖步驟詳解
- 在Python中使用K-Means聚類和PCA主成分分析進(jìn)行圖像壓縮
- python基于K-means聚類算法的圖像分割
- python聚類算法解決方案(rest接口/mpp數(shù)據(jù)庫/json數(shù)據(jù)/下載圖片及數(shù)據(jù))
相關(guān)文章
python實(shí)現(xiàn)簡單tftp(基于udp協(xié)議)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單tftp,基于udp協(xié)議,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Python編程中運(yùn)用閉包時(shí)所需要注意的一些地方
這篇文章主要介紹了Python編程中運(yùn)用閉包時(shí)所需要注意的一些地方,文章來自國內(nèi)知名的Python開發(fā)者felinx的博客,需要的朋友可以參考下2015-05-05pyscript的簡單應(yīng)用實(shí)現(xiàn)
本文主要介紹了pyscript的簡單應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07Python網(wǎng)絡(luò)安全格式字符串漏洞任意地址覆蓋大數(shù)字詳解
這篇文章主要介紹了Python網(wǎng)絡(luò)安全格式字符串漏洞任意地址覆蓋大數(shù)字的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10python?使用?with?open()?as?讀寫文件的操作方法
這篇文章主要介紹了python?使用?with?open()as?讀寫文件的操作代碼,寫文件和讀文件是一樣的,唯一區(qū)別是調(diào)用open()函數(shù)時(shí),傳入標(biāo)識(shí)符'w'或者'wb'表示寫文本文件或?qū)懚M(jìn)制文件,需要的朋友可以參考下2022-11-11