基于matlab实现AdaBoost数据分类

简介: 基于matlab实现AdaBoost数据分类

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法       神经网络预测       雷达通信      无线传感器        电力系统

信号处理              图像处理               路径规划       元胞自动机        无人机

⛄ 内容介绍

在不平衡数据集中,由于少类样本和多类样本的不平衡,在分类过程中容易产生难以分类和错误分类的现象.针对不平衡数据集的分类特点,设计出一种组合分类器,适用于不平衡数据集的分类

⛄ 完整代码

%% -------------------------------------------------------------------

% Descrption : AdaBoost Classification Demo


% Reference : http://staff.ustc.edu.cn/~xjchen99/teaching/ImageUnderstanding/Robust%20real-time%20face%20detection.ppt

%% -------------------------------------------------------------------

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

⛄ 运行结果


⛄ 参考文献

[1]董庆伟. "基于Adaboost算法的不平衡数据集分类效果研究." 长春师范大学学报 41.6(2022):4.

❤️部分理论引用网络文献,若有侵权联系博主删除
❤️ 关注我领取海量matlab电子书和数学建模资料



相关文章
|
5天前
|
算法 数据安全/隐私保护
matlab程序,傅里叶变换,频域数据,补零与不补零傅里叶变换
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
|
5天前
|
数据安全/隐私保护
matlab 曲线光滑,去毛刺,去离群值,数据滤波,高通滤波,低通滤波,带通滤波,带阻滤波
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
|
5天前
|
数据安全/隐私保护
时域与频域数据互相转换,傅里叶变换与逆傅里叶变换,matlab程序,时域转频域
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
|
7天前
|
计算机视觉
MATLAB用Lasso回归拟合高维数据和交叉验证
MATLAB用Lasso回归拟合高维数据和交叉验证
|
14天前
|
SQL 移动开发 算法
MATLAB改进模糊C均值聚类FCM在电子商务信用评价应用:分析淘宝网店铺数据|数据分享
MATLAB改进模糊C均值聚类FCM在电子商务信用评价应用:分析淘宝网店铺数据|数据分享
|
15天前
|
机器学习/深度学习 数据可视化
Matlab决策树对空气质量和天气温度及天气数据做交通出行推荐预测|数据分享
Matlab决策树对空气质量和天气温度及天气数据做交通出行推荐预测|数据分享
|
15天前
|
数据挖掘 数据库
数据分享|MATLAB、R基于Copula方法和k-means聚类的股票选择研究上证A股数据
数据分享|MATLAB、R基于Copula方法和k-means聚类的股票选择研究上证A股数据
|
15天前
|
计算机视觉
MATLAB偏最小二乘回归(PLSR)和主成分回归(PCR)分析光谱数据
MATLAB偏最小二乘回归(PLSR)和主成分回归(PCR)分析光谱数据
|
16天前
|
数据采集 算法 数据可视化
MATLAB、R用改进Fuzzy C-means模糊C均值聚类算法的微博用户特征调研数据聚类研究
MATLAB、R用改进Fuzzy C-means模糊C均值聚类算法的微博用户特征调研数据聚类研究
|
19天前
|
机器学习/深度学习 数据可视化 网络架构
Matlab用深度学习循环神经网络RNN长短期记忆LSTM进行波形时间序列数据预测
Matlab用深度学习循环神经网络RNN长短期记忆LSTM进行波形时间序列数据预测

热门文章

最新文章