作者ctr1 (【積π】)
看板DataScience
標題[問題] LabelEncoder移除沒訓練過的data
時間Wed May 13 10:57:08 2020
#python 3.7
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(["paris", "paris", "tokyo", "amsterdam"])
print('class:{}'.format(list(le.classes_)))
data1 = ["tokyo", "tokyo", "paris"]
print(le.transform(data1))
data2 = ["tokyo", "tokyo", "paris", "USA", "Taiwan"]
print(le.transform(data2))
----------------輸出-----------------
class:['amsterdam', 'paris', 'tokyo']
[2 2 1]
ValueError: y contains previously unseen labels: ['Taiwan', 'USA']
原因是因為Taiwin 跟 USA沒有訓練過
我想要在transform到未知的資料時移除該筆資料
想請問該怎操作呢
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.242.44.65 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/DataScience/M.1589338630.A.2A0.html
→ yoyololicon: 在transform前就處理乾淨ㄅ 05/13 19:00
→ yoyololicon: data2 = [x for x in data2 if x in data1] 05/13 19:01
→ ctr1: 感謝 05/14 00:29