1. 程式人生 > >機器學習之字典學習DictionaryLearning

機器學習之字典學習DictionaryLearning

  • 機器學習之字典學習DictionaryLearning
# -*- coding: utf-8 -*-
"""
Created on Sun Dec  9 13:00:52 2018

@author: muli
"""

from sklearn.decomposition import DictionaryLearning

def test_DictionaryLearning():
    '''
    測試 DictionaryLearning 的用法

    :return: None
    '''
    X=[[1,2,3,4,5],
       [6,7,8,9,10],
       [10,9,8,7,6,],
       [5,4,3,2,1] ]
    print("before transform:",X)
    dct=DictionaryLearning(n_components=3)
    dct.fit(X)
    print("components is :\n",dct.components_)
    print("after transform:\n",dct.transform(X))

if __name__=='__main__':
    test_DictionaryLearning() # 呼叫 test_DictionaryLearning
    
  • 驗證 X - B·A 的結果

import numpy as np

A= [[  0.     ,      0.    ,      -7.39987319],
 [  0.          , 0.       ,  -17.80675663],
 [-18.16560383  , 0.       ,    0.        ],
 [  0.        ,  -7.41619849 ,  0.        ]]


B=[[-0.5473499 , -0.49408716 ,-0.44082441, -0.38756166, -0.33429892],
 [-0.67419986 ,-0.53935989 ,-0.40451992, -0.26967994, -0.13483997],
 [-0.18512671 ,-0.30070103, -0.41627534, -0.53184965 ,-0.64742396]]


#C=np.dot(A,np.transpose(B))
#C=np.dot(np.transpose(B),np.transpose(A)).
C=np.dot(A,B)

print(C)

  • 注意,python生成的字典B與書上定義的字典B,可能存在一個轉置 的關係