<jupyter_start><jupyter_text>Identify Outliers using Tukey BoxPlots<jupyter_code>plt.subplots(figsize=(8,6))
X.boxplot(sym="k.")
plt.plot()<jupyter_output><empty_output><jupyter_text>Outlier! Sepal width contains outliers! Removing the Outlier by replacing with median value:<jupyter_code>outlier = X["sepal width (cm)"] > 4
outlier2 = X["sepal width (cm)"] < 2.05

median = X["sepal width (cm)"].median()

X.loc[X["sepal width (cm)"] > 4, "sepal width (cm)"] = median
X.loc[X["sepal width (cm)"] < 2.05, "sepal width (cm)"] = median
plt.subplots(figsize=(8,6))
X.boxplot(sym="k.")
plt.plot()<jupyter_output><empty_output><jupyter_text>Another way to Detect and remove Outlier:<jupyter_code>data = pd.DataFrame(data =iris_dataset.data, columns = ["A", "B", "C", "D"])
# detecting Outlier
# Inter Quartile Range is the distance between the 3rd Quartile and the first Qartile

minimum = 0
maximum = 0

def detect_outlier(feature):
    first_q = np.percentile(feature, 25)
    third_q = np.percentile(feature, 75) 
    IQR = third_q - first_q
    IQR *= 1.5
    minimum = first_q - IQR 
    maximum = third_q + IQR
    flag = False
    
    if(minimum > np.min(feature)):
        flag = True
    if(maximum < np.max(feature)):
        flag = True
        
    if(flag):
        print("Outlier exist")
    else:
        print("NO outlier exist")
        
def remove_outlier(feature):
    first_q = np.percentile(data[feature], 25)
    third_q = np.percentile(data[feature], 75) 
    IQR = third_q - first_q
    IQR *= 1.5
    minimum = first_q - IQR 
    maximum = third_q + IQR
    
    mean = data[feature].median()
    
    data.loc[data[feature] < minimum, feature] = mean
    data.loc[data[feature] > maximum, feature] = mean
for i in range(len(data.columns)):
    detect_outlier(data[data.columns[i]])

remove_outlier("B")
for i in range(len(data.columns)):
    detect_outlier(data[data.columns[i]])
data.boxplot()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14512008175779254, 'fraction_numerical': 0.01430761369443025, 'mean_word_length': 3.858560794044665, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '148099', 'license': 'MIT', 'n_tokens_mistral': 775, 'n_tokens_neox': 706, 'n_words': 162, 'path': 'Outliers Detection and Removing/Outliers_Detection_and_Removing.ipynb', 'repo_name': '78526Nasir/Kaggle-Students-Academic-Performance', 'size': '1957'}