【问题标题】:I want to make a list from the outcome of a function using lists as arguments我想使用列表作为参数从函数的结果中创建一个列表
【发布时间】:2021-09-11 15:24:28
【问题描述】:

我试图通过使用其他列表作为函数的参数来创建一个列表。但是,我似乎无法获得正确的语法。
这是我的代码:

f1 = theta0 + theta1*(X1_train) + theta2*(X2_train)+theta3*(X3_train)

预期的结果将是 X1_train、X2_train 和 X3_train 长度相同的列表(这 3 个相同)。

我希望获得列表 X1_train、X2_train 和 X3_train 中每个元素的结果列表作为函数的参数。例如,如果我的列表是

X1_train = [0, 1]
X2_train = [1, 2]
X3_train = [0, 2]

我希望有一个数字列表

f1 = [theta0 + theta2, theta0 + theta1 + theta2 + 2*theta3]

thethas 是随机数。

此列表是我转换为列表的数据框的列,因此我可以执行此功能。

【问题讨论】:

  • 您能告诉我们您的期望吗?
  • 是的!我希望获得列表 X1_train、X2_train 和 X3_train 中每个元素的结果列表作为函数的参数。例如,如果我的列表 X1_train=[0,1] X2_train = [1,2] X3_train =[0,2] 我希望得到一个像 f1=[theta0+theta2, theta0+theta1+theta2+ 这样的数字列表2*theta3]
  • 您能提供一个此类函数的虚拟示例吗?
  • f(x1,x2,x3)=1+2*(x1)+3*(x2)+4*(x3) 其中 x1、x2 和 x3 是列表。
  • 如果我理解你在做什么,这从你的问题中并不完全清楚,你可以通过 not 将数据框列转换为列表来实现你想要的。 Pandas 系列可以按照您想要的方式工作;普通的python列表没有。

标签: python list dataframe regression multiplication


【解决方案1】:

我希望这会有所帮助:

import random

X1_train = [0,1]
X2_train = [1,2]
X3_train = [0,1]

amnt = 2

theta0 = random.sample(range(1, 10), amnt)
theta1 = random.sample(range(1, 10), amnt)
theta2 = random.sample(range(1, 10), amnt)
theta3 = random.sample(range(1, 10), amnt)

EndValues = []
for i in range(0, len(X1_train)):

f1 = theta0[i] + theta1[i] * X1_train[i] + theta2[i] * X2_train[i] + theta3[i] * X3_train[i]
EndValues.append(f1)

print(EndValues)

返回

[3, 6] [5, 1] [2, 5] [7, 8]
[5, 25]

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

使用zip 将三个列表压缩成一个三元组列表,解压缩三元组,然后将每个theta 值乘以其对应的元素,然后对结果求和。

f1 = [theta0 + theta1*x1 + theta2*x2 + theta3*x3
      for x1, x2, x3 in zip(X1_train, X2_train, X3_train)]

如果您认为theta0 是 1 的倍数,您可以将其概括为

from itertools import repeat


f1 = [theta0*x0 + theta1*x1 + theta2*x2 + theta3*x3
      for x0, x1, x2, x3 in zip(repeat(1), X1_train, X2_train, X3_train)]

你可以减少到

from operator import mul
thetas = [theta0, theta1, theta2, theta3]
trains = [repeat(1), X1_train, X2_train, X3_train]
f1 = [sum(map(mul, t, thetas)) for t in zip(*trains)]

sum(map(mul, t, thetas)) 只是tthetas 的点积。

def dotp(x, y):
    return sum(map(mul, x, y))

f1 = [dotp(t, thetas) for t in zip(*trains)]

【讨论】:

    【解决方案3】:

    我建议你试试这个简单的代码:

    def f(X: tuple, theta: tuple):
       if not isinstance(X, tuple):
          raise TypeError('X must be a tuple')
       if not isinstance(theta, tuple):
          raise TypeError('theta must be a tuple')
       if not X.__len__() == theta.__len__():
          raise ValueError('length of tuples is not equal')
       return sum([np.array(x_)*t_ for x_, t_ in zip(X, theta)])
    

    请注意,如果 X 或 Theta 不是相同长度的元组,则会引发错误。

    示例:(应该按原样工作

    import numpy as np
    X1_train = [0, 1]
    X2_train = [1, 2]
    X3_train = [0, 2]
    
    theta_1 = 1
    theta_2 = 1
    theta_3 = 3
    
    print(f(
        (X1_train, X2_train, X3_train),
        (theta_1, theta_2, theta_3)
        ))
    >>> [1 9]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-18
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      相关资源
      最近更新 更多