如何在Python用户定义函数中使用数组/向量?

时间:2022-12-05 12:55:40

I'm building a function to calculate the Reliability of a given component/subsystem. For this, I wrote the following in a script:

我正在构建一个函数来计算给定组件/子系统的可靠性。为此,我在一个脚本中编写了以下内容:

import math as m
import numpy as np

def Reliability (MTBF,time):
  failure_param = pow(MTBF,-1)
  R = m.exp(-failure_param*time)
  return R

The function works just fine for any time values I call in the function. Now I wanna call the function to calculate the Reliability for a given array, let's say np.linspace(0,24,25). But then I get errors like "Type error: only length-1 arrays can be converted to Python scalars".

这个函数对于我在函数中调用的任何时间值都能正常工作。现在我想调用这个函数来计算给定数组的可靠性,比如np.linspace(0,24,25)然后我得到了诸如“类型错误:只有长度为1的数组可以转换为Python标量”之类的错误。

Anyone that could help me being able to pass arrays/vectors on a Python function like that?

有人能帮我在Python函数中传递数组/向量吗?

Thank you very much in advance.

非常感谢。

4 个解决方案

#1


3  

The math.exp() function you are using knows nothing about numpy. It expects either a scalar, or an iterable with only one element, which it can treat as a scalar. Use the numpy.exp() instead, which accepts numpy arrays.

您正在使用的math.exp()函数对numpy一无所知。它期望一个标量,或者一个只有一个元素的迭代,它可以把这个元素当作标量。而是使用numpy.exp(),它接受numpy数组。

#2


2  

To be able to work with numpy arrays you need to use numpy functions:

为了能够使用numpy数组,您需要使用numpy函数:

import numpy as np

def Reliability (MTBF,time):
    return np.exp(-(MTBF ** -1) * time)

#3


0  

If possible you should always use numpy functions instead of math functions, when working with numpy objects.

如果可能的话,在处理numpy对象时,应该总是使用numpy函数而不是math函数。

They do not only work directly on numpy objects like arrays and matrices, but they are highly optimized, i.e using vectorization features of the CPU (like SSE). Most functions like exp/sin/cos/pow are available in the numpy module. Some more advanced functions can be found in scipy.

它们不仅直接作用于像数组和矩阵这样的numpy对象,而且它们是高度优化的i。e使用CPU的矢量化特性(如SSE)。在numpy模块中可以使用exp/sin/cos/pow等大多数函数。在scipy中可以找到一些更高级的函数。

#4


-1  

Rather than call Reliability on the vector, use list comprehension to call it on each element:

与其调用向量的可靠性,不如使用列表理解来调用每个元素:

[Reliability(MTBF, test_time) for test_time in np.linspace(0,24,25)]

Or:

或者:

map(Reliability, zip([MTBF]*25, linspace(0,24,25))

The second one produces a generator object which may be better for performance if the size of your list starts getting huge.

第二个生成一个生成器对象,如果列表的大小开始变得很大,那么这个对象对于性能可能会更好。

#1


3  

The math.exp() function you are using knows nothing about numpy. It expects either a scalar, or an iterable with only one element, which it can treat as a scalar. Use the numpy.exp() instead, which accepts numpy arrays.

您正在使用的math.exp()函数对numpy一无所知。它期望一个标量,或者一个只有一个元素的迭代,它可以把这个元素当作标量。而是使用numpy.exp(),它接受numpy数组。

#2


2  

To be able to work with numpy arrays you need to use numpy functions:

为了能够使用numpy数组,您需要使用numpy函数:

import numpy as np

def Reliability (MTBF,time):
    return np.exp(-(MTBF ** -1) * time)

#3


0  

If possible you should always use numpy functions instead of math functions, when working with numpy objects.

如果可能的话,在处理numpy对象时,应该总是使用numpy函数而不是math函数。

They do not only work directly on numpy objects like arrays and matrices, but they are highly optimized, i.e using vectorization features of the CPU (like SSE). Most functions like exp/sin/cos/pow are available in the numpy module. Some more advanced functions can be found in scipy.

它们不仅直接作用于像数组和矩阵这样的numpy对象,而且它们是高度优化的i。e使用CPU的矢量化特性(如SSE)。在numpy模块中可以使用exp/sin/cos/pow等大多数函数。在scipy中可以找到一些更高级的函数。

#4


-1  

Rather than call Reliability on the vector, use list comprehension to call it on each element:

与其调用向量的可靠性,不如使用列表理解来调用每个元素:

[Reliability(MTBF, test_time) for test_time in np.linspace(0,24,25)]

Or:

或者:

map(Reliability, zip([MTBF]*25, linspace(0,24,25))

The second one produces a generator object which may be better for performance if the size of your list starts getting huge.

第二个生成一个生成器对象,如果列表的大小开始变得很大,那么这个对象对于性能可能会更好。