jupyter nbconvert --to terminal mlps/03 - Outliers.ipynb
MDN-118:09

03 Outliers

03 - Outliers.ipynb · 53 KB · scarica
In [2]:
import pandas as pd
import numpy as np
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
In [3]:
df = pd.DataFrame(housing.data)
df.columns = housing.feature_names
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude
0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88 -122.23
1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86 -122.22
2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85 -122.24
3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85 -122.25
4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85 -122.25
.colab-df-container { display:flex; flex-wrap:wrap; gap: 12px; } .colab-df-convert { background-color: #E8F0FE; border: none; border-radius: 50%; cursor: pointer; display: none; fill: #1967D2; height: 32px; padding: 0 0 0 0; width: 32px; } .colab-df-convert:hover { background-color: #E2EBFA; box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15); fill: #174EA6; } [theme=dark] .colab-df-convert { background-color: #3B4455; fill: #D2E3FC; } [theme=dark] .colab-df-convert:hover { background-color: #434B5C; box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15); filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3)); fill: #FFFFFF; } const buttonEl = document.querySelector('#df-5b078452-f815-4fdc-91d0-424fb7a37e6b button.colab-df-convert'); buttonEl.style.display = google.colab.kernel.accessAllowed ? 'block' : 'none'; async function convertToInteractive(key) { const element = document.querySelector('#df-5b078452-f815-4fdc-91d0-424fb7a37e6b'); const dataTable = await google.colab.kernel.invokeFunction('convertToInteractive', [key], {}); if (!dataTable) return; const docLinkHtml = 'Like what you see? Visit the ' + 'data table notebook' + ' to learn more about interactive tables.'; element.innerHTML = ''; dataTable['output_type'] = 'display_data'; await google.colab.output.renderOutput(dataTable, element); const docLink = document.createElement('div'); docLink.innerHTML = docLinkHtml; element.appendChild(docLink); }
In [4]:
df['AveRooms'].describe()
count    20640.000000
mean         5.429000
std          2.474173
min          0.846154
25%          4.440716
50%          5.229129
75%          6.052381
max        141.909091
Name: AveRooms, dtype: float64
In [5]:
df['Population'].describe()
count    20640.000000
mean      1425.476744
std       1132.462122
min          3.000000
25%        787.000000
50%       1166.000000
75%       1725.000000
max      35682.000000
Name: Population, dtype: float64
In [6]:
import matplotlib.pyplot as plt
import seaborn as sns
sns.displot(df['Population'])
sns.displot(df['AveRooms'])
plt.show()
In [7]:
limite_superiore = df['AveRooms'].mean() + 3*df['AveRooms'].std()
limite_inferiore = df['AveRooms'].mean() - 3*df['AveRooms'].std()
print(limite_superiore)
print(limite_inferiore)
12.851519160463326
-1.9935196760825962
In [8]:
df['AveRooms'] = np.where(
    df['AveRooms']> limite_superiore,
    limite_superiore,
    np.where(
        df['AveRooms']< limite_inferiore,
        limite_inferiore,
        df['AveRooms']
    )
)
In [9]:
df['AveRooms'].describe()
count    20640.000000
mean         5.349898
std          1.416862
min          0.846154
25%          4.440716
50%          5.229129
75%          6.052381
max         12.851519
Name: AveRooms, dtype: float64
In [10]:
percentile25 = df['Population'].quantile(0.25)
percentile75 = df['Population'].quantile(0.75)
print(percentile25)
print(percentile75)
787.0
1725.0
In [12]:
iqr = percentile75 - percentile25
print(iqr)
938.0
In [13]:
limite_superiore = percentile75 + 1.5 * iqr
limite_inferiore = percentile25 - 1.5 * iqr
print(limite_superiore)
print(limite_inferiore)
df['Population'] = np.where(
    df['Population'] > limite_superiore,
    limite_superiore,
    np.where(
        df['Population'] < limite_inferiore,
        limite_inferiore,
        df['Population']
    )
)
df['Population'].describe()
3132.0
-620.0
count    20640.000000
mean      1336.959012
std        765.550830
min          3.000000
25%        787.000000
50%       1166.000000
75%       1725.000000
max       3132.000000
Name: Population, dtype: float64
In [14]:
sns.displot(df['Population'])
sns.displot(df['AveRooms'])
plt.show()