Math 如何在Sage中进行回归分析?

Math 如何在Sage中进行回归分析?,math,sage,Math,Sage,我尝试了这个,但没有成功: find_fit(data, quadratic_residues) 我试图找到最适合水流量数据的方法: ---在评论之后编辑--- 新守则: var('x') model(x) = x**2 find_fit((xlist, reqlist), model) 错误消息: Traceback (click to the left for traceback) ... TypeError: data has to be a list of lists, a matr

我尝试了这个,但没有成功:

find_fit(data, quadratic_residues)
我试图找到最适合水流量数据的方法:

---在评论之后编辑---

新守则:

var('x')
model(x) = x**2
find_fit((xlist, reqlist), model)
错误消息:

Traceback (click to the left for traceback)
...
TypeError: data has to be a list of lists, a matrix, or a numpy array
---编辑

错误消息现在为:

Traceback (click to the left for traceback)
...
ValueError: each row of data needs 2 entries, only 5 entries given
此处与图片相同:

我认为你的问题在于二次剩余可能并不意味着你认为它意味着什么。如果你试图拟合最好的二次模型,我想你应该这样做

var('a, b, c, x')
model(x) = a*x*x + b*x + c
find_fit(data, model)

尝试史蒂文的例子时,我也遇到了错误:

ValueError: each row of data needs 5 entries, only 2 entries given
这里有一个更明确的例子,我已经测试过它在sage 4.7中工作

sage: l=[4*i^2+7*i+134+random() for i in xrange(100)]
sage: var('a,b,c,x')
(a, b, c, x)
sage: model=a*x^2+b*x+c
sage: find_fit(zip(xrange(100),l),model,variables=[x])
[a == 4.0000723084513217, b == 6.9904742307159697, c == 134.74698715254667]

显然,您需要变量=[x]来告诉sage a、b、c和x中的哪一个对应于模型中的变量。

谢谢您的更正!我简化了你的代码。但是,我还没有让它工作:var('x')model(x)=x**2 find_fit((xlist,reqlist),model)尝试将自由系数“a,b,c”放回
mydata = [[1,3],[2,7],[3,13],[4,24]]
var('a,b,c')
mymodel(x) = a*x^2 + b*x + c 
myfit = find_fit(mydata,mymodel,solution_dict=True)
points(mydata,color='purple') + plot(
  mymodel(
    a=myfit[a],
    b=myfit[b],
    c=myfit[c]
    ), 
    (x,0,4,),
    color='red'
  )