使用python實現(xiàn)3D聚類圖示例代碼
更新時間:2024年08月20日 10:47:06 作者:兜里沒有一毛錢
這篇文章主要介紹了使用python實現(xiàn)3D聚類圖效果,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
實驗記錄,在做XX得分預(yù)測的實驗中,做了一個基于Python的3D聚類圖,水平有限,僅供參考。
一、以實現(xiàn)三個類別聚類為例
代碼:
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個聚類 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() # 打印每個聚類的名稱和對應(yīng)的數(shù)據(jù)點數(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)}") # 評估聚類效果 from sklearn import metrics print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X_pca, labels))
實現(xiàn)效果:
二、實現(xiàn)3個聚類以上,以9個類別聚類為例
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個聚類 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() # 打印每個聚類的名稱和對應(yīng)的數(shù)據(jù)點數(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)}") # 評估聚類效果 from sklearn import metrics print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X_pca, labels))
實現(xiàn)效果;
到此這篇關(guān)于使用python實現(xiàn)3D聚類圖的文章就介紹到這了,更多相關(guān)python 3D聚類圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Python使用樹狀圖實現(xiàn)可視化聚類詳解
- Python基于紋理背景和聚類算法實現(xiàn)圖像分割詳解
- python 層次聚類算法圖文示例
- Python K-means實現(xiàn)簡單圖像聚類的示例代碼
- Python使用OpenCV和K-Means聚類對畢業(yè)照進(jìn)行圖像分割
- Python實現(xiàn)K-means聚類算法并可視化生成動圖步驟詳解
- 在Python中使用K-Means聚類和PCA主成分分析進(jìn)行圖像壓縮
- python基于K-means聚類算法的圖像分割
- python聚類算法解決方案(rest接口/mpp數(shù)據(jù)庫/json數(shù)據(jù)/下載圖片及數(shù)據(jù))
相關(guān)文章
python實現(xiàn)簡單tftp(基于udp協(xié)議)
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡單tftp,基于udp協(xié)議,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07用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ù)時,傳入標(biāo)識符'w'或者'wb'表示寫文本文件或?qū)懚M(jìn)制文件,需要的朋友可以參考下2022-11-11