<jupyter_start><jupyter_text>Tratamento de Outliers<jupyter_code>import random
import pandas as pd
import numpy as np
df = pd.DataFrame([random.gauss(0, sigma=1) for i in range(5000)], columns=['value'])
df.loc[df.sample(10).index,'value'] += 3
df.loc[df.sample(10).index,'value'] -= 3
df.head()
Q1 = df['value'].quantile(0.25)
Q3 = df['value'].quantile(0.75)
IQR = Q3 - Q1
print('Q1', Q1)
print('Q3', Q3)
print('IQR', IQR)
lower_bound = Q1-1.5*IQR
upper_bound = Q3+1.5*IQR
print('Normal Range', lower_bound, '-', upper_bound)
df['value'].describe(percentiles=[0.01, 0.1, 0.25, 0.50, 0.75, 0.90, 0.99])
import seaborn as sns
sns.boxplot(data=df, x=df['value'])
from scipy import stats
z = np.abs(stats.zscore(df.value))
print(z.head())
df[(df['value'] < df['value'].mean() - 3 * df['value'].std()) | (df['value'] > df['value'].mean() + 3 * df['value'].std())].shape
df[z>3].shape
new_df = df[z<3]
new_df.shape
lower_bound = df.value.mean()-3*df.value.std()
upper_bound = df.value.mean()+3*df.value.std()
df[df.value < lower_bound] = lower_bound
df[df.value > upper_bound] = upper_bound
print('lower_bound', lower_bound)
print('upper_bound', upper_bound)
df.describe(percentiles=[0.01, 0.1, 0.25, 0.50, 0.75, 0.90, 0.99])
sns.boxplot(data=df, x=df['value'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22161741835147744, 'fraction_numerical': 0.060653188180404355, 'mean_word_length': 5.919354838709677, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '306058', 'license': 'MIT', 'n_tokens_mistral': 590, 'n_tokens_neox': 539, 'n_words': 101, 'path': '02_Tratamento de Dados/tratamento_outliers.ipynb', 'repo_name': 'italo013/data_wrangling', 'size': '1286'}