Python显示“太多值无法解包”

2024-04-16 23:02:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个线性回归模型来计算婴儿死亡率是否影响预期寿命。我正在使用谷歌合作来执行它。这是我的密码:

x = data.loc[(data['Country'] == 'Bangladesh'), ['infant deaths']]
y = data.loc[(data['Country'] == 'Bangladesh'), ['Life expectancy ']]

slope, intercept, r, p, std_err = stats.linregress(x, y)

def myfunc(x):
 return slope * x + intercept

mymodel = list(map(myfunc, x))

plt.scatter(x, y)
plt.plot(x, slope * x + intercept)
plt.ylim(ymin=0, ymax=2000)
plt.xlim(xmin=0, xmax=200)
plt.xlabel("infact deaths")
plt.ylabel ("Life expectancy")
plt.show()

但是,我收到如下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-32392bb3a878> in <module>()
      2 y = data.loc[(data['Country'] == 'Bangladesh'), ['Life expectancy ']]
      3 
----> 4 slope, intercept, r, p, std_err = stats.linregress(x, y)
      5 
      6 def myfunc(x):

/usr/local/lib/python3.6/dist-packages/scipy/stats/_stats_mstats_common.py in linregress(x, y)
    114 
    115     # average sum of squares:
--> 116     ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
    117     r_num = ssxym
    118     r_den = np.sqrt(ssxm * ssym)

ValueError: too many values to unpack (expected 4)

我相信我对这些栏目的命名是正确的。我还导入了所有库或模块。请帮我找到解决办法


Tags: datastatspltmyfunccountrylocslopestd