TypeError:无法理解

时间:2019-10-02 17:24:47

标签: python numpy scipy least-squares

我正在拟合一条具有三个点的非常简单的曲线。使用minimumsq方法,遵循所有规则。但是我还是出错了。我不明白。谁能帮忙。非常感谢

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq

x = np.array([2.0,30.2,15.0])
y = np.array([45.0,56.2,30.0])

print(x)
print(y)

# model
def t(x,a,b,c):
    return a*x**2 + b*x + c

#residual fucntion
def residual_t(x,y,a,b,c):
    return y-t(x,a,b,c)


#initial parameters
g0 = np.array([0.0,0.0,0.0])

#leastsq method
coeffs, cov = leastsq(residual_t, g0, args=(x,y))
plt.plot(x,t(x,*coeffs),'r')
plt.plot(x,y,'b')
plt.show()

#finding out Rsquared and Radj squared value
absError = residual_t(y,x,*coeffs)
se = np.square(absError) # squared errors
Rsquared = 1.0 - (np.var(absError) / np.var(y))
n = len(x)
k = len(coeffs)
Radj_sq = (1-((1-Rsquared)/(n-1)))/(n-k-1)
print (f'Rsquared value: {Rsquared}   adjusted R saquared value: {Radj_sq}')

TypeError:residual_t()缺少2个必需的位置参数:“ b”和“ c”

为什么? coeffs已经是包含a,b,c的最佳it值的数组。 coeffs也显示未定义,而residual_t也显示问题。您能帮我理解吗?

1 个答案:

答案 0 :(得分:1)

复制并粘贴您的代码(包括 Child Comp: import Picker from 'react-giphy-component' import React, { Component, PropTypes } from 'react' class AddGif extends Component { log (gif) { console.log(gif.original.mp4) } returnedGifObject = gif ; render () { return ( <div> {/* <button></button> */} <Picker onSelected={this.log.bind(this)} /> </div> ) } } export default AddGif; parent element class PostBox extends Component { constructor(props){ super(props) this.state = { title: null, postBody: null, giphyUrl: null, displayGifPicker: false } } getGifState = (selectedUrl) => { this.setState({ giphyUrl: selectedUrl}) } render () { const {title, postBody} = this.state const displayGifPicker = this.state.displayGifPicker return ( <Grid item xl={8}> {/* <Card className={classes.card} style={mt4}> */} <Card style={mt4}> <CardContent > <PostInput onChange={this.handleInputChange} onSubmit={this.handleSubmit}/> {displayGifPicker ? (<AddGif selectedGif = {this.getGifState} />) : (<Button size="small" onClick={this.displayGifPicker} ><button>Add Gif</button></Button>)} <CardActions> {/* <Button size="small">Submit VH5</Button> */} </CardActions> </CardContent> </Card> </Grid> ) } } 更改),我得到

*coeffs

那是在1135:~/mypy$ python3 stack58206395.py [ 2. 30.2 15. ] [45. 56.2 30. ] Traceback (most recent call last): File "stack58206395.py", line 24, in <module> coeffs, cov = leastsq(residual_t, g0, args=(x,y)) File "/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py", line 383, in leastsq shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) File "/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c' 调用中使用residual_t的错误。

如果我添加

leastsq

residual_t(g0, x, y) 定义之后,我得到了相同的错误:

g0

因此,您需要定义1136:~/mypy$ python3 stack58206395.py [ 2. 30.2 15. ] [45. 56.2 30. ] Traceback (most recent call last): File "stack58206395.py", line 23, in <module> residual_t(g0, x, y) TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c' 来处理这样的呼叫。我不会猜测您真正想要的是什么,所以我将把修补程序留给您。

请记住,residual_t将与residual_t元组结合的x0一起调用。这是args函数的典型用法。如有必要,请查看文档。

编辑

将函数定义为:

scipy.optimize

运行没有错误。