You use the mean() function of Numpy to calculate the mean. Instead of using the "for" loop, you can try slicing operation on Numpy array.
Here is an example to show how to use slicing:
>>> import numpy as np
>>> psx=np.array([[0.3,0.7],[0.6,0.4],[0.1,0.9],[0.8,0.2]])
>>> psx
array([[0.3, 0.7],
[0.6, 0.4],
[0.1, 0.9],
[0.8, 0.2]])
>>> y_test=np.array([1,0,0,1])
>>> y_test
array([1, 0, 0, 1])
>>> K = len(np.unique(y_test))
>>> K
2
>>> th = np.asarray([np.mean(psx[:, k][y_test == k]) for k in range(K)])
>>> th
array([0.35, 0.45])
If you want to calculate n-percentile, you can try the following code:
>>> np.asarray([np.percentile(psx[:, k][y_test == k], 95, axis=0) for k in range(K)])
array([0.575, 0.675])