【问题标题】:How to change the problem solving format to use lambda and/or map (Python)如何更改问题解决格式以使用 lambda 和/或 map (Python)
【发布时间】:2021-01-21 05:16:26
【问题描述】:

我正在尝试编写以下公式:

为此,我使用以下函数:

def predict(x, w):
    """
    Function to predict y(x, w) based on the values of x and w.
    
    Args: 
        x: a vector including all the values of the input variable x for a given set of point.
        w: polynomial coefficient vector.
        
    Returns: an array with the predicted values for y(x, w) function.
    """
    x = np.array(x)
    w = np.array(w)
    # list of powers for x. {0, 1, 2 ... M}
    powers = list(range(0, len(w)))
    
    # apply the polynomial fitting function to each value of vectors x & w
    sumatoria_list = sum(np.array([w[i] * (x ** i) for i in powers]))
    
    # return final sum
    return sumatoria_list

在下图中,您可以看到输入和输出的示例:

地点:

w0 = [-0.17594739490150393]
w1 = [0.7237871780107422, -1.7994691458244925]
x = array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
       0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ])

到目前为止,我的函数的输出是正确的,但是,我正在尝试使用 lambda:

def predict1(x, w):
    """
    Function to predict y(x, w) based on the values of x and w.
    
    Args: 
        x: a vector including all the values of the input variable x for a given set of point.
        w: polynomial coefficient vector.
        
    Returns: an array with the predicted values for y(x, w).
    """
    x = np.array(x)
    w = np.array(w)
    # list of powers for x. {0, 1, 2 ... M}
    powers = list(range(0, len(w)))
    
    # apply the polynomial fitting function to each value of vectors x & w
    sumatoria_list = list(map(lambda x, w, i: w * (x ** i), x, w, powers))
    
    # return final sum
    return sumatoria_list

尽管如此,它似乎无法正常工作。在下图中,您可以找到在函数中使用lambdamap 的输出示例。

我认为我不太了解如何将 lambda 应用于此特定问题,因此非常感谢您的帮助!

【问题讨论】:

  • 在您的正确代码中,您是否获得了sum(np.array([w[i] * (x ** i) for i in powers])) 的矢量输出?这似乎不对!

标签: python lambda map-function


【解决方案1】:

我认为它现在应该可以工作了,

请求的解决方案

代码语法

w = [0.7237871780107422, -1.7994691458244925]
x = [0., 0.11111111, 0.22222222, 0.33333333, 0.44444444, 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ]

powers = list(range(0, len(w)))
print("power list: ", powers)
print("w: ", w)
lista = list(map(lambda rng, x: w[rng] * (x ** rng) ,powers, x))

print(lista)

输出

power list:  [0, 1]
w:  [0.7237871780107422, -1.7994691458244925]

[0.7237871780107422, -0.19994101420331123]

[Program finished]

说明

没有给你所需输出的原因是你不能操作pow()或与其他人一起操作完整列表,你必须迭代两个列表才能给你所需的输出。

例如:

给出 listA = [1, 2, 3] 和 listB = [1, 2, 2]。如果你想操作这两个列表,你必须先迭代它们。

代码语法

listC = list(map(lambda x, y: x ** y, listA, listB))

输出

[1, 4, 9]

[Program finished]

或者,您只能将map() 用于数学运算

代码语法

listC2 = list(map(pow, listA, listB))

输出

[1, 4, 9]

[Program finished]

【讨论】:

  • 它不工作。我得到的输出是第三张图片中显示的内容,而我想要的输出是第二张图片中显示的内容。此外,x 和 w 都是数组。
  • 如果是这样,powers 长度必须与 x 而不是 w 根据您的第二张图片。现在,这是一个小故障
  • you w 变量在其列表中只有 2 个值,所以,问题是您如何将 10 个值作为变量 x 长度,而不是 w 长度!!!!跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 2022-01-11
  • 2018-06-27
相关资源
最近更新 更多