协方差、样本协方差、协方差矩阵、相关系数详解(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学习好帮手~ 1819

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

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

Fanjufei的博客 498

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

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

2301_76886868的博客 1569

使用FloodRisk2插件进行洪水风险评估

入汛以来,长江流域持续的强降雨使得沿岸多地成为洪水风险极高地区,抗洪、抢险、救灾继新冠病毒之后成为又一个攻坚阵地。如何科学地评估洪水风险,是快速决策的关键。 实际生活中,根据风险是否可控,一般将洪水风险大致分为两类:一类是通过数据分析可以得出其发生的可能性和后果,如强降雨引发的区域性洪水、水库/大坝溃决洪水等。这类洪水可以通过有关资料分析和设定一定条件计算得到可能的损失后果,也就是说,风险可以预判。另一类洪水风险基本是未知的,如滑坡堵塞河道形成堰塞湖引发的洪水,无法预知事件发生的地点和规模,具体的洪水风险无

QGISClass的博客 2892

SAPABAP金色传说:关于导入和导出Excel时反复弹窗提示 SAP GUI安全性问题的解决方法

文章目录前言一、提示的到底是什么?二、关闭SAP GUI安全提示的步骤1.设置位置 前言 如果你在SAP中,经常需要将数据导出Excel,或者使用Excel进行批导数据,那么你对下面这个频繁出现的弹窗提示一定不陌生! 系统试图创建文件 C:\Users\15265\Desktop\EXPORT.XLSX 于目录 C:\Users\15265\Desktop\ 是否要授权修改父目录及其所有子目录? 不管你是不是选的记住我的决定,它必出!就很烦!那如何处理才能关闭呢? 提示:以下是本篇文章正文内容,看完快

Easystudios博客 1万+

Vmware虚拟机克隆详解:固定ip + 修改uuid等内容

分两大步 设置固定ip 克隆后修改相关配置 本文引用的其他地方的图片。会附上链接 若侵权,删 一、设置静态ip 先进行虚拟网络的编辑 接下来的操作需要管理员权限。(给权限就完事了) 接下来设置网卡: 网络和共享中心→ 更改适配器设置→VMware Network Adapter VMnet8 -> 右键属性 修改TCP/Ipv4的配置,网上常见是这种,笔者是英文的...

Unknownfuture的博客 9086

Stm32L0 STM32CUBE中UART和使用LPUART1以及串口不进中断问题解决

/* 开启串口1中断 */ LL_USART_EnableIT_RXNE(LPUART1); /* 开启串口2中断 */ LL_USART_EnableIT_RXNE(USART2);

シ❤゛甜虾的个人博客 7697

HDMI2.1接口保护指南:从浪涌损坏案例看RK3588板子的ESD设计要点

本文深入探讨了HDMI2.1接口在RK3588板子上的ESD防护设计要点,通过真实案例分析了浪涌损坏的原因及解决方案。重点介绍了TMDS信号链路的四级防护体系、TVS管选型误区以及连接器与PCB的协同设计策略,帮助工程师有效避免静电放电导致的HDMI无输出故障。

weixin_29305337的博客 458

r3live 使用前提 雷达-相机外参标定 livox_camera_lidar_calibration

港科大livox_camera_calib虽然操作方便,但是使用mid360雷达会有视角问题(投影三维点到相机),尝试了很多场景,标定效果都不理想(推荐场景是楼梯间),看来港大的更适合avia之类窄角度的雷达。如果标定效果不好的话,就使用 getExt2节点,getExt1节点只优化外参,而getExt2节点在计算的时候会将一开始计算的内参作为初值和外参一起优化。一般使用getExt1节点即可,如果在外参初值验证过,并且异常值已经剔除后,优化还是有较大的残差,那么可以使用getExt2试一试。

z1872385的博客 2403

【1】Python交叉编译到OpenHarmony标准系统运行(arm32位)

本文介绍如何Python语言如何在OpenHarmony标准系统运行,包括5.0r和4.1r以及4.0r,和未来版本的OpenHarmony版本上。 Python语言在OpenHarmony上使用,需要将Python解释器CPython移植到OpenHarmony标准系统。通过交叉编译的方式。

weixin_58069108的博客 939

LangGraph教程8:LangGraph流式处理

流式处理可显著提升基于LLM应用的响应性。LangGraph支持两种流式模式:values模式返回每一步的完整结果,适合需要完整上下文处理的场景;updates模式仅返回增量变化,适用于实时更新需求。示例代码展示了如何用create_react_agent实现这两种模式,其中values模式完整记录处理流程,updates模式逐步输出结果。通过异步迭代输出流,开发者可以灵活选择适合应用场景的流式处理方式。

Cachel Wood的博客 643

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

深度学习工具包 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")

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址