2017-09-26 5 views
1

Ich lese ein Buch über Einführung in maschinelles Lernen mit Python. Hier die Autoren beschrieben als unten Sagen wir, für die Workclass-Funktion haben wir mögliche Werte von "Government Employee", "Private Employee", "Self Employed" und "Self Employed Incorpora ted".get_dummies Verwendung in Pandas

print("Original features:\n", list(data.columns), "\n") 

data_dummies = pd.get_dummies(data) 

print("Features after get_dummies:\n", list(data_dummies.columns)) 

Original features: 
['age', 'workclass'] 

Features after get_dummies: 
['age', 'workclass_ ?', 'workclass_ Government Employee', 'workclass_Private Employee', 'workclass_Self Employed', 'workclass_Self Employed Incorporated'] 

Meine Frage ist, was ist neue Spalte workclass_?

Antwort

2

Es wird workclass mit String-Werte der Spalte erstellt:

data = pd.DataFrame({'age':[1,1,3], 
        'workclass':['Government Employee','Private Employee','?'], 
        'workclass1':['Government Employee','Private Employee','Self Employed']}) 

print (data) 
    age   workclass   workclass1 
0 1 Government Employee Government Employee 
1 1  Private Employee  Private Employee 
2 3     ?  Self Employed 

data_dummies = pd.get_dummies(data) 
print (data_dummies) 
    age workclass_? workclass_Government Employee \ 
0 1   0        1 
1 1   0        0 
2 3   1        0 

    workclass_Private Employee workclass1_Government Employee \ 
0       0        1 
1       1        0 
2       0        0 

    workclass1_Private Employee workclass1_Self Employed 
0       0       0 
1       1       0 
2       0       1 

Wenn Sie nicht:

data = pd.DataFrame({'age':[1,1,1,2,1,1], 
        'workclass':['Government Employee','Private Employee','Self Employed','Self Employed Incorpora ted','Self Employed Incorpora ted','?']}) 

print (data) 
    age     workclass 
0 1   Government Employee 
1 1    Private Employee 
2 1    Self Employed 
3 2 Self Employed Incorpora ted 
4 1 Self Employed Incorpora ted 
5 1       ? 

data_dummies = pd.get_dummies(data) 
print (data_dummies) 
    age workclass_? workclass_Government Employee \ 
0 1   0        1 
1 1   0        0 
2 1   0        0 
3 2   0        0 
4 1   0        0 
5 1   1        0 

    workclass_Private Employee workclass_Self Employed \ 
0       0      0 
1       1      0 
2       0      1 
3       0      0 
4       0      0 
5       0      0 

    workclass_Self Employed Incorpora ted 
0          0 
1          0 
2          0 
3          1 
4          1 
5          0 

Und wenn mehrere Spalten mit gleichen Werte haben dieses Präfix wirklich hilfreich ist Brauchen Sie es, ist es möglich, Parameter hinzufügen, um es durch Leerzeichen zu überschreiben:

data_dummies = pd.get_dummies(data, prefix='', prefix_sep='') 
print (data_dummies) 
    age ? Government Employee Private Employee Government Employee \ 
0 1 0     1     0     1 
1 1 0     0     1     0 
2 3 1     0     0     0 

    Private Employee Self Employed 
0     0    0 
1     1    0 
2     0    1 

Und dann möglich groupby durch Spalten und Aggregat max für Dummies pro eindeutige Spalten:

print (data_dummies.groupby(level=0, axis=1).max()) 
    ? Government Employee Private Employee Self Employed age 
0 0     1     0    0 1 
1 0     0     1    0 1 
2 1     0     0    1 3 
+0

hier eigentlich wir workclass_ nicht beobachtet werden? aber der Autor hat das erwähnt. Was ist das für eine Spalte – venkysmarty

+0

Ich füge es in der letzten Bearbeitung, überprüfen Sie es jetzt. – jezrael

+0

@Thanks bekam es jetzt – venkysmarty

Verwandte Themen