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

學無先后,達者為師

網站首頁 編程語言 正文

python的數據與matlab互通問題:SciPy_python

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

python數據與matlab互通SciPy

有時候需要利用python進行科學計算,但需要Matlab進行交互式畫圖,因此需要掌握python與matlab數據互通的技巧:SciPy

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

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

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

(二)例子

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

io.savemat('arr.mat', {"vec": arr})
#如果存入多組數據
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數據分析scipy

SciPy是一組專門解決科學計算中各種標準問題域的包的集合,主要包括下面這些包:

  • scipy.integrate:數值積分例程和微分方程求解器
  • scipy.linalg:擴展了由numpy.linalg提供的線性代數例程和矩陣分解功能
  • scipy.optimize:函數優化器(最小化器)以及跟查找算法
  • scipy.signal:信號處理工具
  • scipy.sparse:稀疏矩陣和系數線性系統求解器
  • scipy.special:SPECFUN(這是一個實現了許多常用數學函數(如伽馬函數)的Fortran庫)的包裝器
  • scipy.stats:標準連續和離散概率分布(如密度函數、采樣器、連續分布函數等)、各種統計檢驗方法,以及更好的描述統計法
  • scipy.weave:利用內聯C++代碼加速數組計算的工具

定積分scipy.integrate

導入積分模塊:

import numpy as np #導入numpy庫
from scipy import integrate #導入定積分模塊

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

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

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

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

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

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]))

優化器scipy.optimize

導入模塊

import numpy as np #導入numpy庫
from scipy import optimize #導入優化模塊

scipy.optimize.minimize(func,x0,args=(),method=None,jac=None):計算標量函數的最小值,參數分別為標量函數、初值、、尋優方法

尋優方法有:

插值scipy.interpolate

導入模塊

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

??

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

linear_f = interpolate.interp1d(x, y) #線性插值函數
x_new = np.linspace(0, 1, 50) #插值后的x
y_new = linear_f(x_new) #線性插值后的y
cubic_f = interpolate.interp1d(x, y, kind='cubic') #應用插值函數
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()

線性計算與矩陣分解scipy.linalg

導入模塊

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

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

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

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

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

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

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

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

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

scipy.linalg.lu():對矩陣進行LU分解

print('LU:',lg.lu(arr)) #對矩陣arr進行lu分解

scipy.linalg.qr():對矩陣進行QR分解

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

scipy.linalg.schur():對矩陣進行Schur分解

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

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

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

總結

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

欄目分類
最近更新