“Scipy”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
无编辑摘要
无编辑摘要
第19行: 第19行:
*Non-linear least squares: Levenberg–Marquardt algorithm implemented in scipy.optimize.leastsq().
*Non-linear least squares: Levenberg–Marquardt algorithm implemented in scipy.optimize.leastsq().
*If the function is linear, this is a linear-algebra problem, and should be solved with scipy.linalg.lstsq().
*If the function is linear, this is a linear-algebra problem, and should be solved with scipy.linalg.lstsq().

==文件输出==
# Some test data
x = np.arange(200).reshape((4,5,10))
# Specify the filename of the .mat file
matfile = 'test_mat.mat'
# Write the array to the mat file. For this to work, the array must be the value
# corresponding to a key name of your choice in a dictionary
scipy.io.savemat(matfile, mdict={'out': x}, oned_as='row')

# For the above line, I specified the kwarg oned_as since python (2.7 with
# numpy 1.6.1) throws a FutureWarning. Here, this isn't really necessary
# since oned_as is a kwarg for dealing with 1-D arrays.
# Now load in the data from the .mat that was just saved
matdata = scipy.io.loadmat(matfile)
# And just to check if the data is the same:
assert np.all(x == matdata['out'])

2018年3月2日 (五) 08:23的版本

http://www.scipy-lectures.org

积分

>>> from scipy.integrate import quad
>>> def integrand(x, a, b):
...     return a*x**2 + b
>>> a = 2
>>> b = 1
>>> I = quad(integrand, 0, 1, args=(a,b))



求函数最小值

  • Methods based on conjugate gradient are named with ‘cg’ in scipy. The simple conjugate gradient method to minimize a function is scipy.optimize.fmin_cg():
  • n scipy, scipy.optimize.fmin() implements the Nelder-Mead approach: (不太依赖于倒数)
  • Brute force: a grid search
scipy.optimize.brute() evaluates the function on a given grid of parameters and returns the parameters corresponding to the minimum value.The parameters are specified with ranges given to numpy.mgrid. By default, 20 steps are taken in each direction:
  • Non-linear least squares: Levenberg–Marquardt algorithm implemented in scipy.optimize.leastsq().
  • If the function is linear, this is a linear-algebra problem, and should be solved with scipy.linalg.lstsq().

文件输出

# Some test data
x = np.arange(200).reshape((4,5,10))
# Specify the filename of the .mat file
matfile = 'test_mat.mat'
# Write the array to the mat file. For this to work, the array must be the value
# corresponding to a key name of your choice in a dictionary
scipy.io.savemat(matfile, mdict={'out': x}, oned_as='row')
# For the above line, I specified the kwarg oned_as since python (2.7 with 
# numpy 1.6.1) throws a FutureWarning.  Here, this isn't really necessary 
# since oned_as is a kwarg for dealing with 1-D arrays.

# Now load in the data from the .mat that was just saved
matdata = scipy.io.loadmat(matfile)
# And just to check if the data is the same:
assert np.all(x == matdata['out'])