推荐系统中多值特征的八大处理技巧
在諸多數據處理的問題中,存在著一類特殊的特征--多值特征,該類特征常見的情形有:
- 論文的關鍵字描述信息;
- 商品信息的描述;
- 網站關鍵詞;
- 其它情況;
那么關于這些多值特征,該如何處理,都有哪些常見操作呢?本文我們介紹多值特征的8大處理技能,希望對大家有所啟發和幫助。
此處我們介紹幾種最為常見的多值特征的處理技巧。
即計算多值特征中有多少個值。
如果特征存在明顯的聚集效應,很多多值特征組合在一起就是一個有意義的信息,例如,
- 文章關鍵詞里面的:SVM|監督學習;
- 商品里面的,女生|連衣裙;
等等,這些詞組合在一起就是很有意義的一類信息,可以直接LabelEncoder進行編碼;
這個不用多說了,就是每個不同的值對應一列特征,出現了就是1沒出現就是0。
CounterVector是One-Hot的擴展,統計了每個詞在當前文本下的出現次數,如果每個關鍵詞在當前的Multi-Value特征中都是唯一的值的時候,那么CounterVector就和One-Hot等價。
TfidfVectorizer又可以認為是CounterVector的擴展,它還考慮了在上下問中文本的出現次數。
先使用Word2Vec進行訓練,得到每個詞的詞向量,再基于詞向量做統計特征。
對多值每個值進行emebdding之后,然后取對應的統計值進行concat得到最終的Multi-Value的表示。
和基礎的直接取均值最大最小值等統計特征不一樣,此處我們自動學習每個embedding對應的權重,然后再進行加權得到最終的emebdding。
當然上面是最基礎的一種加權策略,還有很多其它的策略,例如SENet等等。
此處我們就列舉下面幾種常見的操作,
- LabelEncoder
- CountVectorizer(包含Multi One-Hot)
- TfidfVectorizer
- Word2Vec
其余的有興趣的大家可以自己嘗試。
from sklearn.preprocessing import LabelEncoder corpus = ['This is the first document.','This document is the second document.','And this is the third one.','Is this the first document?',]vectorizer = LabelEncoder() X = vectorizer.fit_transform(corpus) Xarray([3, 2, 0, 1])
02 CountVectorizer
- Multi One-Hot是CountVectorizer的特例,此處就不再贅述。
<4x9 sparse matrix of type '<class 'numpy.int64'>' with 21 stored elements in Compressed Sparse Row format>
03 TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer corpus = ['This is the first document.','This document is the second document.','And this is the third one.','Is this the first document?',]vectorizer = TfidfVectorizer() X = vectorizer.fit_transform(corpus) X<4x9 sparse matrix of type '<class 'numpy.float64'>'
with 21 stored elements in Compressed Sparse Row format>
04 Word2Vec
import numpy as np import pandas as pd from gensim import modelsdef to_text_vector(txt, model):'''將文本txt轉化為文本向量'''words = txt.split(',') array = np.asarray([model.wv[w] for w in words if w in words],dtype='float32') return array.mean(axis=0)## 案例 sentences = ["1,2,3",'3,4,1','1,4,2'] model = models.Word2Vec(sentences, workers=8, min_count = 1, vector_size = 10, window = 2) to_text_vector(txt="1,2,3", model= model)array([-0.03244876, 0.00847926, -0.01252694, -0.0171898 , -0.02409677, -0.01349981, 0.01097592, 0.01558573, -0.01085999, -0.0165647 ], dtype=float32)
本文我們針對常見的多值特征問題,介紹了8大通用的處理策略。當然在多值特征等處理時,還需要注意:
- 如果多值特征和其它基于時間戳的特征等一起出現還需要特別注意很多衍生特征;
- 如果有多個關聯的多值特征出現的時候的一些衍生特征;
總結
以上是生活随笔為你收集整理的推荐系统中多值特征的八大处理技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐系统中的长尾物品(Tail Item
- 下一篇: 负样本的艺术,再读Facebook双塔向