英文:
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笔记本中,这个问题可能会更严重,因为先前运行的单元可能会留下垃圾。
所以在这种情况下,追踪一下对np
或numpy
的任何赋值。像这样的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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论