① python 離散型數據怎麼量化
python 離散型數據量化的方法可以採用變數轉換方法來解決,分類數據和連續數據需要參與模型計算,並且通常會轉換為數值數據。
當然,某些演算法允許這些數據直接參與計算,例如分類演算法中的決策樹和關聯規則。將非數字數據轉換為數字數據的最佳方法是將所有類別或有序變數的范圍從一列多值形式轉換為僅包含真值的多列。可以將True值傳遞給True,False或0、1。這種符號轉換方法有時稱為真值轉換。
具體代碼是:
import pandas as pddata = [.
['yellow', 'S', 10.1, 'class1'].
['red', 'M', 13.5, 'class1'].
['red', 'M', 15.1, 'class2'].
['blue', 'XL', 15.3, 'class2'.
df = pd.DataFrame(.
data,columns=['color', 'size', 'prize', 'class'].
python 離散型數據用連續數據處理的方法是:
1、等寬法:若數據區間為0~20,設置箱子個數為4個,則等寬法會將數據裝入4個箱子:[0,5],(5,10],(10,15],(15,20],並且可以設置每個箱子的名字,如1、2、3、4。
等寬法缺點是分箱結果會受到最值影響。並且需要人為指定箱子個數,比較依賴於經驗。分箱結果會直接影響後續分類、聚類的結果。
2、等頻法:等頻法是指將一組數據分解成n個部分後,每個部分的記錄數量是一樣多的。等頻法常用pandas庫中的qcut()函數進行處理。
② matlab離散數據的常微分方程求解
首先,用dslve()函數求解人口模型的常微分方程
dsolve('DN=r*N','N(t0)=N0')
ans =
(N0*exp(r*t))/exp(r*t0)
第二,根據N和t的離散數據,用lsqcurvefit()擬合函數,擬合出N0 和 r系數值
N=[]
t=[]
fun=@(a,x)(a(1)*exp(a(2)*t))/exp(a(2)) %t0=1
a=lsqcurvefit(fun,t0,t,N)
N0 =a(1)
r=a(2)
③ 如何使用python計算常微分方程
常用形式
odeint(func, y0, t,args,Dfun)
一般這種形式就夠用了。
下面是官方的例子,求解的是
D(D(y1))-t*y1=0
為了方便,採取D=d/dt。如果我們令初值
y1(0) = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
D(y1)(0) = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
這個微分方程的解y1=airy(t)。
令D(y1)=y0,就有這個常微分方程組。
D(y0)=t*y1
D(y1)=y0
Python求解該微分方程。
>>> from scipy.integrate import odeint
>>> from scipy.special import gamma, airy
>>> y1_0 = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
>>> y0_0 = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
>>> y0 = [y0_0, y1_0]
>>> def func(y, t):
... return [t*y[1],y[0]]
>>> def gradient(y,t):
... return [[0,t],[1,0]]
>>> x = arange(0,4.0, 0.01)
>>> t = x
>>> ychk = airy(x)[0]
>>> y = odeint(func, y0, t)
>>> y2 = odeint(func, y0, t, Dfun=gradient)
>>> print ychk[:36:6]
[ 0.355028 0.339511 0.324068 0.308763 0.293658 0.278806]
>>> print y[:36:6,1]
[ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806]
>>> print y2[:36:6,1]
[ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806]
得到的解與精確值相比,誤差相當小。
=======================================================================================================
args是額外的參數。
用法請參看下面的例子。這是一個洛侖茲曲線的求解,並且用matplotlib繪出空間曲線圖。(來自《python科學計算》)
from scipy.integrate import odeint
import numpy as np
def lorenz(w, t, p, r, b):
# 給出位置矢量w,和三個參數p, r, b 計算出
# dx/dt, dy/dt, dz/dt 的值
x, y, z = w
# 直接與lorenz 的計算公式對應
return np.array([p*(y-x), x*(r-z)-y, x*y-b*z])
t = np.arange(0, 30, 0.01) # 創建時間點
# 調用ode 對lorenz 進行求解, 用兩個不同的初始值
track1 = odeint(lorenz, (0.0, 1.00, 0.0), t, args=(10.0, 28.0, 3.0))
track2 = odeint(lorenz, (0.0, 1.01, 0.0), t, args=(10.0, 28.0, 3.0))
# 繪圖
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(track1[:,0], track1[:,1], track1[:,2])
ax.plot(track2[:,0], track2[:,1], track2[:,2])
plt.show()
===========================================================================
scipy.integrate.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0)
計算常微分方程(組)
使用 FORTRAN庫odepack中的lsoda解常微分方程。這個函數一般求解初值問題。
參數:
func : callable(y, t0, ...) 計算y在t0 處的導數。
y0 : 數組 y的初值條件(可以是矢量)
t : 數組 為求出y,這是一個時間點的序列。初值點應該是這個序列的第一個元素。
args : 元組 func的額外參數
Dfun : callable(y, t0, ...) 函數的梯度(Jacobian)。即雅可比多項式。
col_deriv : boolean. True,Dfun定義列向導數(更快),否則Dfun會定義橫排導數
full_output : boolean 可選輸出,如果為True 則返回一個字典,作為第二輸出。
printmessg : boolean 是否列印convergence 消息。
返回: y : array, shape (len(y0), len(t))
數組,包含y值,每一個對應於時間序列中的t。初值y0 在第一排。
infodict : 字典,只有full_output == True 時,才會返回。
字典包含額為的輸出信息。
鍵值:
『hu』 vector of step sizes successfully used for each time step.
『tcur』 vector with the value of t reached for each time step. (will always be at least as large as the input times).
『tolsf』 vector of tolerance scale factors, greater than 1.0, computed when a request for too much accuracy was detected.
『tsw』 value of t at the time of the last method switch (given for each time step)
『nst』 cumulative number of time steps
『nfe』 cumulative number of function evaluations for each time step
『nje』 cumulative number of jacobian evaluations for each time step
『nqu』 a vector of method orders for each successful step.
『imxer』index of the component of largest magnitude in the weighted local error vector (e / ewt) on an error return, -1 otherwise.
『lenrw』 the length of the double work array required.
『leniw』 the length of integer work array required.
『mused』a vector of method indicators for each successful time step: 1: adams (nonstiff), 2: bdf (stiff)
其他參數,官方網站和文檔都沒有明確說明。相關的資料,暫時也找不到。
④ python里怎麼樣求解微分方程
有很多大學生問我,學習python有什麼用呢?我說:你至少可以用來解微分方程,如下面的例子,就是解決微分方程:
y"+a*y'+b*y=0
代碼如下:
[python]view plain
#y"+a*y'+b*y=0
fromscipy.integrateimportodeint
frompylabimport*
defderiv(y,t):#返回值是y和y的導數組成的數組
a=-2.0
b=-0.1
returnarray([y[1],a*y[0]+b*y[1]])
time=linspace(0.0,50.0,1000)
yinit=array([0.0005,0.2])#初值
y=odeint(deriv,yinit,time)
figure()
plot(time,y[:,0],label='y')#y[:,0]即返回值的第一列,是y的值。label是為了顯示legend用的。
plot(time,y[:,1],label="y'")#y[:,1]即返回值的第二列,是y』的值
xlabel('t')
ylabel('y')
legend()
show()
輸出結果如下:
⑤ python求微分方程組的數值解曲線01
如圖所示:
⑥ python 的scipy 里的 odeint 這個求微分方程的函數怎麼用啊
scipy中提供了用於解常微分方程的函數odeint(),完整的調用形式如下:
scipy.integrate.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0,hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0)
實際使用中,還是主要使用前三個參數,即微分方程的描寫函數、初值和需要求解函數值對應的的時間點。接收數組形式。這個函數,要求微分方程必須化為標准形式,即dy/dt=f(y,t,)。
from scipy import odeint
y = odeint(dy/dt=r*y*(1-y/k) ,y(0)=0.1,t)
對於微分方程全還給老師了,
http://hyry.dip.jp:8000/pydoc/index.html
這個地址有很多關於python做科學計算的文檔,你可以去查查
⑦ 用python求離散變數的期望值!
離散型很簡單,一個一個列出來就可以了
連續型的話,也不難,看具體的吧
比如兩個連續型X、Y
現在條件可以任意給,比如知道X、Y的密度函數,那直接用xy乘以聯合密度函數的定積分就可以了。
⑧ python的scipy里的odeint這個求微分方程的函數怎麼用啊
scipy.integrate.odeint(func,y0,t,args=(),Dfun=None,col_deriv=0,full_output=0,ml=None,mu=None,rtol=None,atol=None,tcrit=None,h0=0.0,hmax=0.0,hmin=0.0,ixpr=0,mxstep=0,mxhnil=0,mxordn=12,mxords=5,printmessg=0) 實際使用中,還是主要使用前三個參數,即微分方程的描寫函數、初值和需要求解函數值對應的的時間點。接收數組形式。這個函數,要求微分方程必須化為標准形式,即dy/dt=f(y,t,)。 fromscipyimportodeint y=odeint(dy/dt=r*y*(1-y/k),y(0)=0.1,t) 對於微分方程全還給老師了,