python-3.x - fit() 缺少 1 个必需的位置参数:“theta”
问题描述
我试图实现一个逻辑回归模型。我收到以下错误消息
类型错误:fit() 缺少 1 个必需的位置参数:'theta'
这是我的代码
if __name__ == "__main__":
# X = feature values, all the columns except the last column
X = data.iloc[:, :-1]
# y = target values, last column of the data frame
y = data.iloc[:, -1]
# filter out the applicants that got admitted
admitted = data.loc[y == 1]
# filter out the applicants that din't get admission
not_admitted = data.loc[y == 0]
X = np.c_[np.ones((X.shape[0], 1)), X]
y = y[:, np.newaxis]
theta = np.zeros((X.shape[1], 1))
def sigmoid(x):
# Activation function used to map any real value between 0 and 1
return 1 / (1 + np.exp(-x))
def net_input(theta, x):
# Computes the weighted sum of inputs
return np.dot(x, theta)
def probability(theta, x):
# Returns the probability after passing through sigmoid
return sigmoid(net_input(theta, x))
def cost_function(self, theta, x, y):
# Computes the cost function for all the training samples
m = x.shape[0]
total_cost = -(1 / m) * np.sum(
y * np.log(probability(theta, x)) + (1 - y) * np.log(
1 - probability(theta, x)))
return total_cost
def gradient(self, theta, x, y):
# Computes the gradient of the cost function at the point theta
m = x.shape[0]
return (1 / m) * np.dot(x.T, sigmoid(net_input(theta, x)) - y)
def fit(self, x, y, theta):
opt_weights = fmin_tnc(func=cost_function, x0=theta,
fprime=gradient,args=(x, y.flatten()))
return opt_weights[0]
parameters = fit(X, y, theta)
类型错误:fit() 缺少 1 个必需的位置参数:'theta'
解决方案
推荐阅读
- git - Visual Studio > 团队资源管理器 > 更改 > 与未修改的比较。如何复制左/右?
- postgresql - 是否可以让休眠从 postgresql 的值语句生成更新?
- python - 如何使用字符串值列出 Kivy-Garden Graph x_axis major_tick 标签?
- sql-server - 在下面的过程中运行时,插入值语句只能包含常量文字值或变量引用错误
- python - json.decoder.JSONDecodeError 同时将响应转换为 json
- javascript - 根据百分比自动调整css
- android - 如何删除离子中的下载图标?
- docker - Docker:从 docker hub 拉取,不下载任何文件
- python - Python我无法在烧瓶中打开文件'app.py'
- javascript - 使用 AJAX 的多个实时搜索 Laravel