将fisheriris、COIL20与MNIST三个数据集输入图正则化非负矩阵分解算法中再通过Kmeans聚类评价(精度、NMI)

首先,需要对三个数据集进行预处理,即将它们转换成图像矩阵的形式,并将它们进行归一化处理,使得每个像素值在[0,1]之间。

然后,可以使用图正则化非负矩阵分解算法(Graph-Regularized Non-negative Matrix Factorization, GR-NMF)对这些数据集进行分解,得到每个数据点的低维表示。GR-NMF 是一种常用的矩阵分解算法,它能够自动提取数据中的潜在特征,并生成一组非负的基向量和系数矩阵。此外,GR-NMF 还能够利用数据的图结构信息进行正则化,从而提高聚类效果。

接下来,可以使用 Kmeans 聚类算法对这些数据点进行聚类,并计算聚类结果的精度和 NMI。Kmeans 是一种基于距离的聚类算法,它将数据点划分为 K 个簇,使得每个簇内部的数据点尽可能相似,不同簇之间的数据点尽可能不同。精度和 NMI 是两种常用的聚类评价指标,它们分别衡量聚类结果与真实标签之间的相似程度。

下面是具体的代码实现:

import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score, normalized_mutual_info_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import load_iris, fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
from scipy.sparse import csr_matrix, coo_matrix

def gr_nmf(X, A, k):
    n_samples, n_features = X.shape
    n_neighbors = A.shape[1]

    # Initialize the basis and coefficient matrices
    W = np.random.rand(n_samples, k)
    H = np.random.rand(k, n_features)

    # Set the regularization parameter
    alpha = 1.0

    # Compute the graph Laplacian
    D = np.diag(A.sum(axis=1))
    L = D - A

    # Perform the alternating minimization
    for i in range(100):
        # Update the basis matrix
        numerator = X.dot(H.T) + alpha * W.dot(A)
        denominator = W.dot(H.dot(H.T)) + alpha * W.dot(D)
        W *= numerator / denominator

        # Update the coefficient matrix
        numerator = W.T.dot(X) + alpha * H.T.dot(A)
        denominator = H.T.dot(W.dot(W.T)) + alpha * H.T.dot(D)
        H *= numerator / denominator

    return W, H

# Load the iris dataset
iris = load_iris()
X_iris = iris.data
y_iris = iris.target

# Normalize the data
scaler = MinMaxScaler()
X_iris = scaler.fit_transform(X_iris)

# Compute the affinity matrix
A_iris = np.exp(-0.5 * np.square(X_iris[:, np.newaxis] - X_iris).sum(axis=2))

# Perform the GR-NMF
W_iris, H

猜你喜欢

转载自blog.csdn.net/weixin_47869094/article/details/130106054