特徵抽取:特徵字典向量化和特徵哈希變換

注:本文是人工智能研究網的學習筆記算法

sklearn.feature_extaction模塊提供了從原始數據如文本,圖像等中抽取可以被機器學習算法直接處理的特徵向量。數組

Feature extraction和Feature selection是不一樣的:前者將任意的數據變換成機器學習算法可用的數值型特徵;後者是一個做用於特徵空間上的機器學習技術,是對特徵空間的再次變換。app

  • Loading Features From Dicts
  • Features hashing
  • Text Feature Extraction
  • Image Feature Extraction

Loading Features From Dicts

DictVectorizer類能夠用來把標準Python dict對象表示的特徵數組轉換成Numpy/Scipy的表示形式,以便於scikit-learn estimators的使用。機器學習

儘管速度不是很快,Python的dict使用起來仍是至關方便的,並且還能夠稀疏存儲(absent feature need not be stored);字典的形式便於將特徵的取值和名稱一一對應起來。函數

DictVectorizer實現了one-of-K或者叫「one-hot」編碼對標稱型特徵。標稱型特徵(Categorical feature)是「attribute-value」pairs,其中value是屬性的可能的取值列表,必須是有限的離散的沒有大小順序的。(e.g 男女,話題類別)學習

下面的例子中,‘city’是一個categorical attribute而‘temperature’是一個典型的numerical feature。ui

measurements = [
    {'city': 'Dubai', 'temperature': 33.0},
    {'city': 'London', 'temperature': 12.0},
    {'city': 'San Fransisco', 'temperature': 18.0},
]

from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer()

print(vec.fit_transform(measurements).toarray())
print(vec.get_feature_names())

Features hashing

Features hashing是一個高速的,低存儲的向量化的類。
通常的vectorizer是爲訓練過程當中遇到的特徵構建一個hash table,而FeatureHasher類則直接對特徵應用一個hash函數來決定特徵在樣本矩陣中的列索引。這樣的作法使得計算速度提高而且節省了內存,the hasher沒法記住輸入特徵的樣子,並且不遜在你想變換操做:inverse_transform。this

由於哈希函數可能會致使原本不相關的特徵之間發生衝突,因此使用了有符號的hash函數。對一個特徵,其hash值的符號決定了被存儲到輸出矩陣中的值的符號。經過這種方式就可以消除特徵hash映射時發生的衝突而不是累計衝突。並且任意輸出的值的指望均值是0.編碼

若是non_negative=True被傳入構造函數,將會取絕對值。這樣會發生一些衝突(collision)可是哈希特徵映射的輸出就能夠被傳入到一些只能接受非負特徵的學習器對象好比:
sklearn.naive_bayes.MultinomialNB分類器和sklearn.feature_selection.chi2特徵選擇器。人工智能

Features hashing接受參數類型可使:mappings(字典或者其變體容器)或者(feature,value)對,或者strings。這取決於構造器參數:input_type。

Mapping被看作是由(feature,value)構成的一個列表,而單個字符串隱式的等於1,因此['feat1', 'feat2', 'feat3']被解釋成(feature,value)的列表:[('feat1', 1), ('feat2',2), ('feat3', 3)]。 若是一個特徵在一個樣本中出現了屢次,相關聯的值就會累加起來:(好比('feat', 2)和('feat', 3.5)會累計起來成爲('feat', 5.5))。

FeatureHasher的輸出一般是CSR格式的scipy.sparse matrix。

Feature hashing 可被用於文檔分類中去,可是與text.CountVectorizer不一樣,FeatureHasher不作單詞切分或其餘的預處理操做,除了Unicode-to-UTF-8編碼之外。

Text Feature Extraction

  • The Bag of Words represention 詞袋模型
  • Sparsity
  • Common Vectorizer usage
  • TF-idf term weighting
  • Decoding text files
  • Limitations of the Bag of Words represention 詞袋模型的侷限性
  • Vertorizing a large text corpus with the hashing trick
  • Performing out-of-core scaling with HashingVectorizer
  • Customizing the vectorizer classes

The Bag of Words represention

Sparsity

Common Vectorizer usage

from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=1)
print(vectorizer)
corpus = [
    'This is the first documents.',
    'This is the second documents.',
    'And the third document.',
    'Is this the first documents?',
]

X = vectorizer.fit_transform(corpus)
print(X)

在默認的設置中,提取的字符串長度至少要有兩個字符,低於兩個字符的會被忽略,好比'a'

analyze = vectorizer.build_analyzer()
analyze('This is a text document to analyze.') == (['this', 'is', 'text', 'document', 'to', 'analyze'])

在fit階段被analyser發現的每個詞語(term)都會被分配一個獨特的整數索引(unique interger index), 該索引對應於特徵向量矩陣中的一列,所有小寫化。

使用下面的方法獲取某一個詞語在矩陣中的第幾列。

所以,在訓練語料中沒有見到過的單詞將會被將來的轉換方法徹底忽略。

相關文章
相關標籤/搜索