机器学习(二)非参数估计matlab例程
机器学习(二)非参数估计matlab例程
2018/2/19
by ChenjingDing
问题描述:
分别使用K近邻和核函数的方法。为输入样本xˆx^\widehat{x}估计概率密度函数。xˆx^\widehat{x} = linspace(-5, 5, 100);
假设xtraixtraix_{trai} = 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 createdN = length(samples);% Create a linearly spaced vectorpos = linspace(-5, 5, 100);% Create two big matrices to avoid for loopsx = repmat(pos, N, 1);samples = repmat(samples', 1, length(pos));% Sort the distances so that we can choose the k-th pointdists = 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 variableestDensity = [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 createdN = length(samples);% Create a linearly spaced vectorpos = linspace(-5, 5, 100);% Create two big matrices to avoid for loopsx = 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 variableestDensity = [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 neighborsp.h = 0.3; %kde windowsize/radius
end
结果如下:
图8 核函数估计概率密度结果(红色曲线为估计值,蓝色曲线为理想值)
图9 K近邻法估计概率密度结果(红色曲线为估计值,蓝色曲线为理想值)
机器学习(二)非参数估计matlab例程相关推荐
- 视觉机器学习20讲-MATLAB源码示例(9)-SVM算法
视觉机器学习20讲-MATLAB源码示例(9)-SVM算法 1. SVM算法 2. Matlab仿真 3. 仿真结果 4. 小结 1. SVM算法 SVM(support Vector Mac)又称为 ...
- 用matlab做纹理合成,图像纹理合成的matlab例程
图像纹理合成的matlab例程 关于图像纹理合成的 Matlab 例程纹理是普遍存在的视觉现象,其可以描述地形.植物.矿石.纤维和皮肤等等物体的表面特征.纹理结构在图像中反映其图像像素取值的空间变化情 ...
- 视觉机器学习20讲-MATLAB源码示例(11)-流形学习算法
视觉机器学习20讲-MATLAB源码示例(11)-流形学习算法 1. 流形学习算法 2. Matlab仿真 3. 仿真结果 4. 小结 1. 流形学习算法 流形学习是一类借鉴了拓扑流形概念的降维方法, ...
- 单位斜变函数matlab,实验二 用MATLAB实现线性系统的时域分析
实验二基于MATLAB的线性系统时域分析 [实验目的] 1.研究线性系统在典型输入信号作用下的暂态响应: 2.熟悉线性系统的暂态性能指标: 3.研究二阶系统重要参数阻尼比ξ对系统动态性能的影响: 4. ...
- 机器学习 二分类分类阈值_分类指标和阈值介绍
机器学习 二分类分类阈值_分类指标和阈值介绍_weixin_26752765的博客-CSDN博客 机器学习 二分类分类阈值_分类指标和阈值介绍_weixin_26752765的博客-CSDN博客
- matlab在绘图区加格栅,实验二(2) MATLAB绘图
实验二(2)MATLAB绘图 一.实验目的 1.掌握matlab二维图形的绘制方法, 会对所绘图形进行加格栅,图例和标注等一些简单的处理: 2.了解对数坐标图的绘制方法: 3.了解符号函数(显函数.隐 ...
- 利用MATLAB进行系统时域分析,实验二 利用matlab进行系统的时域分析
实验二 利用matlab进行系统的时域分析 实验二 利用MATLAB进行系统的时域分析 1.实验目的 在理论学习的基础上,通过本实验熟悉LTI连续时间系统的时域分析方法, 熟悉系统的零输入响应.零状态 ...
- 2018.9.10.Matlab实验二:Matlab基本运算
实验二:Matlab基本运算 一.实验任务和目的 1. 掌握变量的定义与数据类型. 2. 掌握变量的初始化方法. 3. 掌握数组.多维数组与子数组的定义.存储.赋值.变换. 4. 掌握逻辑数组的用法. ...
- LibLinear(SVM包)使用说明之(二)MATLAB接口
LibLinear(SVM包)使用说明之(二)MATLAB接口 zouxy09@qq.com http://blog.csdn.net/zouxy09 一.介绍 LIBLINEAR是一个简单的求解大规 ...
最新文章
- ps背景不变换字_分享五个超级实用的PS小技巧
- Mongo北京大会3月3号召开!报名抢注火爆进行中!(免费)
- HDU-1285 确定比赛名次 拓扑排序
- 学习《Building Applications with FME Objects》 之二 使用Sessions(会话)
- 【鸿蒙 HarmonyOS】Ability 简介 ( 简介 | 创建应用 | Page Ability 生命周期 )
- mybatis教程--延迟加载详解
- 使用Spring Boot和Project Reactor处理SQS消息
- Nodejs学习笔记(四)——http协议与服务器
- 【英语学习】【Level 08】U01 Let's Read L3 The classics are always in
- 【SKILLS】About the phonetics
- linux关防火墙和SElinux
- 华为和新华三OSPF单区域配置
- c#控制台应用程序读取 config
- 嵌入式Linux书籍清单
- 面试前的准备和注意事项(非常详细)
- pyodbc linux 乱码,python-无法在Linux上安装pyodbc
- Html论坛提问页面,技术分享 - 制作论坛发帖页面(采用html()方式、操作节点的方式)...
- 【代码开发】neuron_poker安装及简单使用
- 【论文翻译】基于图关注网络的异构网络类型感知锚链路预测
- 日本警方称地震造成3676人死亡7843人下落不明