1

i want to make a linear equation with some dynamic inputs like it can be

y = θ0*x0 + θ1*x1

or

y = θ0*x0 + θ1*x1 + θ2*x2 + θ3*x3 + θ4*x4

for that i have dictionary for x0,x1,x2......xn and array for θ0,θ1,θ2......θn

im new to python so i tried this function but im stuck

so my question is how can i write a fucntion that gets x_values and theta_values as parameters and gives y_values as output

X = pd.DataFrame({'x0': np.ones(6), 'x1': np.linspace(0, 5, 6)})
θ = np.matrix('0 1')
def line_func(features, parameters):
    result = []
    for feat, param in zip(features.iteritems(), parameters):
        for i in feat:
            result.append(i*param)
    return result
line_func(X,θ)
5
  • Hi, welcome to SO. One question, why do you want to make one side a dictionary? If your variables are numbered anyways, you could also access the second structure by integer index and store the values as a list?! Commented Aug 14, 2019 at 17:18
  • I ask because then iterating over it would be much easier, you could just use for theta, x in zip(thetalist, xlist): ... Commented Aug 14, 2019 at 17:19
  • @jottbe thank you for answering.there is so many things in my code that i dont know how it actually works so thats why i can't fix it properly how can i use zip with X and theta together because they have different lenghts like theta only has 2 numbers but X can be 500+ Commented Aug 14, 2019 at 17:29
  • If you would want to work with pandas the whole way down (which I wouldn't recommend), I guess the right way would be to multiply the "column-series" of your dataframe with the corresponding theta value. I'l add it below. Commented Aug 14, 2019 at 17:43
  • Don't use np.matrix, just use np.array Commented Aug 14, 2019 at 18:30

1 Answer 1

1

If you want to multiply your thetas with a list of features, then you technically mulitply a matrix (the features) with a vector (theta).

You can do this as follows:

import numpy as np

x_array= x.values
theta= np.array([theta_0, theta_1])
x_array.dot(theta)

Just order your theta-vector the way your columns are ordered in x. But note, that this gives a row-wise sum of the products for theta_i*x_i for all is. If you don't want it to be summed up rowise, you just need to write x_array * theta.

If you want to work with pandas (which I wouldn't recommend) also for the mulitplication and want to get a dataframe with the products of the column value and the corresponding theta, you could do this as follows:

# define the theta-x mapping (theta-value per column name in x)
thetas={'x1': 1, 'x2': 3}

# create an empty result dataframe with the index of x
df_result= pd.DataFrame(index=x.index)

# assign the calculated columns in a loop
for col_name, col_series in x.iteritems():
    df_result[col_name]= col_series*thetas[col_name] 

df_result

This results in:

   x1  x2
0   1   6
1  -1   3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.