# K均值聚类 K-means
import numpy as np
import matplotlib.pyplot as pltfrom sklearn.cluster import KMeans
from sklearn.datasets import make_blobsplt.figure(figsize=(12, 12))n_samples = 1500
random_state = 170
X, y = make_blobs(n_samples=n_samples, random_state=random_state)# 簇数有误 Incorrect number of clusters
y_pred = KMeans(n_clusters=2, random_state=random_state).fit_predict(X)plt.subplot(221)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.title("Incorrect Number of Blobs")#
transformation = [[0.60834549, -0.63667341], [-0.4088718, 0.85253229]]
X_aniso = np.dot(X, transformation)
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_aniso)plt.subplot(222)
plt.scatter(X_aniso[:, 0], X_aniso[:, 1], c=y_pred)
plt.title("Anisotropicly Distributed Blobs")# 不同的方差 Different variance
X_varied, y_varied = make_blobs(n_samples=n_samples, cluster_std=[1.0, 2.5, 0.5], random_state=random_state)
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_varied)plt.subplot(223)
plt.scatter(X_varied[:, 0], X_varied[:, 1], c=y_pred)
plt.title("Unequal Variance")# 不均匀大小的斑点 Unevenly sized blobs
X_filtered = np.vstack((X[y == 0][:500], X[y == 1][:100], X[y == 2][:10]))
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_filtered)plt.subplot(224)
plt.scatter(X_filtered[:, 0], X_filtered[:, 1], c=y_pred)
plt.title("Unevenly Sized Blobs")plt.show()
详细解决方案
『sklearn学习』K-means 聚类
热度:51 发布时间:2024-01-04 11:34:21.0
相关解决方案
- sklearn 中 make_blobs模块使用
- sklearn.neighbors实现KNN分类案例(海伦约会数据集)
- 【机器学习11】LAD,K-means,SVM分析鸢尾花和月亮数据集
- 【机器学习3】通过excel,python-矩阵求解法,python-sklearn 三种方法做重回归分析
- K-means T-SQL
- sklearn 学习之数据的读取与数据的探索
- scipy 的K-means
- 用 sklearn.utils.shuffle 来打乱样本顺序
- sklearn-1.1.14.被动攻击算法
- sklearn-1.1.13.感知器
- sklearn-1.1.12随机梯度下降
- sklearn-1.1.7.最小角度回归
- 机器学习基础-机器学习练习 7- K-means 和PCA(主成分分析)
- No module named 'sklearn'
- 人工智能-统计机器学习- K均值聚类 (K-means 聚类)
- k-means及k-means++原理【python代码实现】
- Anaconda Navigator图形化界面GUI配置python、jupyter、sklearn
- Sklearn 聚类分析 kmeans,DBSCAN
- k-means 算法 2021-04-25
- Sklearn-preprocessing.PolynomialFeatures
- sklearn naive_bayes
- win10下python配置tensorFlow环境 (matplotlib,opencv(cv2),sklearn,dlib,pandas,linearmodels,statsmode,keras)
- sklearn in python
- 机器学习---编程练习(七):K 均值聚类(K-means)与主成成份分析(PCA)
- 【图像分割】基于 K-means 聚类算法实现图像区域分割matlab代码
- windows 环境下pip环境变量配置以及如何使用pip安装库文件,sklearn,numpy等
- 通俗易懂的讲解K-means
- python sklearn.svm.SVC支持向量机实例
- python sklearn KNN 卷积神经网络-手写字识别实例
- python sklearn Rideg岭回归--交通流量预测实例