Python外卷(8)--pdist, squareform

pdist, squareform

  • 1.pdist, squareform使用例子
  • 2.通過矩陣的四則運算實現上述pdist, squareform

scipy.spatial.distance 距離計算庫中有兩個函數:pdist, squareform,用于計算樣本對之間的歐式距離,并且將樣本間距離用方陣表示出來。

(題外話)
SciPy: 基于Numpy,提供方法(函數庫)直接計算結果,封裝了一些高階抽象和物理模型
Numpy: 來存儲和處理大型矩陣,比Python自身的嵌套列表(nested list structure)結構要高效的多,本身是由C語言開發。
Pandas: 基于NumPy 的一種工具,該工具是為了解決數據分析任務而創建的。
參考 資料:https://www.jianshu.com/p/32cb09d84487

(回正題)

1.pdist, squareform使用例子

pdist, squareform的操作基于numpy,

>>> import numpy as np
>>> from scipy.spatial.distance import pdist, squareform
>>> x=np.array([[1,1,1],[2,2,2],[4,4,4]])  #三個一維向量:x1=[1,1,1] x2=[2,2,2],x3=[4,4,4]>>> Dis=pdist(x)
>>> Dis             # d(x1,x2)=sqrt(3)=1.7 ,d(x1,x3)=sqrt(27),d(x2,x3)=sqrt(8)
array([1.73205081, 5.19615242, 3.46410162])>>> D=squareform(Dis)
array([[0.        , 1.73205081, 5.19615242],     # d(x1,x1),d(x1,x2),d(x1,x3)[1.73205081, 0.        , 3.46410162],	 # d(x2,x1),d(x2,x2),d(x2,x3)[5.19615242, 3.46410162, 0.        ]])    # d(x3,x1),d(x3,x2),d(x3,x1)

因為距離度量具有對稱性,即d(x1,x2)=d(x2,x1)d(x1,x2)=d(x2,x1)d(x1,x2)=d(x2,x1),所以上述矩陣為一個對稱陣。

2.通過矩陣的四則運算實現上述pdist, squareform

有三個三維樣本:x1=[1,1,1],x2=[2,2,2]x3=[4,4,4],樣本之間距離的方陣為:

D=[d(x1,x1)d(x1,x2)d(x1,x3)d(x2,x1)d(x2,x2)d(x2,x3)d(x3,x1)d(x3,x2)d(x3,x3)]D=\begin{bmatrix} d(x1,x1)& d(x1,x2) & d(x1,x3)\\ d(x2,x1)& d(x2,x2) & d(x2,x3)\\ d(x3,x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begin{bmatrix} x_1x_1^T,& x_1x_1^T ,& x_1x_1^T\\ x_2x_2^T,& x_2x_2^T ,& x_2x_2^T\\ x_3x_3^T,& x_3x_3^T ,& x_3x_3^T \end{bmatrix}+ \begin{bmatrix} x_1x_1^T,& x_1x_1^T ,& x_1x_1^T\\ x_2x_2^T,& x_2x_2^T ,& x_2x_2^T\\ x_3x_3^T,& x_3x_3^T ,& x_3x_3^T \end{bmatrix}^T-2 \begin{bmatrix} x_1x_1^T,& x_1x_2^T ,&x_1x_3^T\\ x_2x_1^T,& x_2x_1^T ,&x_2x_3^T\\ x_3x_1^T,& x_3x_2^T ,& x_3x_3^T\end{bmatrix} =???x1?x1T?,x2?x2T?,x3?x3T?,?x1?x1T?,x2?x2T?,x3?x3T?,?x1?x1T?x2?x2T?x3?x3T?????+???x1?x1T?,x2?x2T?,x3?x3T?,?x1?x1T?,x2?x2T?,x3?x3T?,?x1?x1T?x2?x2T?x3?x3T?????T?2???x1?x1T?,x2?x1T?,x3?x1T?,?x1?x2T?,x2?x1T?,x3?x2T?,?x1?x3T?x2?x3T?x3?x3T?????

=>[x1x1T,x1x1T,x1x1Tx2x2T,x2x2T,x2x2Tx3x3T,x3x3T,x3x3T]=> \begin{bmatrix} x_1x_1^T,& x_1x_1^T ,& x_1x_1^T\\ x_2x_2^T,& x_2x_2^T ,& x_2x_2^T\\ x_3x_3^T,& x_3x_3^T ,& x_3x_3^T \end{bmatrix} =>???x1?x1T?,x2?x2T?,x3?x3T?,?x1?x1T?,x2?x2T?,x3?x3T?,?x1?x1T?x2?x2T?x3?x3T?????
矩陣對應元素相乘,行復制

[x1x1T,x1x2T,x1x3Tx2x1T,x2x1T,x2x3Tx3x1T,x3x2T,x3x3T]=[x1x2x3]?[x1x2x3]T\begin{bmatrix} x_1x_1^T,& x_1x_2^T ,&x_1x_3^T\\ x_2x_1^T,& x_2x_1^T ,&x_2x_3^T\\ x_3x_1^T,& x_3x_2^T ,& x_3x_3^T\end{bmatrix}= \begin{bmatrix} x1\\ x2\\ x3\end{bmatrix}* \begin{bmatrix} x1\\ x2\\ x3\end{bmatrix}^T ???x1?x1T?,x2?x1T?,x3?x1T?,?x1?x2T?,x2?x1T?,x3?x2T?,?x1?x3T?x2?x3T?x3?x3T?????=???x1x2x3????????x1x2x3????T

程序實現:

X=np.array([[1,1,1],[2,2,2],[3,3,3]])
X2=(X*X).sum(1)*np.ones([3,3])
XXT=np.matmul(X,X.T)
D=X2+X2.T-2*XXT
D=np.sqrt(D2)
print (D)# 輸出
[[ 0.          1.73205081  5.19615242][ 1.73205081  0.          3.46410162][ 5.19615242  3.46410162  0.        ]]

**溫馨提示:**上述矩陣為距離矩陣,在實際應用的過程中,注意使用的是距離的平方,還是距離。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445104.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445104.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445104.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

模擬進程調度

功能 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define OK 1 #define ERROR 0 #define TimeSlice 1 #define Infinity 10 //INT_MAX#define NAME_M…

gdb調試多進程和多線程命令

1. 默認設置下&#xff0c;在調試多進程程序時GDB只會調試主進程。但是GDB&#xff08;>V7.0&#xff09;支持多進程的 分別以及同時 調試&#xff0c;換句話說&#xff0c;GDB可以同時調試多個程序。只需要設置follow-fork-mode(默認值&#xff1a;parent)和detach-on-fork…

python外卷(10)--取整

"取整"那些事1.python 內置函數1.1int()--向下取整1.2round()--四舍五入2.math模塊取整函數2.1 math.floor()--向下取整2.2 math.ceil()--向上取整2.3 math.modf()--分別取小數部分和整數部分3.numpy模塊取整函數3.1 numpy.floor()--向下取整3.2 numpy.ceil()--向上取…

模擬銀行家算法

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define true 1 #define false 0 #define OK 1 #define ERROR 0 #define RESOURCE_NUM …

Lua 與 C混合編程 .

本文通過程序實例說明C調用lua腳本和lua調用C的方法: 先建立一個 test.c文件: #include <stdio.h> #include <stdlib.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #pragma comment(lib, "lua5.1.lib&qu…

模擬固定分區分配

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PCB #define Status int…

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 list.h #ifndef _List_h_ #define _List_h_#include "Data.h"//******* 鏈表 *******// Status InitLinkList(LinkList *L); void PCBAssign(PCBType *e1, PCBType e2); Status GetElemt_L(LinkList L,int i,PCBType *e); Status ListIn…

python模塊(4)-Collections

collections1.collection.counter(list)2.collections.defaultdict()3.collection.dequecollections是Python內建的一個集合模塊&#xff0c;提供了許多有用的集合類。collections在python官方文檔中的解釋是High-performance container datatypes1.collection.counter(list) …

js知識點匯總

1.本門課的作用&#xff08;JavaScript的作用&#xff09;所有基于Web的程序開發基礎 2.一種計算機客戶端腳本語言&#xff0c;主要在Web瀏覽器解釋執行。 3.瀏覽器中Javascript&#xff0c;用于與用戶交互&#xff0c;以及實現頁面中各種動態特效 4.在HTML文件中&#xff0…

redis——內存概述

Redis通過自己的方法管理內存,&#xff0c;主要方法有zmalloc(),zrealloc()&#xff0c; zcalloc()和zfree(), 分別對應C中的malloc(), realloc()、 calloc()和free()。相關代碼在zmalloc.h和zmalloc.c中。 Redis自己管理內存的好處主要有兩個&#xff1a;可以利用內存池等手段…

Windows下如何用C語言清空特定文件夾中的所有文件

#include "iostream.h" //由于該博客系統發布是不能顯示正常&#xff0c;代碼如需調試&#xff0c;只需將改成""即可 #include "string.h" #include "stdlib.h" #include "time.h" #include "math.h" #include…

MachineLearning(5)-去量綱:歸一化、標準化

去量綱&#xff1a;歸一化、標準化1.歸一化(Normalization)1.1 Min-Max Normalization1.2 非線性Normalization2.標準化(Standardlization)2.1 Z-score Normalization3.標準化在梯度下降算法中的重要性本博文為葫蘆書《百面機器學習》閱讀筆記。去量綱化 可以消除特征之間量綱的…

GDB調試技術(一)

啟動GDB的方法有以下幾種: 1、gdb <program> program也就是你的執行文件,一般在當然目錄下。 2、gdb <program> core 用gdb同時調試一個運行程序和core文件,core是程序非法執行后core dump后產生的文件。 3、

GDB調試技術(二)

1) 恢復程序運行和單步調試 當程序被停住了,你可以用continue命令恢復程序的運行直到程序結束,或下一個斷點到來。也可以使用step或next命令單步跟蹤程序。 continue [ignore-count] c [ignore-count] fg [ignore-count] 恢復程序運行,直到程序結束,或是下一個斷點到…

關于Java中String的問題

String 對象的兩種創建方式&#xff1a; String str1 "abcd";//先檢查字符串常量池中有沒有"abcd"&#xff0c;如果字符串常量池中沒有&#xff0c;則創建一個&#xff0c;然后 str1 指向字符串常量池中的對象&#xff0c;如果有&#xff0c;則直接將 st…