日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python的數(shù)據(jù)與matlab互通問題:SciPy_python

作者:后廠村路蔡徐坤 ? 更新時(shí)間: 2023-01-14 編程語言

python數(shù)據(jù)與matlab互通SciPy

有時(shí)候需要利用python進(jìn)行科學(xué)計(jì)算,但需要Matlab進(jìn)行交互式畫圖,因此需要掌握python與matlab數(shù)據(jù)互通的技巧:SciPy

  • SciPy 提供了與 Matlab 的交互的方法。
  • SciPy 的 scipy.io 模塊提供了很多函數(shù)來處理 Matlab 的數(shù)組。

python利用scipy.io.savemat()存儲(chǔ)日志

參考:https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html

(一)語法

scipy.io.savemat(file_name, mdict, appendmat = True, format = '5', long_field_names = False, do_compression = False, oned_as = 'row')
參數(shù)
file_name:str
      .mat 文件的名稱(如果沒有擴(kuò)展名,需要設(shè)置appendmat == True)
mdict:dict
      從中保存 matfile 變量的字典。
appendmat:布爾值,可選
      True(默認(rèn)值)將 .mat 擴(kuò)展名附加到給定文件名的末尾(如果尚不存在)。
fomat:{'5', '4'},字符串,可選
      '5'(默認(rèn)值)用于 MATLAB 5 及更高版本(至 7.2),'4' 用于 MATLAB 4 .mat 文件。
long_field_names:布爾型,可選
      False(默認(rèn)值)- 結(jié)構(gòu)中的最大字段名稱長度為 31 個(gè)字符,這是記錄的最大長度。True - 結(jié)構(gòu)中的最大字段名稱長度為 63 個(gè)字符,適用于 MATLAB 7.6+。
do_compression:布爾值,可選
      是否在寫入時(shí)壓縮矩陣。默認(rèn)為假。
oned_as :{'row', 'column'},可選
      如果是 'column',則將一維 NumPy 數(shù)組寫為列向量。如果為“行”,則將一維 NumPy 數(shù)組寫為行向量。

(二)例子

from scipy import io
import numpy as np
arr = np.arange(10)

io.savemat('arr.mat', {"vec": arr})
#如果存入多組數(shù)據(jù)
io.savemat('saveddata.mat', {'xi': xi,'yi': yi,'ui': ui,'vi': vi})

利用matlab讀取mat日志的方法

(一)載入日志文件

mat_struct=load('C:\Users\XXX\Desktop\task3-2022y01m23d-17h52m.mat');

(二)取出元素

mat_struct=load('C:\Users\XXX\Desktop\task3-2022y01m23d-17h52m.mat');
ans=mat_struct.ans;
select_info=mat_struct.select_info;
select_info_p=mat_struct.select_info_p;
clear mat_struct;

Python數(shù)據(jù)分析scipy

SciPy是一組專門解決科學(xué)計(jì)算中各種標(biāo)準(zhǔn)問題域的包的集合,主要包括下面這些包:

  • scipy.integrate:數(shù)值積分例程和微分方程求解器
  • scipy.linalg:擴(kuò)展了由numpy.linalg提供的線性代數(shù)例程和矩陣分解功能
  • scipy.optimize:函數(shù)優(yōu)化器(最小化器)以及跟查找算法
  • scipy.signal:信號(hào)處理工具
  • scipy.sparse:稀疏矩陣和系數(shù)線性系統(tǒng)求解器
  • scipy.special:SPECFUN(這是一個(gè)實(shí)現(xiàn)了許多常用數(shù)學(xué)函數(shù)(如伽馬函數(shù))的Fortran庫)的包裝器
  • scipy.stats:標(biāo)準(zhǔn)連續(xù)和離散概率分布(如密度函數(shù)、采樣器、連續(xù)分布函數(shù)等)、各種統(tǒng)計(jì)檢驗(yàn)方法,以及更好的描述統(tǒng)計(jì)法
  • scipy.weave:利用內(nèi)聯(lián)C++代碼加速數(shù)組計(jì)算的工具

定積分scipy.integrate

導(dǎo)入積分模塊:

import numpy as np #導(dǎo)入numpy庫
from scipy import integrate #導(dǎo)入定積分模塊

scipy.integrate.quad(func,a,b):計(jì)算單重積分,參數(shù)分別為被積函數(shù)(f(x))、積分下限、積分上限

res, err = integrate.quad(np.sin, 0, np.pi/2) # 對(duì)sin函數(shù)在[0,$\pi/2$]區(qū)間上積分,quad函數(shù)返回兩個(gè)值,第一個(gè)為積分結(jié)果,第二個(gè)為誤差值
print(integrate.quad(lambda x:x**2,0,1)) # 計(jì)算x**2的定積分,積分區(qū)間為0到1,并輸出結(jié)果

scipy.integrate.dblquad(func,a,b,gfun,hfun):計(jì)算雙重積分,參數(shù)分別為被積函數(shù)(f(y,x))、x的積分下限、x的積分上限、y的積分下限、y的積分上限

print(integrate.dblquad(lambda x,y:x**2+y,0,2,lambda x:0,lambda x:1)) #對(duì)x**2+y求定積分,x積分區(qū)間[0,1],y積分區(qū)間[0,2],并輸出結(jié)果

scipy.integrate.nquad(func,ranges):計(jì)算多重積分,參數(shù)分別為被積函數(shù)(f(x0,x1,…,xn))、積分區(qū)間(格式為[[a,b],[c,d],[e,f]],依次為x0、x1、x2、的積分區(qū)間)

def f(x,y):
? ? return x**2+y
?
def bound_x():
? ? return [0,1]
?
def bound_y():
? ? return [0,2]
?
print(integrate.nquad(f,[bound_x,bound_y]))

優(yōu)化器scipy.optimize

導(dǎo)入模塊

import numpy as np #導(dǎo)入numpy庫
from scipy import optimize #導(dǎo)入優(yōu)化模塊

scipy.optimize.minimize(func,x0,args=(),method=None,jac=None):計(jì)算標(biāo)量函數(shù)的最小值,參數(shù)分別為標(biāo)量函數(shù)、初值、、尋優(yōu)方法

尋優(yōu)方法有:

插值scipy.interpolate

導(dǎo)入模塊

import numpy as np #導(dǎo)入numpy庫
from scipy import interpolate #導(dǎo)入interpolate模塊
import matplotlib.pyplot as plt #導(dǎo)入繪圖模塊
x = np.linspace(0, 1, 10) #創(chuàng)建數(shù)組,相當(dāng)于x
y = np.sin(2 * np.pi * x) #相當(dāng)于y

??

scipy.interpolate.interp1d():1維插值函數(shù)

linear_f = interpolate.interp1d(x, y) #線性插值函數(shù)
x_new = np.linspace(0, 1, 50) #插值后的x
y_new = linear_f(x_new) #線性插值后的y
cubic_f = interpolate.interp1d(x, y, kind='cubic') #應(yīng)用插值函數(shù)
cubic_y = cubic_f(x_new) #插值后的y值?

展示效果

plt.figure()
plt.plot(x, y, 'o', ms=6, label='x')
plt.plot(x_new, y_new, label='linear interp')
plt.plot(x_new, cubic_y, label='cubic interp')
plt.legend()
plt.show()

線性計(jì)算與矩陣分解scipy.linalg

導(dǎo)入模塊

import numpy as np #導(dǎo)入numpy庫
from scipy import linalg as lg #導(dǎo)入scipy庫的linalg模塊
arr=np.array([[1,2],[3,4]]) #創(chuàng)建方陣arr
b=np.array([6,14]) #創(chuàng)建矩陣b

scipy.linalg.det():計(jì)算方陣的行列式

print('Det:',lg.det(arr)) #求矩陣arr的行列式

scipy.linalg.inv():計(jì)算方陣的逆矩陣

print('Inv:',lg.inv(arr)) #求矩陣arr的逆矩陣

scipy.linalg.eig():計(jì)算方陣的特征向量

print('Eig:',lg.eig(arr)) #求矩陣arr的特征向量

scipy.linalg.svd():對(duì)矩陣進(jìn)行奇異值分解

print('SVD:',lg.svd(arr)) #對(duì)矩陣arr進(jìn)行svd分解

scipy.linalg.lu():對(duì)矩陣進(jìn)行LU分解

print('LU:',lg.lu(arr)) #對(duì)矩陣arr進(jìn)行l(wèi)u分解

scipy.linalg.qr():對(duì)矩陣進(jìn)行QR分解

print('QR:',lg.qr(arr)) #對(duì)矩陣arr進(jìn)行qr分解

scipy.linalg.schur():對(duì)矩陣進(jìn)行Schur分解

print('Schur:',lg.schur(arr)) #對(duì)矩陣arr進(jìn)行Schur分解

scipy.linalg.solve():方程組求解

print('Sol:',lg.solve(arr,b)) #求方程組arr*x=b的解

總結(jié)

原文鏈接:https://blog.csdn.net/zeye5731/article/details/122655546

欄目分類
最近更新