Bio.MaxEntropy 模組
最大熵碼。
使用改進的迭代縮放法。
- class Bio.MaxEntropy.MaxEntropy
基礎:
object
持有最大熵分類器的資訊。
成員:classes 資料的可能類別列表。alphas 每個特徵的權重列表。feature_fns 特徵函數列表。
來自 Eric Meisner 於 2003 年 11 月 22 日所舉的 Naive Bayes 分類器範例的汽車數據 http://www.inf.u-szeged.hu/~ormandi/teaching
>>> from Bio.MaxEntropy import train, classify >>> xcar = [ ... ['Red', 'Sports', 'Domestic'], ... ['Red', 'Sports', 'Domestic'], ... ['Red', 'Sports', 'Domestic'], ... ['Yellow', 'Sports', 'Domestic'], ... ['Yellow', 'Sports', 'Imported'], ... ['Yellow', 'SUV', 'Imported'], ... ['Yellow', 'SUV', 'Imported'], ... ['Yellow', 'SUV', 'Domestic'], ... ['Red', 'SUV', 'Imported'], ... ['Red', 'Sports', 'Imported']] >>> ycar = ['Yes','No','Yes','No','Yes','No','Yes','No','No','Yes']
需要一些規則或特徵
>>> def udf1(ts, cl): ... return ts[0] != 'Red' ... >>> def udf2(ts, cl): ... return ts[1] != 'Sports' ... >>> def udf3(ts, cl): ... return ts[2] != 'Domestic' ... >>> user_functions = [udf1, udf2, udf3] # must be an iterable type >>> xe = train(xcar, ycar, user_functions) >>> for xv, yv in zip(xcar, ycar): ... xc = classify(xe, xv) ... print('Pred: %s gives %s y is %s' % (xv, xc, yv)) ... Pred: ['Red', 'Sports', 'Domestic'] gives No y is Yes Pred: ['Red', 'Sports', 'Domestic'] gives No y is No Pred: ['Red', 'Sports', 'Domestic'] gives No y is Yes Pred: ['Yellow', 'Sports', 'Domestic'] gives No y is No Pred: ['Yellow', 'Sports', 'Imported'] gives No y is Yes Pred: ['Yellow', 'SUV', 'Imported'] gives No y is No Pred: ['Yellow', 'SUV', 'Imported'] gives No y is Yes Pred: ['Yellow', 'SUV', 'Domestic'] gives No y is No Pred: ['Red', 'SUV', 'Imported'] gives No y is No Pred: ['Red', 'Sports', 'Imported'] gives No y is Yes
- __init__()
初始化類別。
- Bio.MaxEntropy.calculate(me, observation)
計算每個類別的機率對數。
me 是一個已經訓練過的最大熵物件。observation 是一個表示觀察數據的向量。返回值是每個類別的未標準化對數機率列表。
- Bio.MaxEntropy.classify(me, observation)
將觀察值分類到一個類別中。
- Bio.MaxEntropy.train(training_set, results, feature_fns, update_fn=None, max_iis_iterations=10000, iis_converge=1.0e-5, max_newton_iterations=100, newton_converge=1.0e-10)
訓練最大熵分類器,返回 MaxEntropy 物件。
在訓練集上訓練最大熵分類器。training_set 是觀察值的列表。results 是每個觀察值的類別分配列表。feature_fns 是特徵的列表。這些是回調函數,接受觀察值和類別,並返回 1 或 0。update_fn 是一個在每次訓練迭代時調用的回調函數。它會傳遞一個 MaxEntropy 物件,該物件封裝了訓練的當前狀態。
IIS 的最大迭代次數和收斂標準分別由 max_iis_iterations 和 iis_converge 給定,而 max_newton_iterations 和 newton_converge 則是牛頓法的最大迭代次數和收斂標準。