title: In outliers detection, train test split after or before fit the algorithm
body: <p>I'm implementing an outlier's detection pipeline.</p>
  
  <p>3 escenarios raised when I train the algorithm:</p>
  
  <p>Should I first split the data before fitting my outlier's detection algorithm?</p>
  
  <p>Should I fit only on train?</p>
  
  <p>Should I split and then fit on train and test separately?</p>
  
  <p>For illustrative purpose I'm going to use iris dataset </p>
  
  <p>A similar discussion is stated here in the case of scaling:</p>
  
  <p><a href="https://sebastianraschka.com/faq/docs/scale-training-test.html" rel="nofollow noreferrer">Why do we need to re-use training parameters to transform test data?</a></p>
  
  <pre><code>
  import pandas as pd
  import numpy as np
  import matplotlib.pyplot as plt
  from sklearn.datasets import load_iris
  from sklearn.ensemble import IsolationForest
  from sklearn.neighbors import LocalOutlierFactor
  from sklearn.svm import OneClassSVM
  from sklearn.model_selection import train_test_split
  
  from warnings import filterwarnings
  
  filterwarnings('ignore')
  
  
  X,y = load_iris(return_X_y= True)
  
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .3, random_state = 123)
  
  
  # Option 1
  
  Iso_outliers = IsolationForest().fit(X_train)
  
  Iso_outliers_train = Iso_outliers.predict(X_train)
  
  Iso_outliers_test = Iso_outliers.predict(X_test)
  
  # Option 2
  
  Iso_outliers_train = IsolationForest().fit_predict(X_train)
  
  Iso_outliers_test = IsolationForest().fit_predict(X_test)
  
  # Option 3
  
  Iso_outliers_X = IsolationForest().fit_predict(X)
  
  </code></pre>
  
  <ol>
  <li><p>Which of the 3 above options is the correct one and why?</p></li>
  <li><p>Does it depend on the algorithm to be used or it would apply the same for any outlier's detection algorithm?</p></li>
  </ol>