高端網(wǎng)網(wǎng)站建設(shè)蘇州百度
實(shí)驗(yàn)開始前先配置環(huán)境
以實(shí)驗(yàn)室2023安裝的版本為例:
1、安裝anaconda:(anaconda自帶Python,安裝了anaconda就不用再安裝Python了)
下載并安裝 Anaconda3-2022.10-Windows-x86_64.exe
自己選擇安裝路徑,其他使用默認(rèn)選項。
(1)在“Advanced Installation Options”中,
勾選“Add Anaconda3 to my PATH environment variable.”(“添加Anaconda至我的環(huán)境變量。”)。
(2)勾選“Register Anaconda3 as my default Python 3.9”。
2、安裝pycharm
下載并安裝 pycharm-community-2022.2.4.exe?
3、打開cmd窗口,輸入以下命令
conda create -n ?DMEv ?pip python=3.8
?記住DMEV所在的磁盤路徑
# 如需刪除環(huán)境,使用命令 conda remove -n DMEv ? ?--all
?安裝要用到的Python庫:
activate ? DMEv ?
pip install numpy==1.20.0 --index-url https://mirrors.aliyun.com/pypi/simple/
pip install matplotlib==3.3.4 --index-url https://mirrors.aliyun.com/pypi/simple/
pip install opencv_python==4.4.0.40 --index-url https://mirrors.aliyun.com/pypi/simple/
pip install scipy==1.6.0 --index-url https://mirrors.aliyun.com/pypi/simple/
pip install scikit-learn==0.24.1 --index-url https://mirrors.aliyun.com/pypi/simple/?
pip install h5py==2.10.0 --index-url https://mirrors.aliyun.com/pypi/simple/?
pip install mnist==0.2.2 --index-url https://mirrors.aliyun.com/pypi/simple/?
4、測試
在Pycharm中創(chuàng)建項目時,DMEV所在的路徑下選擇python.exe即可
在Pycharm中新建項目,配置 interpreter,運(yùn)行以下代碼:(沒有報錯,則導(dǎo)入成功)
import cv2 as cv
import numpy as np
from sklearn.decomposition import PCA
import mnist
import matplotlib.pyplot as plt?
?
實(shí)驗(yàn)1 數(shù)據(jù)
一、實(shí)驗(yàn)?zāi)康?/p>
(1)練習(xí)和掌握python的基本使用。
(2)理解數(shù)據(jù)類型、數(shù)據(jù)質(zhì)量、數(shù)據(jù)預(yù)處理、相似性和相異性度量的概念
(3)理解各種相似性和相異性度量(測度)及其含義,并且能編程計算。
二、實(shí)驗(yàn)內(nèi)容
1編程實(shí)現(xiàn)任意給定兩個相同維度的向量之間的歐氏距離計算函數(shù)dist_E(x,y)。
輸入:兩個任意k維向量x和y,其中k的值隨由數(shù)據(jù)決定。如x=[3,20,3.5], y=[-3,34,7]。
import numpy as npdef dist_E(vect1, vect2):return np.sqrt(sum(np.power((vect1-vect2),2)))if __name__ == "__main__":x=np.array([3,20,3.5])y=np.array([-3,34,7])dist=dist_E(x,y)print(dist)
2編程實(shí)現(xiàn)任意給定兩個相同維度的向量之間的夾角余弦相似度計算函數(shù)sim=sim_COS(x,y)。輸入:兩個任意k維向量x和y,其中k的值由數(shù)據(jù)決定。
import numpy as npdef sim_COS(x, y):num = x.dot(y.T)denom = np.linalg.norm(x) * np.linalg.norm(y)return num / denomif __name__ == "__main__":x=np.array([3, 2, 0, 5, 0, 0, 0, 2, 0, 0])y=np.array([1, 0, 0, 0, 0, 0, 0, 1, 0, 2])sim=sim_COS(x,y)print(sim)
3編程實(shí)現(xiàn)任意給定兩個相同維度的布爾向量之間的Jaccard系數(shù)計算函數(shù)dist1=dist_Jaccard(x,y)。
import numpy as npdef sim_Jaccard(vect1, vect2):sim=-1if(vect1.size!=vect2.size):print("length of input vectors must agree")else:ind1=np.logical_and(vect1==1,vect2==1)ind2=np.logical_or(vect1==1,vect2==1)x=vect1[ind1]y=vect2[ind2]n1=np.size(x)n2=np.size(y)sim=n1/n2return simif __name__ == "__main__":x=np.array([1, 0, 0, 0, 0, 0, 1, 0, 0, 0])y=np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])dist=sim_Jaccard(x,y)print(dist)
4編程實(shí)現(xiàn)任意給定兩個相同維度的布爾向量之間的簡單匹配系數(shù)計算函數(shù)dist1=dist_SMC(x,y)。
import numpy as npdef sim_SMC(vect1, vect2):sim = -1if (vect1.size != vect2.size):print("length of input vectors must agree")else:ind0 = np.logical_and(vect1 == 0, vect2 == 0)ind1 = np.logical_and(vect1 == 1, vect2 == 1)ind2 = np.logical_or(vect1 == 1, vect2 == 1)x = vect1[ind1]y = vect1[ind2]z=vect1[ind0]n1 = np.size(x)n2 = np.size(y)n3 = np.size(z)sim = (n1+n3) / (n2+n3)return simif __name__ == "__main__":x=np.array([1, 0, 0, 0, 0, 0, 1, 0, 0, 0])y=np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])dist=sim_SMC(x,y)print(dist)