机器学习(二)非参数估计matlab例程

机器学习(二)非参数估计matlab例程

2018/2/19
by ChenjingDing


问题描述:
分别使用K近邻和核函数的方法。为输入样本 xˆ x ^ 估计概率密度函数。 xˆ x ^ = linspace(-5, 5, 100);
假设 xtrai x t r a i = random(‘norm’, 0, 1, 1, 100)服从高斯分布,则估计出的概率密度也应该服从高斯分布。


apply.m

close all;
clc;

%get fixed K of knn and fixed h of kde
p = parameters();

disp('Question: Kernel/K-Nearest Neighborhood Density Estimators');

% Produce the random samples
samples = random('norm', 0, 1, 1, 100);

% Compute the original normal distribution
realDensity = gauss1D(0, 1, 100, 5);

% Estimate the probability density using the KDE
estDensity = kde(samples, p.h);

% plot results
figure;
plot(estDensity(1, :), estDensity(2, :), 'r', 'LineWidth', 1.5);
hold on;
plot(realDensity(1, :), realDensity(2, :), 'b', 'LineWidth', 1.5);
legend('KDE Estimated Distribution', 'Real Distribution');
hold off;

% Estimate the probability density using KNN
estDensity = knn(samples, p.k);

% Plot the distributions
figure;
plot(estDensity(1, :), estDensity(2, :), 'r', 'LineWidth', 1.5);
hold on;
plot(realDensity(1, :), realDensity(2, :), 'b', 'LineWidth', 1.5);
legend('KNN Estimated Distribution', 'Real Distribution');
hold off;

knn.m

function estDensity = knn(samples, k)
    % compute density estimation from samples with KNN
    % Input
    %  samples    : DxN matrix of data points
    %  k          : number of neighbors
    % Output
    %  estDensity : estimated density in the range of [-5, 5]

    % Compute the number of the samples created
    N = length(samples);

    % Create a linearly spaced vector
    pos = linspace(-5, 5, 100);

    % Create two big matrices to avoid for loops
    x = repmat(pos, N, 1);
    samples = repmat(samples', 1, length(pos));

    % Sort the distances so that we can choose the k-th point
    dists = sort(abs(x-samples), 1);

    % Estimate the probability density using the k-NN density estimation
    % dists(k, :) = V/2;    
    res = (k/(2*N)) ./ dists(k, :);

    % Form the output variable
    estDensity = [pos; res];

end

kde.m

function estDensity = kde(samples, h)
    % compute density estimation from samples with KDE
    % Input
    %  samples    : DxN matrix of data points
    %  h          : (half) window size/radius of kernel
    % Output
    %  estDensity : estimated density in the range of [-5,5]

    % Compute the number of samples created
    N = length(samples);

    % Create a linearly spaced vector
    pos = linspace(-5, 5, 100);

    % Create two big matrices to avoid for loops
    x = repmat(pos, N, 1);
    samples = repmat(samples', 1, length(pos));

    % Estimate the density from the samples using a kernel density estimator
    % 参考机器学习(二)非参数估计核函数法 高斯函数一例
    res = sum(exp(-(x-samples).^2./(2*h^2)), 1) ./ (sqrt(2*pi)*h*N);

    % Form the output variable
    estDensity = [pos; res];

end

gauss1D.m

function [realDensity] = gauss1D(m, v, N, w)

    pos = (-w:(2*w/N):w-w/N);
    meanV = repmat(m,N,1)';

    aux = pos - meanV;
    insE = (aux.*aux)./(v^2)*(-0.5);
    norm = 1/(v*sqrt(2*pi));

    res = norm.*exp(insE);

    realDensity = [pos;res];

end

parameter.m

function p = parameters()
    p.k = 30; %knn neighbors
    p.h = 0.3; %kde windowsize/radius
end

结果如下:



图8 核函数估计概率密度结果(红色曲线为估计值,蓝色曲线为理想值)

图9 K近邻法估计概率密度结果(红色曲线为估计值,蓝色曲线为理想值)


  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值