Naive Bayes是一種非常方便,流行且重要的機器學(xué)習(xí)算法,尤其適用于文本分析和一般分類。在本文中,我將討論高斯樸素貝葉斯:算法,其實現(xiàn)和應(yīng)用于微型維基百科數(shù)據(jù)集(維基百科中給出的數(shù)據(jù)集)。 算法:高斯樸素貝葉斯算法是一種概率算法。它涉及到對數(shù)據(jù)集中的類和給定類的測試數(shù)據(jù)分別進行先驗概率和后驗概率的計算。 先驗概率的數(shù)學(xué)公式…eq-1) 所有類的先驗概率都使用相同的公式計算。 eq-2)
但是,如何獲得給定類的測試數(shù)據(jù)特征的條件概率呢? 這由從高斯(正常)分布獲得的概率給出。 eq-3)
最后,使用貝葉斯定理計算給定實例(測試實例)的每個類的條件概率。 給定測試數(shù)據(jù)x, c_i類條件概率的數(shù)學(xué)表達式 ... eq-4) 對所有類重復(fù)等式4),并且顯示最高概率的類最終被聲明為預(yù)測結(jié)果。 從頭開始在Python中實現(xiàn):如前所述,從頭開始編寫算法時,除了Numpy(它為Python提供了matlab類型的環(huán)境)和列表/字典相關(guān)的庫之外,沒有使用其他庫。4個模塊實現(xiàn)了高斯樸素貝葉斯二元分類,每個模塊執(zhí)行不同的操作。 => pre_prob():它通過將標簽集y作為輸入,按照eq-1)返回2個類的先驗概率。Python實現(xiàn)如下: # Importing necessary libraries...import collections import numpy as npdef pre_prob(y): y_dict = collections.Counter(y) pre_probab = np.ones(2) for i in range(0, 2): pre_probab[i] = y_dict[i]/y.shape[0] return pre_probab => mean_var():在給定特征集X和標簽集y作為輸入的情況下,該函數(shù)返回2個類標簽(二元分類)的所有特征的均值和方差。Python實現(xiàn)如下: def mean_var(X, y): n_features = X.shape[1] m = np.ones((2, n_features)) v = np.ones((2, n_features)) n_0 = np.bincount(y)[np.nonzero(np.bincount(y))[0]][0] x0 = np.ones((n_0, n_features)) x1 = np.ones((X.shape[0] - n_0, n_features)) k = 0 for i in range(0, X.shape[0]): if y[i] == 0: x0[k] = X[i] k = k + 1 k = 0 for i in range(0, X.shape[0]): if y[i] == 1: x1[k] = X[i] k = k + 1 for j in range(0, n_features): m[0][j] = np.mean(x0.T[j]) v[0][j] = np.var(x0.T[j])*(n_0/(n_0 - 1)) m[1][j] = np.mean(x1.T[j]) v[1][j] = np.var(x1.T[j])*((X.shape[0]-n_0)/((X.shape[0] - n_0) - 1)) return m, v # mean and variance => prob_feature_class():通過將均值m,方差v和測試數(shù)據(jù)x作為輸入,返回給定類c(eq-2)的測試數(shù)據(jù)x的后驗概率的函數(shù)。Python時如下: def prob_feature_class(m, v, x): n_features = m.shape[1] pfc = np.ones(2) for i in range(0, 2): product = 1 for j in range(0, n_features): product = product * (1/sqrt(2*3.14*v[i][j])) * exp(-0.5 * pow((x[j] - m[i][j]),2)/v[i][j]) pfc[i] = product return pfc => GNB():最后通過獲取測試實例x(eq-4)來計算2個類中每個類的條件概率。特征集X,標簽集y和測試數(shù)據(jù)x作為輸入并返回
GNB()的Python實現(xiàn)如下: def GNB(X, y, x): m, v = mean_var(X, y) pfc = prob_feature_class(m, v, x) pre_probab = pre_prob(y) pcf = np.ones(2) total_prob = 0 for i in range(0, 2): total_prob = total_prob + (pfc[i] * pre_probab[i]) for i in range(0, 2): pcf[i] = (pfc[i] * pre_probab[i])/total_prob prediction = int(pcf.argmax()) return m, v, pre_probab, pfc, pcf, prediction 高斯樸素貝葉斯在微型數(shù)據(jù)集中的應(yīng)用維基百科中給出的樣本性別數(shù)據(jù)集已用于實施的高斯樸素貝葉斯的應(yīng)用。 樸素貝葉斯分類器--Dataset(https://en./wiki/Naive_Bayes_classifier#Sex_classification) 問題陳述:“ 考慮到身高(以英尺為單位),體重(以磅為單位)和足部尺寸(以英寸為單位),預(yù)測該人是男性還是女性 ” =>數(shù)據(jù)讀取使用Pandas完成,因為數(shù)據(jù)集包含列的文本標題。 import pandas as pdimport numpy as npdata = pd.read_csv('gender.csv', delimiter = ',')data.head() =>對Wikipedia中使用的測試實例執(zhí)行4-module-Gaussian Naive Bayes。 樸素貝葉斯分類器 - 測試(https://en./wiki/Naive_Bayes_classifier#Testing) # converting from pandas to numpy ...X_train = np.array(data.iloc[:,[1,2,3]])y_train = np.array(data['Person'])for i in range(0,y_train.shape[0]): if y_train[i] == 'Female': y_train[i] = 0 else: y_train[i] = 1x = np.array([6, 130, 8]) # test instance used in Wikipedia# executing the Gaussian Naive Bayes for the test instance...m, v, pre_probab, pfc, pcf, prediction = GNB(X_train, y_train, x)print(m) # Output given below...(mean for 2 classes of all features)print(v) # Output given below..(variance for 2 classes of features)print(pre_probab) # Output given below.........(prior probabilities)print(pfc) # Output given below............(posterior probabilities)print(pcf) # Conditional Probability of the classes given test-dataprint(prediction) # Output given below............(final prediction) 所有特征(列)的2個類(行)的均值 所有特征(列)的2類(行)的樣本方差 類的先驗概率, Female and Male 給出兩個類別中的每一個的測試數(shù)據(jù)的后驗概率 給出測試數(shù)據(jù)的2個類的最終條件概率 最終預(yù)測 最后,計算和預(yù)測結(jié)果符合使用相同數(shù)據(jù)集的Wikipedia中顯示的結(jié)果。 |
|
來自: taotao_2016 > 《計算機》