-1

I have this least squares method, but I need it to obtain 10 dimensional data. This is from a practice text I'm learning from. I came up with this method for a two-dimensional data set. Now I need to have it work for a 10 dimensional one, but I'm totally stuck on it.

def least_squares(w):
    cost = 0
    for p in range(len(y)):
        # get pth input/output pair
        x_p = x[p]
        y_p = y[p]

        # form linear combination
        c_p = w[0] + w[1] * x_p

        # add least squares for this datapoint
        cost += (c_p - y_p) ** 2

    return cost

This is the result I should get after the edit

w = np.ones((11,1))
print (least_squares(w))
[ 7917.97952037]
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
Aloma85
  • 63
  • 1
  • 1
  • 6
  • Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is a knowledge base for *specific* programming problems -- not a coding, research, or tutorial resource. This appears to be a homework dump. Where is your effort? Where are the missing variables? What don't you understand about extending this to more dimensions? – Prune Feb 01 '20 at 00:29
  • It's not a homework dump. I came up with this code for a 2 dimensional data set, now I'm trying to use it for a 10 dimensional one. It's a practice problem from a book I'm learning from – Aloma85 Feb 01 '20 at 00:39
  • @prune try to give people the benefit of the doubt sometime! – Aloma85 Feb 01 '20 at 03:42

1 Answers1

0

I figured it out after a lot of tinkering.

# least squares cost function for linear regression
def least_squares(w):
    cost = 0
    for p in range(len(y)):
        # get pth input/output pair
        x_p = x[p]
        y_p = y[p]

        # form linear combination
        c_p = w[0] + w[10] * sum(x_p)

        # add least squares for this datapoint
        cost += (c_p - y_p) ** 2
    return cost
Aloma85
  • 63
  • 1
  • 1
  • 6