jupyter nbconvert --to terminal mlps/03 - Encoding.ipynb
MDN-117:59

03 Encoding

03 - Encoding.ipynb · 21 KB · scarica
In [7]:
#importa librerie
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
# creo una lista di colori
colori = ['Bianco', 'Blu', 'Giallo', 'Grigio', 'Nero', 'Rosso', 'Verde']

# trasformo la lista in dataframe
df = pd.DataFrame()
df['Colore'] = colori

df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
Colore
0 Bianco
1 Blu
2 Giallo
3 Grigio
4 Nero
5 Rosso
6 Verde
.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-ba7a4778-f46e-4c69-bbda-43688aff4981 button.colab-df-convert'); buttonEl.style.display = google.colab.kernel.accessAllowed ? 'block' : 'none'; async function convertToInteractive(key) { const element = document.querySelector('#df-ba7a4778-f46e-4c69-bbda-43688aff4981'); 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 [8]:
encoder = OneHotEncoder()
encoded = encoder.fit_transform(df['Colore'].values.reshape(-1,1)).toarray()

# trasformo in dataframe
dfOneHot = pd.DataFrame(encoded, columns = ["Colore_"+str(i) for i in df['Colore']])

# concateno al dataframe originale
df = pd.concat([df, dfOneHot], axis=1)
df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
Colore Colore_Bianco Colore_Blu Colore_Giallo Colore_Grigio Colore_Nero Colore_Rosso Colore_Verde
0 Bianco 1.0 0.0 0.0 0.0 0.0 0.0 0.0
1 Blu 0.0 1.0 0.0 0.0 0.0 0.0 0.0
2 Giallo 0.0 0.0 1.0 0.0 0.0 0.0 0.0
3 Grigio 0.0 0.0 0.0 1.0 0.0 0.0 0.0
4 Nero 0.0 0.0 0.0 0.0 1.0 0.0 0.0
5 Rosso 0.0 0.0 0.0 0.0 0.0 1.0 0.0
6 Verde 0.0 0.0 0.0 0.0 0.0 0.0 1.0
.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-f39828a9-aed5-4809-83cc-a03c712da0cb button.colab-df-convert'); buttonEl.style.display = google.colab.kernel.accessAllowed ? 'block' : 'none'; async function convertToInteractive(key) { const element = document.querySelector('#df-f39828a9-aed5-4809-83cc-a03c712da0cb'); 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 [5]:
df['Colore'].sort()
AttributeError: ignored
In [3]:
#importa librerie
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# creo una lista di colori
colori = ['Giallo', 'Blu', 'Rosso', 'Verde', 'Grigio', 'Nero', 'Bianco']

# trasformo la lista in dataframe
df = pd.DataFrame()
df['Colore'] = colori
encoder = LabelEncoder()
df['Country_encoded'] = encoder.fit_transform(df['Colore'])

df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
Colore Country_encoded
0 Giallo 2
1 Blu 1
2 Rosso 5
3 Verde 6
4 Grigio 3
5 Nero 4
6 Bianco 0
.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-66c5399e-12da-4403-a84a-b7f03788e75e button.colab-df-convert'); buttonEl.style.display = google.colab.kernel.accessAllowed ? 'block' : 'none'; async function convertToInteractive(key) { const element = document.querySelector('#df-66c5399e-12da-4403-a84a-b7f03788e75e'); 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); }