python - 如何估计逻辑回归中的权重 (theta) 值?
问题描述
我正在训练一个数据集,我必须根据性别、受抚养人、总收入、贷款金额、贷款期限、毕业、已婚、财产、自雇人士等特征来预测贷款状态(是/否)。我已经为它编写了一个逻辑回归模型。我面临的问题是,获得贷款的概率位于 [0,1] 之间的标签 (y_label) 对于所有条目返回等于 1(或 0.9999)。sigmoid 函数已用于评估权重 (theta) 和特征向量(每个条目的特征的 NumPy 数组)的点积概率。你能告诉我为什么当 theta (向量)具有较大的初始值时最终概率等于 1,而当 theta 具有较小的初始值时它等于 0?
我的代码看起来像这样它是一个 sigmoid 函数来计算 y 预测标签的概率
def calc_sigmoid(z):
p=1/(1+ np.exp(-z))
p=np.minimum(p, 0.9999)
p = np.maximum(p, 0.0001)
#print("value of sigmoid", p)
return p
计算成本的函数:
def calc_cost_func(theta,x):
y=np.dot(theta,np.transpose(x))
return calc_sigmoid(y)
计算误差的函数:
def calc_error(y_pred, y_label):
len_label=len(y_label)
cost= (-y_label*np.log(y_pred) - (1-y_label)*np.log(1-y_pred)).sum()/len_label
return cost
计算梯度下降的函数:
def gradient_descent(y_pred,y_label,x, learning_rate, theta):
len_label=len(y_label)
J= (-(np.dot(np.transpose(x),(y_label-y_pred)))/len_label)
theta-= learning_rate*J
return theta
训练数据的功能:
def train(y_label,x, learning_rate, theta, iterations):
list_cost=[]
for i in range(iterations):
y_pred=calc_cost_func(theta,x)
theta=gradient_descent(y_pred,y_label,x, learning_rate, theta)
if i%100==0:
print("\n iteration",i)
print("y_label:",y_pred)
print("theta:",theta)
cost=calc_error(y_pred, y_label)
list_cost.append(cost)
return theta, cost
从数据框贷款中提取数据:
'''
theta: array(1,no. of features)
theta_0: array(1,length of data)
x: array(no. of features, length of data)
y_label, y_pred: array(length of data)
'''
x_label=loan.iloc[:500, 1:10].values
x_rows, x_columns= x_label.shape
z = np.ones((x_rows,1), dtype=float)
x_label=np.append(x_label,z,axis=1)
x_label=x_label.astype(float)
y_label=loan.loc[:499,"Status_New" ].values
y_label=y_label.astype(float)
#theta=np.array([0.0000005,0.0000000455547,0.000000222203,0.0000066005,0.000000022505,0.0000000025059,0.000000002585,0.000025500049,0.00000000034,0.00000000068])
theta=np.array([50.0,20.0,40.0,10.0,5.0,35.0,12.0,40.0,69.0,40.5])
train(y_label,x_label,0.2,theta,1000)
请帮助我为什么我得到的概率等于 1 对于大 theta 值和 0 对于小 theta 值?
解决方案
推荐阅读
- java - 带有 pojo 注释的 BeanPropertySqlParameterSource
- elasticsearch - ElasticSearch - 查询以将字符串附加到文档字段中的现有字符串
- php - PHP fopen() as APPEND 是否比 file_get_(put)contents() 更好,以避免丢失多个请求的日志?
- jquery - 使用 Ajax 和 PHP
- r - RStudio 开始时为空
- react-native - 将 props 从根组件传递到 AppNavigator
- angular - Angular 8:动态路由仅在使用 RouterLink 时有效?
- c# - 切片器连接不显示可透视的更改后数据源
- c++ - "rdtsc": "=a" (a0), "=d" (d0) 这是做什么的?
- azure - .netcore 2.1 记录到 Azure Blob 的问题