我試圖用數據集由UCI機器學習團隊發佈實施樸素貝葉斯分類器,我是新來的機器學習,並試圖瞭解技術使用我的工作相關的問題,所以我認爲最好先理解理論。MatLab的:樸素貝葉斯與單變量高斯
我使用pima數據集(Link to Data - UCI-ML),目標是針對K類問題(數據僅在K = 2時)構建樸素貝葉斯單變量高斯分類器。我已經做了分割數據,並計算平均爲每個類,標準偏差,先驗每個類但畢竟這,我有點堅持,因爲我不知道什麼,我應該如何在這之後做。我已經覺得我應該被計算後驗概率,這裏是我的代碼,我使用的百分比作爲載體,因爲我想看到的行爲,我是從80:20分增加訓練數據的大小。基本上,如果你通過[10 20 30 40]將採取該百分比從80:20分裂,並使用10%的80%,如訓練
function[classMean] = naivebayes(file, iter, percent)
dm = load(file);
for i=1:iter
idx = randperm(size(dm.data,1))
%Using same idx for data and labels
shuffledMatrix_data = dm.data(idx,:);
shuffledMatrix_label = dm.labels(idx,:);
percent_data_80 = round((0.8) * length(shuffledMatrix_data));
%Doing 80-20 split
train = shuffledMatrix_data(1:percent_data_80,:);
test = shuffledMatrix_data(percent_data_80+1:length(shuffledMatrix_data),:);
train_labels = shuffledMatrix_label(1:percent_data_80,:)
test_labels = shuffledMatrix_data(percent_data_80+1:length(shuffledMatrix_data),:);
%Getting the array of percents
for pRows = 1:length(percent)
percentOfRows = round((percent(pRows)/100) * length(train));
new_train = train(1:percentOfRows,:)
new_trin_label = shuffledMatrix_label(1:percentOfRows)
%get unique labels in training
numClasses = size(unique(new_trin_label),1)
classMean = zeros(numClasses,size(new_train,2));
for kclass=1:numClasses
classMean(kclass,:) = mean(new_train(new_trin_label == kclass,:))
std(new_train(new_trin_label == kclass,:))
priorClassforK = length(new_train(new_trin_label == kclass))/length(new_train)
priorClassforK_1 = 1 - priorClassforK
end
end
end
端