GaussianRandomProjection

2022-09-21 08:46:35 浏览数 (2)

代码语言:javascript复制
from time import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import offsetbox
from sklearn import (manifold,datasets,decomposition,ensemble,discriminant_analysis,random_projection)
digits=datasets.load_digits(n_class=6)
X=digits.data
y=digits.target
n_samples,n_features=X.shape
n_neighbors=30
#缩放和可视化嵌入向量
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif']=['SimHei']
def plot_embedding(X,title=None):
    x_min,x_max=np.min(X,0),np.max(X, 0)
    X=(X-x_min)/(x_max-x_min)
    plt.figure()
    ax=plt.subplot(111)
    for i in range(X.shape[0]):
        plt.text(X[i, 0], X[i, 1], str(digits.target[i]),
                 color=plt.cm.Set1(y[i]/10.),fontdict={'weight':'bold','size':9})
    if hasattr(offsetbox,'AnnotationBbox'):
        shown_images=np.array([[1.,1.]])
        for i in range(digits.data.shape[0]):
            dist=np.sum((X[i]-shown_images)**2,1)
            if np.min(dist)<4e-3:
                continue
            shown_images=np.r_[shown_images,[X[i]]]
            imagebox = offsetbox.AnnotationBbox(offsetbox.OffsetImage(digits.images[i],cmap=plt.cm.gray_r),X[i])
            ax.add_artist(imagebox)
    plt.xticks([]),plt.yticks([])
    if title is not None:
        plt.title(title)
#绘制数字图像
n_img_per_row=20
img = np.zeros((10*n_img_per_row,10*n_img_per_row))
for i in range(n_img_per_row):
    ix=10*i 1
    for j in range(n_img_per_row):
        iy=10*j 1
        img[ix:ix 8,iy:iy 8]=X[i*n_img_per_row j].reshape((8,8))
plt.imshow(img,cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.title('选择64维数字数据集')
#高斯随机投影嵌入数字数据集
print("计算高斯随机投影嵌入")
rp=random_projection.GaussianRandomProjection(n_components=2,random_state=42)
X_projected=rp.fit_transform(X)
plot_embedding(X_projected,"高斯随机投影嵌入数字数据集")
plt.show()

算法:GaussianRandomProjection是将原始输入空间投影到随机生成的矩阵的降维方法。

链接:https://scikit-learn.org.cn/view/771.html

0 人点赞