协方差、样本协方差、协方差矩阵、相关系数详解(python代码)

什么是协方差(covariance)?(延伸到 协方差矩阵、多元高斯分布、PCA) 协方差是一个统计量,是对一个样本的某一统计特性给出的一个估算量。 常见统计量 阅读详情
AI摘要

本文详细介绍了协方差与相关系数的概念及其在统计学中的应用,包括协方差、样本协方差、协方差矩阵的定义与计算方法,并通过实例展示了如何使用Python中的Numpy和Pandas库进行相关计算。

对于一个随机变量的分布特征,可以由均值、方差、标准差等进行描述。而对于两个随机变量的情况,有协方差和相关系数来描述两个随机变量的相互关系。

本文主要参考概率论与数理统计的教科书,整理了协方差、样本协方差、协方差矩阵、相关系数的概念解释和代码。

协方差(covariance)

协方差的概念来自概率论,实际应用中的样本协方差则与统计学概念有关。

协方差反应了随机变量 X 、 Y X、Y X、Y之间“协同”变化的关系。也可以说,协方差在某种意义上给出了两个变量线性相关性的强度以及这些变量的尺度。

当 Y Y Y就是 X X X时, c o v ( X , Y ) = c o v ( X ) = v a r ( X ) cov(X,Y)=cov(X)=var(X) cov(X,Y)=cov(X)=var(X)协方差即为方差,这就是我们称其为协方差的原因。1

协方差定义:
c o v ( X , Y ) = E { [ X − E ( X ) ] [ Y − E ( Y ) ] } = E ( X Y ) − E ( X ) E ( Y ) cov(X,Y)=E\{[X-E(X)][Y-E(Y)]\} =E(XY)-E(X)E(Y) cov(X,Y)=E{ [X−E(X)][Y−E(Y)]}=E(XY)−E(X)E(Y)

直观解释

若 c o v ( X , Y ) > 0 cov(X,Y)>0 cov(X,Y)>0,即事件 { X > E ( X ) } ∩ { Y > E ( Y ) } \{X>E(X)\}\cap\{Y>E(Y)\} { X>E(X)}∩{ Y>E(Y)}或 { X < E ( X ) } ∩ { Y < E ( Y ) } \{X<E(X)\}\cap\{Y<E(Y)\} { X<E(X)}∩{ Y<E(Y)}发生的可能性更大,说明 X X X和 Y Y Y均有同时大于或同时小于各自平均值的趋势。

协方差的绝对值如果很大,则意味着变量值变化很大,并且它们同时距离各自的均值很远。

如果协方差是正的,那么两个变量都倾向于同时取得相对较大的值。如果协方差是负的,那么其中一个变量倾向于取得相对较大的值的同时,另一个变量倾向于取得相对较小的值,反之亦然。

样本协方差(sample covariance)

实际应用中,总体 X X X的均值

样本协方差矩阵的定义与计算   协方差矩阵是用来衡量一组随机变量之间的线性关系的矩阵。我们都知道,对于$n$个随机变量$X_1,X_2,...,X_n$,总体协方差矩阵定义为: $ \left[ \begin{matrix} D(X_1)&Cov(X_1,X_2)&\dots&Cov(X_1,X_n)\\ Cov(X_2,X_1)&D(X_2)&\dots&Cov(X... 阅读详情

相关推荐

【AI概念】协方差(covariance)与相关系数(correlation)是什麽?有什么区别?在机器学习中担任什么角色?|协方差矩阵、马氏距离、归一化|主成分分析PCA、代码实现与可视化、实际应用

大家好,我是爱酱。本篇將會系统讲解协方差(covariance,又称共变异数)与相关系数(correlation coefficient,或简称correlation)的定义、公式、几何意义、实际案例和应用场景。内容适合初学者和进阶读者,分步解释,配合公式和可视化示例。 注:本文章含大量数学算式、详细例子说明及大量代码演示,大量干货,建议先收藏再慢慢观看理解。新频道发展不易,你们的每个赞、收藏跟转发都是我继续分享的动力!

AI人工智能爱酱~你的AI学习好帮手~ 1813

【统计模型】数据样本协方差和相关性

通过计算样本协方差和相关性,我们可以了解两个变量之间的关系,以及它们是如何随着变化而变化的。这对于发现变量之间的关联、预测未来趋势以及进行决策和分析都非常重要。

Fanjufei的博客 493

用python学习协方差矩阵和热力图

协方差矩阵(Covariance Matrix)是一个用于描述多个变量之间关系的矩阵。对于一组多维数据,协方差矩阵的每个元素表示数据集中不同变量之间的协方差。协方差公式:对角线上的元素是每个变量的方差。非对角线上的元素是不同变量之间的协方差。可以选择不同的色图(cmap)来表示协方差的大小,coolwarm是常用的一种。

2301_76886868的博客 1569

ROS中costmap_converter插件的配置与使用

在学习TEB算法的过程中,作者提供了一个插件用于转换代价地图,原始的代价地图是由栅格地图中单元格组成,用于表示障碍物,但单元格占用的计算资源较大,故采用插件将单元格转换成点,线,多边形表示。 插件激活 rospack plugins --attrib=plugin costmap_converter 配置cfg文件 在导航配置文件下,新建costmap_converter_params.yaml #######################################################

weixin_42621524的博客 5905

<202505高项>《【VIP合集】 软考高项(第四版)十大知识域、五大过程组、ITTO输入输出工具、项目绩效域》

软考高项(第四版)十大知识域、五大过程组、ITTO输入输出工具、项目绩效域

Else 的博客 1854

从Bit到MCS:深度解析Xilinx FPGA程序固化流程与文件选型

本文深度解析Xilinx FPGA程序从调试到量产的固化流程,重点对比BIT、BIN、MCS和ELF四种文件格式的特性与适用场景。针对工业级应用,详细讲解MCS文件的生成优化、Flash烧录避坑指南及三级验证流程,并分享多镜像管理、加密安全等高级技巧,帮助开发者提升FPGA量产可靠性。

weixin_30924239的博客 324

若依框架集成saToken

若依版本3.8.8,鉴权采用security框架,这种框架太重,项目中想实现对外提供接口功能。尽快减少修改原框架的逻辑基础上,采用satoken实现。

sccd2009的专栏 1482

Vc出现不能连接到一个或多个 vCenter server 系统:http://vcenter-vx.utibet.edu.cn:443/sdk

问题描述: 解决办法: esxi6.7的bug 第一种通过升级系统来解决问题 第二种是通过删除数据来解决 第一步:进入到esxi的shell 第二步查看其中的服务开启状态 第二步 df -h 第三步 /opt/vmware/vpostgres/current/bin/psql -d VCDB -U postgres 第四步 SELECT nspname || '.' || relname AS "relation", pg_size_pretty(pg_total_relation_size(C.oi

m0_49328536的博客 9900

基于JAVA的GB28181实现之SIP服务核心组件解析

本文深入解析了基于JAVA的GB28181标准中SIP服务的核心组件实现,包括协议栈选择、消息处理引擎、事务管理器等关键技术点。通过实战案例分享海康威视、大华设备的特殊处理技巧,并提供性能优化和常见问题排查指南,帮助开发者高效构建稳定的视频监控系统。

weixin_30892037的博客 264

LSTM 深度解析从原理推演到现代序列建模的演进

LSTM(Long Short-Term Memory,长短期记忆网络)是一种特殊的递归神经网络(RNN),它通过引入记忆单元(Cell State)来解决普通RNN在长序列数据处理中的梯度消失问题。LSTM可以在长时间的序列中捕捉依赖关系,是一种非常适合处理时间序列、自然语言处理、语音识别等任务的深度学习模型。在LSTM中,记忆单元(Cell State)是一个贯穿整个序列的数据通道,用于存储和传递关键信息。记忆单元在LSTM的时间步之间保持信息的持久性,可以看作是一个长期记忆的“容器”。

hello.reader 3万+

2020年大连理工大学《单片机原理及应用》期末试卷

2020年大连理工大学《单片机原理及应用》期末试卷 此试卷为大连理工大学2020年《单片机原理及应用》期末试卷; 大连理工大学2020年《单片机原理及应用》期末试卷; 大连理工大学2020年《单片机原理及应用》期末试卷。 ...

无名何许人的博客 1479

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: 欧式距离、曼哈顿距离、余弦相似度(python代码)
下一篇: 马氏距离详解(数学原理、适用场景、应用示例代码)
虾米小馄饨
博客等级 码龄12年 1万+粉丝 60原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值