標籤

二元樹 (1) 字串常數池 (1) 投資 (3) 每月損益 (37) 例外處理 (1) 泛型 (2) 股票 (15) 指標 (5) 英文 (8) 基本資料型別 (1) 期貨 (1) 程式交易 (10) 量化投資 (5) 亂亂寫 (3) 概念 (3) 資料結構 (3) 演算法 (3) 數學 (3) 轉型 (1) AMA (1) ArrayList (1) assert (1) BeautifulSoup (1) C/C++ (8) casting (1) ClassCastException (1) classpath (1) Collection (4) Comparable (1) comparTo() (1) constructor (1) database (3) Debian (1) Dropbox (2) EA (2) enum (1) equals() (2) exception (3) extends (1) ffmpeg (1) final (1) Git (1) HashMap (1) HashSet (1) hasNext() (1) HTS (3) instanceof (1) IS-A (1) Iterator (1) JAVA (43) length (1) Linux (31) List (1) Mac (6) Map (1) ML (2) MT4 (6) MySQL (2) next() (1) NullPointerException (1) Number (1) Numpy (2) OpenCart (1) OpenCV (3) OSX (1) overloading (1) overriding (3) pandas (2) PHP (8) PriorityQueue (1) Python (11) Queue (1) random() (1) reverse() (1) Samba (1) SCJP (21) sqrt() (1) synchronized (1) talib (1) ufw (1) uTorrent (1) var-args (2) VHF (1) vim (2) Yhoo知識+ (4)

2019年12月26日 星期四

One-Hot Encoding

在分類中,若有下列資料

import pandas as pd
df1 = pd.DataFrame([['male', '178'],['female', '162']], columns=['gender','height (cm)'])
genderheight (cm)
0male178
1female162

因身高有連續性,標示在空間裡沒問題;若性別改成male:0,female:1,則導致不同性別對原點的距離不同。相同的若顏色在分類上也會出現一樣的問題。

解決方式利用One-Hot Encoding來編碼。


pd.get_dummies(df1['gender'])  #在pandas中用get_dummies這個函式

femalemale
001
110
female編碼成[0,1],male編碼成[1,0];不要把它想成二進位,它們都沒有量,只是標示而已。

練習合併
onehotEncode = pd.get_dummies(df1['gender'], prefix='gender')  #先產生一個df放編碼後的表格
onehotEncode

gender_femalegender_male
001
110


再把原來的gender那欄先刪掉
df2 = df1.drop('gender',1)
df2
height (cm)
0178
1162


再和One-Hot Encoding過的onehotEncode合併
df2 = pd.concat([onehotEncode,df2],axis=1)
df2
gender_femalegender_maleheight (cm)
001178
110162

大工告成,可喜可賀


1 則留言:

  1. If you're attempting to burn fat then you absolutely have to jump on this totally brand new custom keto plan.

    To produce this keto diet service, certified nutritionists, fitness trainers, and professional cooks united to provide keto meal plans that are effective, painless, economically-efficient, and enjoyable.

    From their launch in 2019, 1000's of clients have already remodeled their figure and well-being with the benefits a proper keto plan can give.

    Speaking of benefits; in this link, you'll discover 8 scientifically-confirmed ones provided by the keto plan.

    回覆刪除