original_docstring: Performs a binary search to get P-values in such a way that each conditional Gaussian has the same perplexity.
docstring: Performs a binary search to get P-values in such a way that each conditional Gaussian has the same perplexity.
docstring_tokens: Performs a binary search to get P - values in such a way that each conditional Gaussian has the same perplexity .
code: def x2p(D, 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."""
  
  	# Adrien: Removed these lines because the distances are already given as input (D)
  	# # Initialize some variables
  	# print("Computing pairwise distances...")
  	# (n, d) = X.shape;
  	# sum_X = np.sum(np.square(X), 1);
  	# D = np.add(np.add(-2 * np.dot(X, X.T), sum_X).T, sum_X);
  
  	n = len(D)
  
  	P = np.zeros((n, n));
  	beta = np.ones((n, 1));
  	logU = np.log(perplexity);
      
  	# Loop over all datapoints
  	for i in range(n):
  	
  		# Compute the Gaussian kernel and entropy for the current precision
  		betamin = -np.inf; 
  		betamax =  np.inf;
  		Di = D[i, np.concatenate((np.r_[0:i], np.r_[i+1:n]))];
  		(H, thisP) = Hbeta(Di, beta[i]);
  			
  		# Evaluate whether the perplexity is within tolerance
  		Hdiff = H - logU;
  		tries = 0;
  		while np.abs(Hdiff) > tol and tries < 50:
  				
  			# If not, increase or decrease precision
  			if Hdiff > 0:
  				betamin = beta[i];
  				if betamax == np.inf or betamax == -np.inf:
  					beta[i] = beta[i] * 2;
  				else:
  					beta[i] = (beta[i] + betamax) / 2;
  			else:
  				betamax = beta[i];
  				if betamin == np.inf or betamin == -np.inf:
  					beta[i] = beta[i] / 2;
  				else:
  					beta[i] = (beta[i] + betamin) / 2;
  			
  			# Recompute the values
  			(H, thisP) = Hbeta(Di, beta[i]);
  			Hdiff = H - logU;
  			tries = tries + 1;
  			
  	# print("Mean value of sigma: ", np.mean(np.sqrt(1 / beta)))
  	return np.sqrt(1 / beta)
code_tokens: def x2p ( D , tol = 1e-5 , perplexity = 30.0 ) : n = len ( D ) P = np . zeros ( ( n , n ) ) ; beta = np . ones ( ( n , 1 ) ) ; logU = np . log ( perplexity ) ; for i in range ( n ) : betamin = - np . inf ; betamax = np . inf ; Di = D [ i , np . concatenate ( ( np . r_ [ 0 : i ] , np . r_ [ i + 1 : n ] ) ) ] ; ( H , thisP ) = Hbeta ( Di , beta [ i ] ) ; Hdiff = H - logU ; tries = 0 ; while np . abs ( Hdiff ) > tol and tries < 50 : if Hdiff > 0 : betamin = beta [ i ] ; if betamax == np . inf or betamax == - np . inf : beta [ i ] = beta [ i ] * 2 ; else : beta [ i ] = ( beta [ i ] + betamax ) / 2 ; else : betamax = beta [ i ] ; if betamin == np . inf or betamin == - np . inf : beta [ i ] = beta [ i ] / 2 ; else : beta [ i ] = ( beta [ i ] + betamin ) / 2 ; ( H , thisP ) = Hbeta ( Di , beta [ i ] ) ; Hdiff = H - logU ; tries = tries + 1 ; return np . sqrt ( 1 / beta )
short_docstring: Performs a binary search to get P-values in such a way that each conditional Gaussian has the same perplexity.
short_docstring_tokens: Performs a binary search to get P - values in such a way that each conditional Gaussian has the same perplexity .
comment: """Performs a binary search to get P-values in such a way that each conditional Gaussian has the same perplexity."""$SEP$# Adrien: Removed these lines because the distances are already given as input (D)$SEP$# # Initialize some variables$SEP$# print("Computing pairwise distances...")$SEP$# (n, d) = X.shape;$SEP$# sum_X = np.sum(np.square(X), 1);$SEP$# D = np.add(np.add(-2 * np.dot(X, X.T), sum_X).T, sum_X);$SEP$# Loop over all datapoints$SEP$# Compute the Gaussian kernel and entropy for the current precision$SEP$# Evaluate whether the perplexity is within tolerance$SEP$# If not, increase or decrease precision$SEP$# Recompute the values$SEP$# print("Mean value of sigma: ", np.mean(np.sqrt(1 / beta)))