'Fit the model using X as training data.
Parameters
X : {array-like, sparse matrix, BallTree, KDTree}
Training data. If array or matrix, shape [n_samples, n_features],
or [n_samples, n_samples] if metric=\'precomputed\'.
Returns
self : object
Returns self.'

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

    if (not (0.0 < self.contamination <= 0.5)):
        raise ValueError('contamination    must    be    in    (0,    0.5]')
    super(LocalOutlierFactor, self).fit(X)
    n_samples = self._fit_X.shape[0]
    if (self.n_neighbors > n_samples):
        warn(('n_neighbors    (%s)    is    greater    than    the    total    number    of    samples    (%s).    n_neighbors    will    be    set    to    (n_samples    -    1)    for    estimation.' % (self.n_neighbors, n_samples)))
    self.n_neighbors_ = max(1, min(self.n_neighbors, (n_samples - 1)))
    (self._distances_fit_X_, _neighbors_indices_fit_X_) = self.kneighbors(None, n_neighbors=self.n_neighbors_)
    self._lrd = self._local_reachability_density(self._distances_fit_X_, _neighbors_indices_fit_X_)
    lrd_ratios_array = (self._lrd[_neighbors_indices_fit_X_] / self._lrd[:, np.newaxis])
    self.negative_outlier_factor_ = (- np.mean(lrd_ratios_array, axis=1))
    self.threshold_ = (- scoreatpercentile((- self.negative_outlier_factor_), (100.0 * (1.0 - self.contamination))))
    return self
