Regina Briseño
Regina Briseño

Reputation: 55

I want to make a list from the outcome of a function using lists as arguments

I'm trying to make a list by using other lists as arguments of a function. However, i can't seem to get the right syntax.
This is my code:

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

The expected outcome would be a list of the same length of X1_train, X2_train and X3_train (which is the same for those 3).

I expect to get a list of the outcomes of each element on the lists X1_train, X2_train and X3_train as arguments of the funcion. For example, if my lists were

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

I'd expect a list of numbers like

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

The thethas are random numbers.

This lists are columns of a dataframe I converted into lists so I could do the function.

Upvotes: 0

Views: 196

Answers (3)

Alexandre Mahdhaoui
Alexandre Mahdhaoui

Reputation: 841

I suggest you try this simple code:

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)])

Note it would throw an error if X or Theta are not tuples of the same length.

Example: (should work as is

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]

Upvotes: 0

chepner
chepner

Reputation: 532093

Use zip to zip the three lists into a list of 3-tuples, unpack the 3-tuple, then multiply each theta value by its corresponding element before summing the results.

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

If you think of theta0 as being multiple by 1, you can generalize this to

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)]

which you can reduce to

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)) is just the dot product of t and thetas.

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

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

Upvotes: 0

AxelotlZ
AxelotlZ

Reputation: 97

I hope this helps:

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)

This returns

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

Upvotes: 0

Related Questions