首页 > 解决方案 > 实现逻辑回归“TypeError:fit() 缺少 1 个必需的位置参数:'y'”

问题描述

我在做什么?
尝试实现逻辑回归算法将特征分类为通过或失败。

代码:

def fit(self, theta, x, y):
    opt_weights = fmin_tnc(func = cost_function, x0 = theta, fprime = gradient, args = (x, y.flatten()))
    return opt_weights
parameters = fit(X, y, theta)

错误:

----> 1 个参数中的 TypeError Traceback (最近一次调用最后一次) = fit(X, y, theta)

类型错误:fit() 缺少 1 个必需的位置参数:'y'

这里有什么错误?

标签: pythonregressiontypeerror

解决方案


您应该删除该self参数。

那是当您的方法是类的一部分时。根据您的使用示例,它只是一个不属于某个类的函数。

def fit(theta, x, y):
    opt_weights = fmin_tnc(func = cost_function, x0 = theta, fprime = gradient, args = (x, y.flatten()))
    return opt_weights

parameters = fit(X, y, theta)

推荐阅读