AttributeError:“ int”对象没有属性“ squeeze”

时间:2019-04-13 16:20:07

标签: python python-3.x

尝试调用(按值)名为make_dashboard的函数时出现以下错误

函数调用:

make_dashboard(x=1948, gdp_change=10, unemployment=3.75, title=title, file_name=file_name)
*Error:* AttributeError                            Traceback (most recent call last)
<ipython-input-42-8f2b4601acb0> in <module>()
      1 # Fill up the parameters in the following function:
----> 2 make_dashboard(1948, 10, 3.75, title, file_name)
      3 #make_dashboard(1948, 10, unemployment=3.75, title=title, file_name=file_name)

<ipython-input-5-0d2e581d2fa5> in make_dashboard(x, gdp_change, unemployment, title, file_name)
      2     output_file(file_name)
      3     p = figure(title=title, x_axis_label='year', y_axis_label='%')
----> 4     p.line(x.squeeze(), gdp_change.squeeze(), color="firebrick", line_width=4, legend="% GDP change")
      5     p.line(x.squeeze(), unemployment.squeeze(), line_width=4, legend="% unemployed")
      6     show(p)

AttributeError: 'int' object has no attribute 'squeeze'

*Function:* def make_dashboard(x, gdp_change, unemployment, title, file_name):
    output_file(file_name)
    p = figure(title=title, x_axis_label='year', y_axis_label='%')
    p.line(x.squeeze(), gdp_change.squeeze(), color="firebrick", line_width=4, legend="% GDP change")
    p.line(x.squeeze(), unemployment.squeeze(), line_width=4, legend="% unemployed")
    show(p)

函数中使用的属性“ x”,“ gdp_change”和“失业”是数据框。请有人帮助我解决这个错误。

在调用函数时尝试输入属性值 make_dashboard(x=1948, gdp_change=10, unemployment=3.75, title=title, file_name=file_name)

def make_dashboard(x, gdp_change, unemployment, title, file_name):
    output_file(file_name)
    p = figure(title=title, x_axis_label='year', y_axis_label='%')
    p.line(x.squeeze(), gdp_change.squeeze(), color="firebrick", line_width=4, legend="% GDP change")
    p.line(x.squeeze(), unemployment.squeeze(), line_width=4, legend="% unemployed")
    show(p)


make_dashboard(x=1948, gdp_change=10, unemployment=3.75, title=title, file_name=file_name)

期望基于属性显示的仪表板

Actual: Error : 
 AttributeError                            Traceback (most recent call last)
<ipython-input-42-8f2b4601acb0> in <module>()
      1 # Fill up the parameters in the following function:
----> 2 make_dashboard(1948, 10, 3.75, title, file_name)
      3 #make_dashboard(1948, 10, unemployment=3.75, title=title, file_name=file_name)

<ipython-input-5-0d2e581d2fa5> in make_dashboard(x, gdp_change, unemployment, title, file_name)
      2     output_file(file_name)
      3     p = figure(title=title, x_axis_label='year', y_axis_label='%')
----> 4     p.line(x.squeeze(), gdp_change.squeeze(), color="firebrick", line_width=4, legend="% GDP change")
      5     p.line(x.squeeze(), unemployment.squeeze(), line_width=4, legend="% unemployed")
      6     show(p)

AttributeError: 'int' object has no attribute 'squeeze'

2 个答案:

答案 0 :(得分:0)

操作以下代码:

def make_dashboard(x, gdp_change, unemployment, title, file_name):
    output_file(file_name)
    p = figure(title=title, x_axis_label='year', y_axis_label='%')
    p.line(x.squeeze(), gdp_change.squeeze(), color="firebrick", line_width=4, legend="% GDP change")
    p.line(x.squeeze(), unemployment.squeeze(), line_width=4, legend="% unemployed")
    show(p)

答案 1 :(得分:0)

您还需要在第一个代码块中导入 numpy。这是因为当我们要从数组中删除一维条目时使用了挤压()函数。

import numpy as np

现在,您必须在调用“make_dashboard”函数的地方稍作更改。不要将属性作为 int、float 或 list 传递。您必须将其作为数组传递。

make_dashboard(x=np.array(x),gap_change=np.array(gap_change),unemployment=np.array(unemployment), title=title, file_name=file_name)

这应该会运行程序。