'Calculate the affinity matrix from data
Parameters
X : array-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples
and n_features is the number of features.
If affinity is "precomputed"
X : array-like, shape (n_samples, n_samples),
Interpret X as precomputed adjacency graph computed from
samples.
Returns
affinity_matrix, shape (n_samples, n_samples)'

def _get_affinity_matrix(self, X, Y=None):

    if (self.affinity == 'precomputed'):
        self.affinity_matrix_ = X
        return self.affinity_matrix_
    if (self.affinity == 'nearest_neighbors'):
        if sparse.issparse(X):
            warnings.warn('Nearest    neighbors    affinity    currently    does    not    support    sparse    input,    falling    back    to    rbf    affinity')
            self.affinity = 'rbf'
        else:
            self.n_neighbors_ = (self.n_neighbors if (self.n_neighbors is not None) else max(int((X.shape[0] / 10)), 1))
            self.affinity_matrix_ = kneighbors_graph(X, self.n_neighbors_, include_self=True, n_jobs=self.n_jobs)
            self.affinity_matrix_ = (0.5 * (self.affinity_matrix_ + self.affinity_matrix_.T))
            return self.affinity_matrix_
    if (self.affinity == 'rbf'):
        self.gamma_ = (self.gamma if (self.gamma is not None) else (1.0 / X.shape[1]))
        self.affinity_matrix_ = rbf_kernel(X, gamma=self.gamma_)
        return self.affinity_matrix_
    self.affinity_matrix_ = self.affinity(X)
    return self.affinity_matrix_
