1. 程式人生 > >機器學習-字典資料抽取

機器學習-字典資料抽取

字典資料抽取

from sklearn.feature_extraction import DictVectorizer

def dictverc():
    
    """
    字典資料抽取
    """
    # 例項化
    dict = DictVectorizer()
    # 呼叫fit_transform
    data = dict.fit_transform([{'city':'北京','temperature':100},{'city':'上海','temperature':90},{'city':'青島','temperature':80}]);
    # 輸出
    print(data)
    
    
# 執行
dictverc()

結果:

  (0, 1)        1.0
  (0, 3)        100.0
  (1, 0)        1.0
  (1, 3)        90.0
  (2, 2)        1.0
  (2, 3)        80.0

Sparse矩陣輸出

from sklearn.feature_extraction import DictVectorizer

def dictverc():
    
    """
    字典資料抽取
    """
    # 例項化
    dict = DictVectorizer(sparse=False)
    # 呼叫fit_transform
    data = dict.fit_transform([{'city':'北京','temperature':100},{'city':'上海','temperature':90},{'city':'青島','temperature':80}]);
    # 輸出
    print(data)
    
    
# 執行
dictverc()

結果:

[[  0.   1.   0. 100.]
 [  1.   0.   0.  90.]
 [  0.   0.   1.  80.]]