① 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) 对于微分方程全还给老师了,