Python字元統一標準化

2021-10-09 16:17:28 字數 576 閱讀 7577

在python 3.x環境下使用hugging face中bert模型獲取詞向量時,發現tokenizer的分詞結果出現亂碼,如下:

# 這是直接在互動式命令列執行的結果

tokenizer.tokenize(

"anti - spam suit"

)out[3]

:['anti'

,'-'

,'spa'

,'##m'

,'suit'

]

轉念一想,spam這樣乙個常見詞應該在詞表裡出現啊,那問題應該出現在編碼上,只要將字串編碼統一即可,想起之前看github上使用bert的dnn專案**,那個專案裡在資料載入之前便進行了這樣的操作。

使用unicodedata包的normalize方法,**如下

import unicodedata

unicodedata.normalize('nfd', 'anti - spam suit')

out[6]: 'anti - spam suit'

python 資料標準化

def datastandard from sklearn import preprocessing import numpy as np x np.array 1.1.2.2.0.0.0.1.1.print 原始資料為 n x print method1 指定均值方差資料標準化 預設均值0 方差 ...

Python 資料標準化

定義 將資料按照一定的比例進行縮放,使其落入乙個特定的區間。好處 加快模型的收斂速度,提高模型 精度 常見的六種標準化方法 class datanorm def init self self.arr 1 2,3 4,5 6,7 8,9 self.x max max self.arr self.x m...

Python資料標準化

z score標準化 1.產生隨機數 import numpy as np 產生隨機數 data 1 np.random.randn 3,4 從標準正態分佈中返回乙個或多個樣本值.data 2 np.random.rand 3,4 產生 0,1 的數 print randn產生的隨機數 n data...