'Creates an affinity matrix for X using the selected affinity,
then applies spectral clustering to this affinity matrix.
Parameters
X : array-like or sparse matrix, shape (n_samples, n_features)
OR, if affinity==`precomputed`, a precomputed affinity
matrix of shape (n_samples, n_samples)'

def fit(self, X, y=None):

    X = check_array(X, accept_sparse=['csr', 'csc', 'coo'], dtype=np.float64)
    if ((X.shape[0] == X.shape[1]) and (self.affinity != 'precomputed')):
        warnings.warn('The    spectral    clustering    API    has    changed.    ``fit``now    constructs    an    affinity    matrix    from    data.    To    use    a    custom    affinity    matrix,    set    ``affinity=precomputed``.')
    if (self.affinity == 'nearest_neighbors'):
        connectivity = kneighbors_graph(X, n_neighbors=self.n_neighbors, include_self=True, n_jobs=self.n_jobs)
        self.affinity_matrix_ = (0.5 * (connectivity + connectivity.T))
    elif (self.affinity == 'precomputed'):
        self.affinity_matrix_ = X
    else:
        params = self.kernel_params
        if (params is None):
            params = {}
        if (not callable(self.affinity)):
            params['gamma'] = self.gamma
            params['degree'] = self.degree
            params['coef0'] = self.coef0
        self.affinity_matrix_ = pairwise_kernels(X, metric=self.affinity, filter_params=True, **params)
    random_state = check_random_state(self.random_state)
    self.labels_ = spectral_clustering(self.affinity_matrix_, n_clusters=self.n_clusters, eigen_solver=self.eigen_solver, random_state=random_state, n_init=self.n_init, eigen_tol=self.eigen_tol, assign_labels=self.assign_labels)
    return self
