Logistic Regression Numerical Exercises
Logistic Regression Numerical Exercises
The odds are calculated as P(event)/(1-P(event)) = 0.75/0.25 = 3. The log-odds (logit) is log(odds) = log(3) ≈ 1.098612 .
Compute the error term as y - y_hat (1 - 0.65 = 0.35). The gradient of the cost function with respect to theta_0, theta_1, and theta_2 is calculated as: for theta_0: 0.35; for theta_1: 0.35 * x1 = 0.35 * 2 = 0.7; for theta_2: 0.35 * x2 = 0.35 * 1 = 0.35. Parameters updated by subtracting the product of the learning rate (0.01) and the gradients from the current parameters .
First, compute the value of z as z = 3(2) - 2(1) + 0.5 = 6 - 2 + 0.5 = 4.5. The sigmoid function is given by 1 / (1 + exp(-z)). Therefore, the probability is 1 / (1 + exp(-4.5)) ≈ 0.989013057 .
Given model coefficients and feature values, calculate the linear combination to find z, then apply the sigmoid function, 1/(1 + exp(-z)), to find the probability of being diabetic. The specific values for age and BMI were not provided, but they would be plugged into the logistic equation .
The regularized cost J(theta) = 0.6 + 0.1 * 0.2 = 0.62 .
A coefficient in logistic regression represents the change in the log-odds of the outcome for a one-unit increase in the predictor variable, holding all other variables constant. For age in this context, it affects the odds of being diabetic by altering the log-odds proportionally .
Increasing lambda in L2 regularization penalizes larger weights, encouraging smaller model parameters which can reduce model complexity, helping to mitigate overfitting but may underfit the data if lambda is too large. This results in a trade-off between bias and variance .
Log-loss is calculated as -1/n * Σ (y_i * log(p_i) + (1 - y_i) * log(1 - p_i)), where n is the number of samples. Plugging in the values: (-1/4) * [(1 * log(0.9) + (0 * log(0.1)) + (0 * log(0.7) + 1 * log(0.3)) + (1 * log(0.6) + 0 * log(0.4))] ≈ 0.361711 .
Precision = TP / (TP + FP) = 2 / (2 + 1) = 0.67, Recall = TP / (TP + FN) = 2 / (2 + 1) = 0.67, F1-score = 2 * (Precision * Recall) / (Precision + Recall) ≈ 0.67. True Positives (TP): 2, False Positives (FP): 1, False Negatives (FN): 1 .
If the predicted probability (0.65) is greater than the decision threshold (0.5), the predicted class is 1 .