我有一个带有二进制结果的数据集。我使用Logistic回归进行预测。
这是我的数据结构的一个示例:
id Col_A ... y
1 5 ... 1
2 10 ... 1
3 500 ... 0
4 705 ... 0
5 2 ... 1
我想对我的逻辑回归函数进行衰减 即预测结果将取决于col_A:如果col_A包含高值,则预测必须收敛为0(指数函数)。这样我将获得概率值而不是二进制
有可能这样做吗?如果可能的话,我该如何实现?
PS:我已经将coll_A作为功能添加到logit中,但是它的行为不正确 我使用followig Logistic回归源代码:
class LogisticRegressionUsingGD:
@staticmethod
def sigmoid(x):
return 1 / (1 + np.exp(-x))
@staticmethod
def logit(theta, x):
return np.dot(x, theta)
def p_hat(self, theta, x):
return self.sigmoid(self.logit(theta, x))
def cost_function(self, theta, x, y):
m = x.shape[0]
a = y * np.log(self.p_hat(theta, x))
b = (1 - y) * np.log(1 - self.p_hat(theta, x))
total_cost = -(1 / m) * np.sum(a + b)
return total_cost
def gradient(self, theta, x, y):
m = x.shape[0]
return (1 / m) * np.dot(x.T, self.sigmoid(self.logit(theta, x)) - y)
def fit(self, x, y, theta):
opt_weights = fmin_tnc(func=self.cost_function, x0=theta, fprime=self.gradient, args=(x, y.flatten()))
self.w_ = opt_weights[0]
return self
def predict(self, x):
theta = self.w_[:, np.newaxis]
return self.p_hat(theta, x)