<jupyter_start><jupyter_text>MDS<jupyter_code>class MDS():
    
    def __init__(self, samples, dim=2):
        
        self.samples = samples
        self.dim = dim
        self.N = samples.shape[0]
        
        self.D = self.getD()
        self.B = self.getB()
        self.Z = self.getZ()

        
    def getD(self):
        
        Dij = np.dot(self.samples, self.samples.T)
        diag = Dij.diagonal()
        Dii = np.tile(diag, (self.N, 1))
        Djj = Dii.T
        D = np.sqrt(Dii + Djj - 2 * Dij)
        
        return D
        
    def getB(self):
        
        D = self.D
        Di = np.sum(np.square(D), axis=1, keepdims=1)
        Dj = np.sum(np.square(D), axis=0, keepdims=1)
        Dsum = np.square(D).sum()
        Di = np.tile(Di, self.N)
        Dj = np.tile(Dj, (self.N, 1))
        
        B = (Di + Dj - self.N * np.square(D) - Dsum / self.N) / (2 * self.N)
        
        return B.astype(np.float32)
    
    def getZ(self):
        
        vals, vecs = np.linalg.eig(self.B)
        sort_vals = np.argsort(vals)[::-1]
        
        vals = vals[sort_vals[:self.dim]]
        vecs = vecs[:, sort_vals[:self.dim]]
        
        vals_diag = np.diag(vals)
        
        Z = np.dot(vecs, np.sqrt(vals_diag).T)
        
        return Z
    
    def visualization(self):
        
        points = self.Z
        
        if self.dim == 2:
            plt.plot(points[labels==1, 0],points[labels==1, 1], 'x', label='1')
            plt.plot(points[labels==2, 0],points[labels==2, 1], '.', label='2')
            plt.savefig('dim2.png')
            
        if self.dim == 3:
            fig = plt.figure()
            ax = Axes3D(fig)
            ax.scatter(points[labels==1, 0],points[labels==1, 1], points[labels==1, 2], 'x', label='1')
            ax.scatter(points[labels==2, 0],points[labels==2, 1], points[labels==2, 2], '.', label='2')
            plt.savefig('dim3.png')<jupyter_output><empty_output><jupyter_text>Load Dataset MNIST12<jupyter_code>dict = sio.loadmat('./datasets/MNIST12.mat')
samples = dict['samples'][:5000]
labels = dict['labels'][:5000]
labels = np.reshape(labels, -1)<jupyter_output><empty_output><jupyter_text>二维降维<jupyter_code>mds2d = MDS(samples, dim=2)
mds2d.visualization()<jupyter_output><empty_output><jupyter_text>三维降维<jupyter_code>mds3d = MDS(samples, dim=3)
mds3d.visualization()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16562369628702545, 'fraction_numerical': 0.025448477263245724, 'mean_word_length': 2.0201511335012596, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '497036', 'license': 'Apache-2.0', 'n_tokens_mistral': 944, 'n_tokens_neox': 916, 'n_words': 143, 'path': 'hw3/MDS.ipynb', 'repo_name': 'ThreeSRR/SJTU-AU331-MLKD', 'size': '2397'}