数据聚类源码程序 - matlab算法设计 - 谷速源码
下载频道> 资源分类> matlab源码> 算法设计> 数据聚类源码程序

标题:数据聚类源码程序
分享到:

所属分类: 算法设计 资源类型: 文件大小: 2.14 KB 上传时间: 2016-01-25 19:11:56 下载次数: 14 资源积分:1分 提 供 者: xiaopeng2 数据聚类源码程序
内容:
数据聚类源码程序,程序员在编程的过程中可以参考学习使用,希望对IT程序员有用,此源码程序简单易懂、方便阅读,有很好的学习价值!
function adaboostDemo
close all
clear all
clc
 
%% Settings
weakLearnerNum = 16; % how many weak classifiers will learn during AdaBoost training
displayTrainingProcess = 1; % whether display training process
 
%% generate samples
r = sqrt(rand(100,1)); % radius
t = 2*pi*rand(100,1); % angle
positive = [r.*cos(t), r.*sin(t)];
 
r = sqrt(3*rand(100,1)+1); % radius
t = 2*pi*rand(100,1); % angle
negative = [r.*cos(t), r.*sin(t)];
 
figure(1)
hold on
plot(positive(:,1),positive(:,2),'.r')
plot(negative(:,1),negative(:,2),'.b')
ezpolar(@(x)1);ezpolar(@(x)2);
axis equal
hold off
 
%% AdaBoost training process
data = [positive; negative];
labels = [ones(size(positive,1),1); -ones(size(negative,1),1)];
weights = [ones(size(positive,1),1) ./ (2*size(positive,1)); ones(size(negative,1),1) ./ (2*size(negative,1))];
weakLearners = zeros(weakLearnerNum,4); % [dimension, threshold, polarity, errorRate]
for t = 1 : weakLearnerNum
    % re-normalize the weights
    weights = weights ./ sum(weights);
    % select best classifier
    [dimension, threshold, polarity, errorRate] = selectBestClassifier(data, labels, weights);
    % update sample weights
    weights = updateSampleWeights(data, labels, weights, dimension, threshold, polarity, errorRate, displayTrainingProcess);
    % load weak classifier
    weakLearners(t,:) = [dimension, threshold, polarity, errorRate];
end
 
%% AdaBoosting predict process
figure(3)
hold on
for i = 1 : size(data,1)
    % classify by weak classifiers
    x = data(i,:);
    if adaboostingPredict(x, weakLearners) == 1
        plot(x(1), x(2), '.r');
    else
        plot(x(1), x(2), '.b');
    end
end
ezpolar(@(x)1);ezpolar(@(x)2);
axis equal
hold off
 
%% Classify sample
function label = adaboostingPredict(x, weakLearners)
dimensions = weakLearners(:,1);
thresholds = weakLearners(:,2);
polarities = weakLearners(:,3);
errorRates = weakLearners(:,4);
beta = errorRates ./ (1-errorRates);
alpha = log(1./beta);
features = x(dimensions);
hypothesis = (polarities' .* features)' < (polarities .* thresholds);
label = (alpha' * hypothesis) > (sum(alpha) * 0.5);
 
%% Update samples weights
function weights = updateSampleWeights(data, labels, weights, dimension, threshold, polarity, errorRate, displayTrainingProcess)
% classify data by current threshold
positive = find(polarity.*data(:,dimension) < polarity*threshold);
negative = find(polarity.*data(:,dimension) >= polarity*threshold);
% find the correct samples
positive(find(labels(positive) ~= 1)) = [];
negative(find(labels(negative) ~= -1)) = [];
corrects = [positive; negative];
weights(corrects) = weights(corrects) .* (errorRate / (1-errorRate));
% plot current weak classifier and weighted samples
if displayTrainingProcess == 1
    figure(2)
    clf(figure(2),'reset')
    hold on
    for i = 1 : size(data,1)
        color = 'y'; % default : incorrect classfied samples
        if ~isempty(find(positive == i)) % correct classified positive samples
            color = 'r';
        end
        if ~isempty(find(negative == i)) % correct classified negative samples
            color = 'b';
        end
        plot(data(i,1), data(i,2), 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', color, 'MarkerSize', weights(i)*size(data,1)*10);
    end
    if dimension == 1
        line([threshold, threshold], [-2, 2], 'LineWidth', 4, 'Color', [.8 .8 .8])
    else
        line([-2, 2], [threshold, threshold], 'LineWidth', 4, 'Color', [.8 .8 .8])
    end
    ezpolar(@(x)1);ezpolar(@(x)2);
    axis equal
    hold off
    pause
end
 
%% Select best classifier
function [bestDim, bestThreshold, bestPolarity, bestErrorRate] = selectBestClassifier(data, labels, weights)
bestDim = 0;
bestThreshold = 0;
bestPolarity = 0;
bestErrorRate = sum(weights);
for dim = 1 : size(data,2)
    [threshold, polarity, errorRate] = buildFeatureClassifier(data(:,dim), labels, weights);
    if errorRate <= bestErrorRate
        bestDim = dim;
        bestThreshold = threshold;
        bestPolarity = polarity;
        bestErrorRate = errorRate;
    end
end
 
%% Build feature classifier
function [bestThreshold, bestPolarity, bestErrorRate] = buildFeatureClassifier(data, labels, weights)
bestThreshold = 0;
bestPolarity = 0;
bestErrorRate = sum(weights);
% sort data
[data, index] = sort(data);
labels = labels(index);
weights = weights(index);
% generate possible splitters
splitters = data(1:end-1) + 0.5 * (data(2:end) - data(1:end-1));
for i = 1 : size(splitters,1)
    threshold = splitters(i);
    for polarity = -1 : 2 : 1 % polarity is (+) / (-)
        positive = find(polarity.*data < polarity*threshold);
        negative = find(polarity.*data >= polarity*threshold);
        positive(find(labels(positive) == 1)) = []; % dimiss correct samples
        negative(find(labels(negative) == -1)) = [];
        incorrects = [positive; negative];
        errorRate = sum(weights(incorrects));
        if errorRate <= bestErrorRate
            bestThreshold = threshold;
            bestPolarity = polarity;
            bestErrorRate = errorRate;
        end
    end
end

文件列表(点击上边下载按钮,如果是垃圾文件请在下面评价差评或者投诉):

数据聚类源码程序/
数据聚类源码程序/adaboostDemo/
数据聚类源码程序/adaboostDemo/adaboostDemo.m

关键词: 源码 程序 数据

Top_arrow
回到顶部
联系方式| 版权声明| 招聘信息| 广告服务| 银行汇款| 法律顾问| 兼职技术| 付款方式| 关于我们|
网站客服网站客服 程序员兼职招聘 程序员兼职招聘
沪ICP备19040327号-3
公安备案号:沪公网安备 31011802003874号
库纳格流体控制系统(上海)有限公司 版权所有
Copyright © 1999-2014, GUSUCODE.COM, All Rights Reserved