适用于机器学习的Python numpy和pandas

时间:2019-03-02 23:49:02

标签: python pandas numpy

只是免责声明,这个问题是关于功课的。从字面上看,我的教授说要利用这个网站来提供帮助。

我正在学习机器学习,虽然我们的教授是一位出色的数学家,但他可能在编程方面有些欠缺。

这里的游戏名称是读取代码部分并查找/修复错误。

我在这部分上花费了数小时,我认为我的问题是在数据框和numpy零之间有一个点积。

问题的出现类似于+的不受支持的操作数类型:'float'和'str'

我尝试阅读文档和此站点的解决方法,但是我对编程非常新手,尤其是像numpy和pandas这样的库

这是使用python熊猫编程

# Initialize the paarmeter set theta with zeros length as equal to column size in X
X = pd.DataFrame(X)
theta = np.zeros(X.shape[1], dtype = int)
print(theta)


def cost_function(X, y, theta):
    """
    cost_function(X, y, theta) computes the cost of using theta as the
    parameter for linear regression to fit the data points in X and y
    """
    ## number of training examples
    m = len(y) 

    ## Calculate the cost with the given parameters
    J = 1/(2*m)*np.sum((X.dot(theta)-y)**2)

    return J

#Initial cost
cost_function(X,y,theta)

运行最后一行会产生最多的问题。

1 个答案:

答案 0 :(得分:1)

只需使用np.dot(X,theta)而不是X.dot(theta)。这是编辑后的代码。

def cost_function(X, y, theta):
"""
cost_function(X, y, theta) computes the cost of using theta as the
parameter for linear regression to fit the data points in X and y
"""
## number of training examples
    m = len(y) 

## Calculate the cost with the given parameters
    J = 1/(2*m)*np.sum((np.dot(X,theta)-y)**2)

    return J

#Initial cost
cost_function(X,y,theta)

希望这会对您有所帮助。

相关问题