Thank you Hassan for your reply. I followed all the steps outlined in the exercise.
y_true: array([[1, 0],
[1, 0],
[1, 0],
[1, 0],
[0, 1]])
using the axis:
using axis = 0 [4 1]
using axis = 1 [1 1 1 1 1]
w_p = np.sum(y_true == 0,axis=0) / y_true.shape[0]
w_p
output: array([0.2, 0.8])
w_n = np.sum(y_true == 1, axis=0) / y_true.shape[0]
w_n
output:
array([0.8, 0.2])
y_pred = np.ones(y_true.shape)
y_pred[:,0] = 0.3 * y_pred[:,0]
y_pred[:,1] = 0.7 * y_pred[:,1]
y_pred
output: array([[0.3, 0.7],
[0.3, 0.7],
[0.3, 0.7],
[0.3, 0.7],
[0.3, 0.7]])
print(f"w_p[0]: {w_p[0]}“)
print(f"y_true[:,0]: {y_true[:,0]}”)
print(f"y_pred[:,0]: {y_pred[:,0]}")
output: w_p[0]: 0.2
y_true[:,0]: [1 1 1 1 0]
y_pred[:,0]: [0.3 0.3 0.3 0.3 0.3]
loss_0_pos = -1 * np.sum(w_p[0] *
y_true[:, 0] *
np.log(y_pred[:, 0])
)
print(f"loss_0_pos: {loss_0_pos:.4f}")
output: loss_0_pos: 0.9632
print(f"w_n[0]: {w_n[0]}“)
print(f"y_true[:,0]: {y_true[:,0]}”)
print(f"y_pred[:,0]: {y_pred[:,0]}")
output: w_n[0]: 0.8
y_true[:,0]: [1 1 1 1 0]
y_pred[:,0]: [0.3 0.3 0.3 0.3 0.3]
loss_0_neg = -1 * np.sum(
w_n[0] *
(1 - y_true[:, 0]) *
np.log(1 - y_pred[:, 0])
)
print(f"loss_0_neg: {loss_0_neg:.4f}")
output: loss_0_neg: 0.2853
loss_0 = loss_0_neg + loss_0_pos
print(f"loss_0: {loss_0:.4f}")
output: loss_0: 1.2485
I am trying to do this next one for class 1 “Can you calculate the loss for class (column) 1
” and I got this error:
1 print(f"w_p[1]: {w_p[1]}“)
----> 2 print(f"y_true[:,1]: {y_true[:, 1]}”)
3 print(f"y_pred[:,1]: {y_pred[:, 1]}")
IndexError: index 1 is out of bounds for axis 1 with size 1.
Kindly review my code and let me know the next step. Thank you.