# 聚类.zip

• zcyzm
了解作者
• Python
开发工具
• 4KB
文件大小
• zip
文件格式
• 0
收藏次数
• 10 积分
下载积分
• 1
下载次数
• 2020-10-22 18:57
上传日期
kmeans/dbscan/agnes...算法简单实现

• 聚类
• KModes.py
546B
• GMM.py
889B
• KMediod.py
591B
• DBSCANPackage.py
801B
• LVQ.py
1.5KB
• FCMPackage.py
1.1KB
• OPTICS.py
795B

# -*- coding:utf-8 -*- import numpy as np from sklearn import datasets import matplotlib.pyplot as plt from sklearn.decomposition import PCA iris = datasets.load_iris() # 随机挑选节点作为训练集合 def random_split(X, y, train_rate=0.3): y_ = np.array(list(zip(y, range(len(y))))) np.random.shuffle(y_) train_n = int(len(y) * train_rate) y_train = y_[:train_n] y_test = y_[train_n:] index_train = y_train[:, 1] index_test = y_test[:, 1] return X[index_train], y_train[:, 0], X[index_test], y_test[:, 0] # LVQ函数和LVQ的聚类函数 def LVQ(X, y, k, MAX_TIME=100, ita=0.2): init_index = np.random.choice(len(y), k) px = X[init_index] py = y[init_index] for _ in range(MAX_TIME): j = np.random.choice(len(y), 1) xj, yj = X[j], y[j] i = np.argmin([np.linalg.norm(xj - pi) for pi in px]) pyi = py[i] if pyi == yj: px[i] = px[i] + ita * (xj - px[i]) else: px[i] = px[i] - ita * (xj - px[i]) return px def PVQ_clustering(X, px): return np.array(list(map(lambda x: np.argmin([np.linalg.norm(x - pi) for pi in px]), X))) train_x, train_y, test_x, test_y = random_split(iris.data, iris.target) # 拆分数据集并训练 px = LVQ(train_x, train_y, 3) y = PVQ_clustering(test_x, px) X_reduced = PCA(n_components=2).fit_transform(test_x) plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap=plt.cm.Set1)

相关推荐