简体   繁体   English

如何编写向量函数来应用操作 f(x,y)?

[英]How to write a vector function to apply operation f(x,y)?

scalar_function can only handle scalar input, we could use the function np.vectorize() turn it into a vectorized function. scalar_function 只能处理标量输入,我们可以使用函数 np.vectorize() 将其转换为向量化函数。 Note that the input argument of np.vectorize() should be a scalar function, and the output of np.vectorize() is a new function that can handle vector input.注意 np.vectorize() 的输入参数应该是一个标量函数,而 np.vectorize() 的输出是一个可以处理向量输入的新函数。

Please write a vector function vector_function, which will apply the operation 𝑓(𝑥,𝑦) defined above element-wisely with input vectors with same dimension x and y.请编写一个向量函数vector_function,它将对具有相同维度x和y的输入向量逐元素地应用上面定义的操作𝑓(𝑥,𝑦)。

So for the scalar, I got :所以对于标量,我得到:

def scalar_function(x, y):
    
    if x <= y:
        return x*y
    else:
        return x/y

For the vector function I have :对于向量函数,我有:

def vector_function(x, y):

    vfunc = np.vectorize(scalar_function, otypes = [float])
    return vfunc

From here on I am stuck.从这里开始,我被困住了。

尝试这个

vector_function = np.vectorize(scalar_function)

based on this 'Please write a vector function vector_function, which will apply the operation 𝑓(𝑥,𝑦) defined above element-wisely with input vectors with same dimension x and y' here is what you are looking for:基于这个'Please write a vector function vector_function, which will apply the operation 𝑓(𝑥,𝑦) defined above element-wisely with input vectors with same dimension x and y'这里是你正在寻找的:

import numpy as np
def scalar_function(x, y):
    if x <= y:
        return x*y
    else:
        return x/y

vector_function = np.vectorize(scalar_function, otypes = [float])

print(vector_function(np.array([1, 2, 3, 6]), np.array([1, 3, 4, 5])))
[Output]>>> [ 1.   6.  12.   1.2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何编写一个 function `f`,它返回一个 integer 使得 `f(x) == f(y)` 当且仅当 `x` 和 `y` 相同? - How can we write a function `f` which returns an integer such that `f(x) == f(y)` if and only if `x` and `y` are the same? 如何在两个单独列表中的 X 和 Y 值范围内计算函数 f(X,Y) 的值 - How to compute values of function, f(X,Y), over a range of values of X and Y in two separate lists 如何在 y=x 的函数的一行中编写 plt.scatter(x, y) function - How to Write a plt.scatter(x, y) function in one line where y=function of x 如何求解函数y = f(x,y),即函数值取决于自身 - How to solve a function y=f(x,y), i.e, the functional value depends on itself 如何在python中绘制f(x,y)= sqrt(2x-y) - how to plot in python f(x,y)=sqrt(2x-y) 如何在x和y中找到f(x,y)的偏导数:python中的del ^ 2 f(x,y)/ [del(x)] [del(y)] - How to find Partial derivative of f(x,y) along x and y: del^2 f(x,y)/[del(x)][del (y)] in python 如何绘制grad(f(x,y))? - How to plot grad(f(x,y))? 如何绘制离散f(x,y)输出 - How to plot discrete f(x,y) output 在 sorted 方法中使用 if else f'y{x}' 评估 lambda function - Evaluating a lambda function with if else f'y{x}' inside the sorted method 在Python / numpy / pandas中具有函数f(x,y)结果的矩阵 - Matrix with results of function f(x,y) in Python/numpy/pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM