@@ -40,10 +40,35 @@ class TSNE(BaseTSNE):
4040 def _fit (self , X , skip_num_points = 0 ):
4141 """Private function to fit the model using X as training data."""
4242
43+ if isinstance (self .init , str ) and self .init == 'warn' :
44+ warnings .warn ("The default initialization in TSNE will change "
45+ "from 'random' to 'pca' in 1.2." , FutureWarning )
46+ self ._init = 'random'
47+ else :
48+ self ._init = self .init
49+
50+ if isinstance (self ._init , str ) and self ._init == 'pca' and issparse (X ):
51+ raise TypeError ("PCA initialization is currently not suported "
52+ "with the sparse input matrix. Use "
53+ "init=\" random\" instead." )
54+
4355 if self .method not in ['barnes_hut' , 'exact' ]:
4456 raise ValueError ("'method' must be 'barnes_hut' or 'exact'" )
4557 if self .angle < 0.0 or self .angle > 1.0 :
4658 raise ValueError ("'angle' must be between 0.0 - 1.0" )
59+ if self .learning_rate == 'warn' :
60+ warnings .warn ("The default learning rate in TSNE will change "
61+ "from 200.0 to 'auto' in 1.2." , FutureWarning )
62+ self ._learning_rate = 200.0
63+ else :
64+ self ._learning_rate = self .learning_rate
65+ if self ._learning_rate == 'auto' :
66+ self ._learning_rate = X .shape [0 ] / self .early_exaggeration / 4
67+ self ._learning_rate = np .maximum (self ._learning_rate , 50 )
68+ else :
69+ if not (self ._learning_rate > 0 ):
70+ raise ValueError ("'learning_rate' must be a positive number "
71+ "or 'auto'." )
4772
4873 if hasattr (self , 'square_distances' ):
4974 if self .square_distances not in [True , 'legacy' ]:
@@ -74,7 +99,7 @@ def _fit(self, X, skip_num_points=0):
7499 X = check_array (X , accept_sparse = ['csr' , 'csc' , 'coo' ],
75100 dtype = [np .float32 , np .float64 ])
76101 if self .metric == "precomputed" :
77- if isinstance (self .init , str ) and self .init == 'pca' :
102+ if isinstance (self ._init , str ) and self ._init == 'pca' :
78103 raise ValueError ("The parameter init=\" pca\" cannot be "
79104 "used with metric=\" precomputed\" ." )
80105 if X .shape [0 ] != X .shape [1 ]:
@@ -187,13 +212,17 @@ def _fit(self, X, skip_num_points=0):
187212 P = _joint_probabilities_nn (distances_nn , self .perplexity ,
188213 self .verbose )
189214
190- if isinstance (self .init , np .ndarray ):
191- X_embedded = self .init
192- elif self .init == 'pca' :
215+ if isinstance (self ._init , np .ndarray ):
216+ X_embedded = self ._init
217+ elif self ._init == 'pca' :
193218 pca = PCA (n_components = self .n_components , svd_solver = 'randomized' ,
194219 random_state = random_state )
195220 X_embedded = pca .fit_transform (X ).astype (np .float32 , copy = False )
196- elif self .init == 'random' :
221+ warnings .warn ("The PCA initialization in TSNE will change to "
222+ "have the standard deviation of PC1 equal to 1e-4 "
223+ "in 1.2. This will ensure better convergence." ,
224+ FutureWarning )
225+ elif self ._init == 'random' :
197226 # The embedding is initialized with iid samples from Gaussians with
198227 # standard deviation 1e-4.
199228 X_embedded = 1e-4 * random_state .randn (
0 commit comments