Python的最小化leastsq的列 [英] Python minimization leastsq with columns

查看:279
本文介绍了Python的最小化leastsq的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我,但我最近开始使用Python ...和code。
我有一些麻烦,在蟒蛇优化...
我想确定系数K0,K1 ......与向量T的数据residual_x定义二维数组测试的每一列多项式的(例如如这里3列),这可能吗?和存储结果二维数组。

正如你所看到的,我只有一个数据列表做。

非常感谢你的帮助!

 导入numpy的是NP
进口SciPy的
进口pylab
从scipy.optimize进口leastsqT = np.arange(5,56,5)
T = np.reshape(T,(11,1))测试= np.array([3051.11,2984.85,3059.17]
       [3510.78,3442.43,3520.7]
       [4045.91,3975.03,4058.15]
       [4646.37,4575.01,4662.29]
       [5322.75,5249.33,5342.1]
       [6102.73,6025.72,6127.86]
       [6985.96,6906.81,7018.22]
       [7979.81,7901.04,8021]
       [9107.18,9021.98,9156.44]
       [10364.26,10277.02,10423.1]
       [11776.65,11682.76,11843.18]])K0 = 100。
K1 = 100。
K 2 = 1。
K3 = 1。
K4 = 1。
K5 = 10。高清residual_x(增值分销商,T,最近搜索):
    K0 =瓦尔[0]
    K1 =瓦尔[1]
    K2 =瓦尔[2]
    K3 =瓦尔[3]
    K4 =瓦尔[4]
    K5 =瓦尔[5]
    MODELE = T ** 5 * K0 + T ** 4 * K1 + T ** 3 * K2 + T ** 2 * K3 + T * K4 + K5
    返回(最近搜索-MODELE)瓦尔= [K0,K1,K2,K3,K4,K5]
out_x = leastsq(residual_x,增值经销商,ARGS =(T,试),epsfcn = 0.001)


解决方案

我想你想以适应三种不同的第五次多项式有一组通用的域名点,{5,10,15,......, 55},三个不同的范围集(测试栏目)。

您原来的code的缩进错误:从 K0 = 100 和下被错误地缩进;这可能是一个剪切和粘贴错误。

更重要的是,你需要适合每​​个多项式分开,所以你应该叫scipy.optimize.l​​eastsq为每列。这是最简单的用做循环(见code)。您还需要收集每个多项式的系数;你可以在一个3×6阵列做到这一点;我已经在下面的示例中使用的列表。

编辑:我的重点是让你的code工作时,我回答了这一点,所以我完全忘了这件多项式系数并不需要非线性求解。你应该看看 numpy.polyfit ,这是周围多项式系数最小二乘线性解算器的便捷包装器。你可以找到更多在numpy的文档或< A HREF =htt​​p://en.wikipedia.org/wiki/Polynomial_regression相对=nofollow>在Wikipedia ,我已经添加了 numpy.polyfit 下面的例子。

 导入numpy的是NP
从scipy.optimize进口leastsqT = np.arange(5,56,5)测试= np.array([3051.11,2984.85,3059.17]
                 [3510.78,3442.43,3520.7]
                 [4045.91,3975.03,4058.15]
                 [4646.37,4575.01,4662.29]
                 [5322.75,5249.33,5342.1]
                 [6102.73,6025.72,6127.86]
                 [6985.96,6906.81,7018.22]
                 [7979.81,7901.04,8021]
                 [9107.18,9021.98,9156.44]
                 [10364.26,10277.02,10423.1]
                 [11776.65,11682.76,11843.18]])
K0 = 100。
K1 = 100。
K 2 = 1。
K3 = 1。
K4 = 1。
K5 = 10。高清residual_x(增值分销商,T,最近搜索):
    K0 =瓦尔[0]
    K1 =瓦尔[1]
    K2 =瓦尔[2]
    K3 =瓦尔[3]
    K4 =瓦尔[4]
    K5 =瓦尔[5]
    MODELE = T ** 5 * K0 + T ** 4 * K1 + T ** 3 * K2 + T ** 2 * K3 + T * K4 + K5
    返回最近搜索-MODELE瓦尔= [K0,K1,K2,K3,K4,K5]
coeffs = []因为我在范围内(test.shape [1]):
#EDIT:previously附加leastsq的完整输出coeffs列表
#这是错误的; leastsq返回两个输出(即,元组)
#我们只正处于系数非常感兴趣
    thiscoeffs,_ = leastsq(residual_x,增值经销商,ARGS =(T,测试[:,我]),epsfcn = 0.001)
    coeffs.append(thiscoeffs)因为我在范围内(test.shape [1]):
    打印'聚[%D]。coeffs:%s'的%(I,STR(coeffs [I]))#尝试numpy.polyfit第一个例子
coeffs2 = np.polyfit(T,测试[:,0],5)
打印'的情况下,0 np.polyfit:%s'的,STR(coeffs2)#EDIT:此加入第二个编辑
#如果你想coeffs为一个数组,你可以转换列表如下
coeffs_array = np.asarray(coeffs)
#或者你可以让coeffs首先一个numpy的阵列

这给了

 聚[0] coeffs:(阵列([-9.70769231e-07,1.56254079e-04,5.27449301e-03,
         1.03258957e + 00,7.62019210e + 01,2.64248394e + 03]),3)
聚[1] coeffs:(阵列([-1.37025641e-06,2.10074592e-04,2.49477855e-03,
         1.09748240e + 00,7.51708855e + 01,2.58007182e + 03]),2)
聚[2] coeffs:(阵列([-1.09435897e-06,1.58983683e-04,5.97712121e-03,
         1.01704522e + 00,7.67132035e + 01,2.64824061e + 03]),3)
np.polyfit的情况下0:%S [-9.70769231e-07 1.56254079e-04 5.27449301e-03 1.03258957e + 00
   7.62019210e + 01 + 2.64248394e 03]

Please excuse me but I started recently using Python...and code. I have some troubles with optimization in python... I would like to determine coefficients k0,k1...of a polynom defined in residual_x with data of vector "T" for each column of 2D array "test" (as example here with 3 columns), is it possible? and store results as 2D array.

As you can see, I do for only a list of data.

Thanks a lot for help!!!

import numpy as np
import scipy
import pylab 
from scipy.optimize import leastsq

T = np.arange(5,56,5)
T = np.reshape(T,(11,1))

test = np.array([[  3051.11,   2984.85,   3059.17],
       [  3510.78,   3442.43,   3520.7 ],
       [  4045.91,   3975.03,   4058.15],
       [  4646.37,   4575.01,   4662.29],
       [  5322.75,   5249.33,   5342.1 ],
       [  6102.73,   6025.72,   6127.86],
       [  6985.96,   6906.81,   7018.22],
       [  7979.81,   7901.04,   8021.  ],
       [  9107.18,   9021.98,   9156.44],
       [ 10364.26,  10277.02,  10423.1 ],
       [ 11776.65,  11682.76,  11843.18]])

k0 = 100.
k1 = 100.
k2 = 1.
k3 = 1.
k4 = 1.
k5 = 10.

def residual_x(vars, T, donnees):
    k0 = vars[0]
    k1 = vars[1]
    k2 = vars[2]
    k3 = vars[3]
    k4 = vars[4]
    k5 = vars[5]
    modele = T**5*k0+T**4*k1+T**3*k2+T**2*k3+T*k4+k5
    return (donnees-modele)

vars = [k0, k1, k2, k3, k4, k5]
out_x = leastsq(residual_x, vars, args=(T, test),epsfcn=0.001)

解决方案

I think you're trying to fit three different 5th-degree polynomials with a common set of domain points, {5,10,15,...,55}, to three different range sets (the columns of test).

Your original code had indentation errors: from k0 = 100. and down was incorrectly indented; that was probably a cut-and-paste error.

More importantly, you need to fit each polynomial separately, so you should call scipy.optimize.leastsq for each column. This is easiest to do with a for loop (see code). You also need to collect the coefficients for each polynomial; you could do this in a 3x6 array; I've used a list in the example below.

EDIT: I was focused on getting your code working when I answered this, so I completely forgot that fitting polynomial coefficients doesn't need a non-linear solver. You should take a look at numpy.polyfit, which is a convenience wrapper around a least-squares linear solver for polynomial coefficients. You can find out more in the numpy docs or at Wikipedia, and I've added sample usage of numpy.polyfit to the example below.

import numpy as np
from scipy.optimize import leastsq

T = np.arange(5,56,5)

test = np.array([[  3051.11,   2984.85,   3059.17],
                 [  3510.78,   3442.43,   3520.7 ],
                 [  4045.91,   3975.03,   4058.15],
                 [  4646.37,   4575.01,   4662.29],
                 [  5322.75,   5249.33,   5342.1 ],
                 [  6102.73,   6025.72,   6127.86],
                 [  6985.96,   6906.81,   7018.22],
                 [  7979.81,   7901.04,   8021.  ],
                 [  9107.18,   9021.98,   9156.44],
                 [ 10364.26,  10277.02,  10423.1 ],
                 [ 11776.65,  11682.76,  11843.18]])


k0 = 100.
k1 = 100.
k2 = 1.
k3 = 1.
k4 = 1.
k5 = 10.

def residual_x(vars, T, donnees):
    k0 = vars[0]
    k1 = vars[1]
    k2 = vars[2]
    k3 = vars[3]
    k4 = vars[4]
    k5 = vars[5]
    modele = T**5*k0+T**4*k1+T**3*k2+T**2*k3+T*k4+k5
    return donnees-modele

vars = [k0, k1, k2, k3, k4, k5]
coeffs=[]

for i in range(test.shape[1]):
#EDIT: previously appended the complete output of leastsq to coeffs list
#      this is wrong; leastsq returns two outputs (i.e., a tuple)
#      and we're only really interested in the coefficients
    thiscoeffs,_=leastsq(residual_x, vars, args=(T, test[:,i]),epsfcn=0.001)
    coeffs.append(thiscoeffs)

for i in range(test.shape[1]):
    print 'poly[%d] coeffs: %s' % (i,str(coeffs[i]))

# try first example with numpy.polyfit
coeffs2 = np.polyfit(T,test[:,0],5)
print 'np.polyfit for case 0: %s',str(coeffs2)

#EDIT: this added in second edit
# if you want coeffs as an array, you could convert the list like this
coeffs_array=np.asarray(coeffs)
# or you could make coeffs a numpy array in the first place

This gives [EDIT: coeffs2 solution added]

poly[0] coeffs: (array([ -9.70769231e-07,   1.56254079e-04,   5.27449301e-03,
         1.03258957e+00,   7.62019210e+01,   2.64248394e+03]), 3)
poly[1] coeffs: (array([ -1.37025641e-06,   2.10074592e-04,   2.49477855e-03,
         1.09748240e+00,   7.51708855e+01,   2.58007182e+03]), 2)
poly[2] coeffs: (array([ -1.09435897e-06,   1.58983683e-04,   5.97712121e-03,
         1.01704522e+00,   7.67132035e+01,   2.64824061e+03]), 3)
np.polyfit for case 0: %s [ -9.70769231e-07   1.56254079e-04   5.27449301e-03   1.03258957e+00
   7.62019210e+01   2.64248394e+03]

这篇关于Python的最小化leastsq的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆