Problema
I modelli hanno bisogno di numeri, non di parole. La codifica one-hot trasforma ogni categoria in un vettore con una posizione per categoria: 1 nella posizione della categoria e 0 in tutte le altre.
Ordina le categorie alfabeticamente. Per ogni etichetta, produci il suo vettore one-hot.
Implementa one_hot(labels).
Input
labels: Una lista di nomi di categorie.
Output
Restituisci una matrice con una riga per etichetta e una colonna per categoria distinta (in ordine alfabetico).
Vincoli
1 ≤ lunghezza ≤ 1.000
Ogni etichetta ha da 1 a 20 caratteri
Esempi
Input:
labels = ["red", "green", "blue", "green"]
Output atteso:
expected = [
[0, 0, 1],
[0, 1, 0],
[1, 0, 0],
[0, 1, 0]
]
Input:
std::vector<std::string> labels = {"red", "green", "blue", "green"};
Output atteso:
std::vector<std::vector<long long>> expected = {
{0, 0, 1},
{0, 1, 0},
{1, 0, 0},
{0, 1, 0}
};
Input:
string[] labels = new string[] {"red", "green", "blue", "green"};
Output atteso:
long[][] expected = new long[][] {
new long[] {0, 0, 1},
new long[] {0, 1, 0},
new long[] {1, 0, 0},
new long[] {0, 1, 0}
};
Spiegazione
Le categorie in ordine alfabetico sono blue, green, red. Quindi red → [0, 0, 1], green → [0, 1, 0] e blue → [1, 0, 0].
Input:
labels = ["yes"]
Output atteso:
expected = [
[1]
]
Input:
std::vector<std::string> labels = {"yes"};
Output atteso:
std::vector<std::vector<long long>> expected = {
{1}
};
Input:
string[] labels = new string[] {"yes"};
Output atteso:
long[][] expected = new long[][] {
new long[] {1}
};
Spiegazione
C'è una sola categoria, quindi ogni vettore è [1].
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.