如何解决 AttributeError: ‘numpy.float64′ 对象没有 ’empty’ 属性?

huangapple go评论68阅读模式
英文:

How do I solve AttributeError: 'numpy.float64' object has no attribute 'empty'?

问题

I am trying to perform a sensitivity analysis for a system of 10 differential equations and 38 parameters, with the following code:

# Formulating the problem for sensitivity analysis
problem = {
    'num_vars': num_vars,
    'names': p_names,
    'bounds': bounds
}

# Generate the parameter samples using Saltelli's sampling method
num_samples = 256 # number of samples

param_values = saltelli.sample(problem, num_samples, calc_second_order=False)

# Run the model for each parameter sample
results = numpy.empty([param_values.shape[0], 10])

and I keep getting a this message error:

results = np.empty([param_values.shape[0], 10])
AttributeError: 'numpy.float64' object has no attribute 'empty'

I have followed some solutions in discussion forums like, trying to format param_values as a float, np.float64 or different kinds of array or lists. Also, I have tried to use different functions, for example, instead of using np.empty, used np.zeros or np.ones. I also tried downgrading numpy to a previous version, but it did not work. I am using python 3.11 and numpy 1.24.3.
I could not find a build-in plain function in python to substitute np.empty.

英文:

I am trying to perform a sensitivity analysis for a system of 10 differential equations and 38 parameters, with the following code:

# Formulating the problem for sensitivity analysis
problem = {
    'num_vars': num_vars,
    'names': p_names,
    'bounds': bounds
}

# Generate the parameter samples using Saltelli's sampling method
num_samples = 256 # number of samples

param_values = saltelli.sample(problem, num_samples, calc_second_order=False)


# Run the model for each parameter sample
results = numpy.empty([param_values.shape[0], 10])

and I keep getting a this message error:

results = np.empty([param_values.shape[0], 10])
AttributeError: 'numpy.float64' object has no attribute 'empty'

I have followed some solutions in discussion forums like, trying to format param_values as a float, np.float64 or different kinds of array or lists. Also, I have tried to use different functions, for example, instead of using np.empty, used np.zeros or np.ones. I also tried downgrading numpy to a previous version, but it did not work. I am using python 3.11 and numpy 1.24.3.
I could not find a build-in plain function in python to substitute np.empty.

答案1

得分: 1

请看错误消息的全部内容,而不是胡乱猜测。

如果你正在使用一些不属于numpy的函数,你会收到如下消息:

 AttributeError: module 'numpy' has no attribute 'foobar'

但你得到了:

 AttributeError: 'numpy.float64' object has no attribute 'empty'

这意味着在np.empty(...)中,np不是numpy的module,而是一个numpy浮点对象,即数组的一个元素。

AttributeError通常是左侧变量的误认为结果。如果你打字错误或猜错了右侧,问题可能也出在右侧,但更常见的是左侧被分配给了意外的东西。通常,左侧的问题类似于None。在Jupyter笔记本中,这个问题可能会更严重,因为先前运行的单元可能会留下垃圾。

所以在这种情况下,追踪一下对npnumpy的任何赋值。像这样的AttributeError错误经常出现,几乎可以归为一个接近的原因。

英文:

Instead of flopping around trying all sorts of wild guesses, read the error message - all of it.

If you were using some function that isn't part of numpy you'd get a message like

 AttributeError: module 'numpy' has no attribute 'foobar'

you got

 AttributeError: 'numpy.float64' object has no attribute 'empty'

That means that in np.empty(...), np is not the numpy module, but is instead a numpy float object, an element of an array.

AttributeError is often the result of mistaken identity of the variable on the left side. It could be a problem with the right side, if you made a typo or made a bad guess, but more often the left side has been assigned to something unexpected. Often that LHS is something like None. The problem may be worse in Jupyter notebooks, where previously run cells can leave garbage like this.

So in this case track down any assignments to np or numpy.

AttributeError like this comes up often enough that it almost qualifies as a close reason.

huangapple
  • 本文由 发表于 2023年6月1日 21:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定