Python 方法接受10维数据

Python 方法接受10维数据,python,numpy,least-squares,Python,Numpy,Least Squares,我有这个最小二乘法,但我需要它来获得10维数据。这是我学习的一篇练习课文。我为二维数据集提出了这个方法。现在我需要让它在10维空间中工作,但我完全被它卡住了 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

我有这个最小二乘法,但我需要它来获得10维数据。这是我学习的一篇练习课文。我为二维数据集提出了这个方法。现在我需要让它在10维空间中工作,但我完全被它卡住了

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
这是编辑后我应该得到的结果

w = np.ones((11,1))
print (least_squares(w))
[ 7917.97952037]

经过一番修补,我终于找到了答案

# 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

欢迎来到StackOverflow,而且。。。在这里申请。StackOverflow是针对特定编程问题的知识库,而不是编码、研究或教程资源。这似乎是一个家庭作业转储。你的努力在哪里?缺失的变量在哪里?你不明白把它扩展到更多的维度有什么?这不是一个家庭作业垃圾场。我为一个二维数据集设计了这个代码,现在我正试图将它用于一个10维的数据集。这是我正在学习的一本书中的练习题from@prune试着在某个时候让人们受益于怀疑!