LabelEncoder
OrdinalEncoder ja OneHotEncoder käytetään tyypillisesti ominaisuuksien (eli X-muuttujan) koodaukseen. Kuitenkin myös kohdemuuttuja (y) voi olla kategorinen.
123456789import pandas as pd # Load the data and assign X, y variables df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/adult_edu.csv') y = df['income'] # Income is a target in this dataset X = df.drop('income', axis=1) print(y) print('All values: ', y.unique())
LabelEncoder-luokkaa käytetään kohdemuuttujan koodaukseen, riippumatta siitä, onko se nominaalinen vai ordinaalinen.
ML-mallit eivät ota huomioon kohteen järjestystä, joten se voidaan koodata millä tahansa numeerisilla arvoilla.
LabelEncoder koodaa kohteen numeroiksi 0, 1, ... .
1234567891011121314import pandas as pd from sklearn.preprocessing import LabelEncoder # Load the data and assign X, y variables df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/adult_edu.csv') y = df['income'] # Income is a target in this dataset X = df.drop('income', axis=1) # Initialize a LabelEncoder object and encode the y variable label_enc = LabelEncoder() y = label_enc.fit_transform(y) print(y) # Decode the y variable back y_decoded = label_enc.inverse_transform(y) print(y_decoded)
Yllä oleva koodi koodaa kohteen käyttäen LabelEncoder-luokkaa ja käyttää sitten .inverse_transform()-metodia muuntaakseen sen takaisin alkuperäiseen esitysmuotoon.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 3.13
LabelEncoder
Pyyhkäise näyttääksesi valikon
OrdinalEncoder ja OneHotEncoder käytetään tyypillisesti ominaisuuksien (eli X-muuttujan) koodaukseen. Kuitenkin myös kohdemuuttuja (y) voi olla kategorinen.
123456789import pandas as pd # Load the data and assign X, y variables df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/adult_edu.csv') y = df['income'] # Income is a target in this dataset X = df.drop('income', axis=1) print(y) print('All values: ', y.unique())
LabelEncoder-luokkaa käytetään kohdemuuttujan koodaukseen, riippumatta siitä, onko se nominaalinen vai ordinaalinen.
ML-mallit eivät ota huomioon kohteen järjestystä, joten se voidaan koodata millä tahansa numeerisilla arvoilla.
LabelEncoder koodaa kohteen numeroiksi 0, 1, ... .
1234567891011121314import pandas as pd from sklearn.preprocessing import LabelEncoder # Load the data and assign X, y variables df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/adult_edu.csv') y = df['income'] # Income is a target in this dataset X = df.drop('income', axis=1) # Initialize a LabelEncoder object and encode the y variable label_enc = LabelEncoder() y = label_enc.fit_transform(y) print(y) # Decode the y variable back y_decoded = label_enc.inverse_transform(y) print(y_decoded)
Yllä oleva koodi koodaa kohteen käyttäen LabelEncoder-luokkaa ja käyttää sitten .inverse_transform()-metodia muuntaakseen sen takaisin alkuperäiseen esitysmuotoon.
Kiitos palautteestasi!