
加载中…
个人资料
- 博客访问:
- 关注人气:
- 获赠金笔:0支
- 赠出金笔:0支
- 荣誉徽章:
tsne降维可视化
(2015-10-20 13:07:13)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
tsne.py
#
# Implementation of t-SNE in Python. The implementation was tested
on Python 2.5.1, and it requires a working
# installation of NumPy. The implementation comes with an example
on the MNIST dataset. In order to plot the
# results of this example, a working installation of matplotlib is
required.
# The example can be run by executing: ipython tsne.py -pylab
#
#
# Created by Laurens van der Maaten on
20-12-08.
# Copyright (c) 2008 Tilburg University. All
rights reserved.
import numpy as Math
import pylab as Plot
def Hbeta(D = Math.array([]), beta = 1.0):
"""Compute
the perplexity and the P-row for a specific value of the precision
of a Gaussian distribution."""
# Compute
P-row and corresponding perplexity
P =
Math.exp(-D.copy() * beta);
sumP =
sum(P)+1e-6;
H =
Math.log(sumP) + beta * Math.sum(D * P) / sumP;
P = P /
sumP;
return H,
P;
def x2p(X = Math.array([]), tol = 1e-5, perplexity = 30.0):
"""Performs
a binary search to get P-values in such a way that each conditional
Gaussian has the same perplexity."""
# Initialize
some variables
print
"Computing pairwise distances..."
(n, d) =
X.shape;
sum_X =
Math.sum(Math.square(X), 1);
D =
Math.add(Math.add(-2 * Math.dot(X, X.T), sum_X).T, sum_X);
P =
Math.zeros((n, n));
beta =
Math.ones((n, 1));
logU =
Math.log(perplexity);
# Loop over
all datapoints
for i in
range(n):
# Print progress
if i % 500 == 0:
print "Computing P-values for point ", i, " of ",
n, "..."
# Compute the Gaussian kernel and entropy for the
current precision
betamin = -Math.inf;
betamax = Math.inf;
Di = D[i, Math.concatenate((Math.r_[0:i],
Math.r_[i+1:n]))];
(H, thisP) = Hbeta(Di, beta[i]);
# Evaluate whether the perplexity is within
tolerance
Hdiff = H - logU;
tries = 0;
while Math.abs(Hdiff) > tol and tries <
50:
# If not, increase or decrease precision
if Hdiff > 0:
betamin = beta[i].copy();
if betamax == Math.inf or betamax ==
-Math.inf:
beta[i] = beta[i] * 2;
else: