吴恩达机器学习之异常检测与推荐系统的MATLAB实现(对应ex8练习)

这篇博客介绍了如何使用MATLAB实现吴恩达机器学习课程中的异常检测算法,包括estimateGaussian.m(求均值和标准差)、multivariateGaussian.m(计算样本概率)和selectThreshold.m(选择最佳阈值epsilon)。文章强调了高斯分布和概率阈值选择的重要性,并指出异常检测的核心在于处理协方差数据。同时,博客还提及了推荐系统的相关内容。
摘要由CSDN通过智能技术生成

异常检测

前言:异常检测算法的重点是高斯分布的实现与概率阈值的选取。高斯分布是异常检测的核心,其实现就是依靠高斯分布来实现的;而概率阈值的选取决定了整个异常检测算法的实现好坏。下面给出MATLAB实现。

estimateGaussian.m

这部分实质是求均值u和标准差σ。直接将公式实现即可。下面先给出公式。

代码实现也没什么难点,细心点即可。 

function [mu sigma2] = estimateGaussian(X)
%ESTIMATEGAUSSIAN This function estimates the parameters of a 
%Gaussian distribution using the data in X
%   [mu sigma2] = estimateGaussian(X), 
%   The input X is the dataset with each n-dimensional data point in one row
%   The output is an n-dimensional vector mu, the mean of the data set
%   and the variances sigma^2, an n x 1 vector
% 

% Useful variables
[m, n] = size(X);

% You should return these values correctly
mu = zeros(n, 1);
sigma2 = zeros(n, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the mean of the data and the variances
%               In particular, mu(i) should contain the mean of
%               the data for the i-th feature and sigma2(i)
%               should contain variance of the i-th feature.
%
mu = 1 / m * sum(X);%计算均值
sigma2 = 1 / (m - 1) * sum((bsxfun(@minus,X,mu) .^ 2));%计算方差
% =======
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值