Implement the precision metric for binary classification. Precision is the ratio of true positives to the total number of predicted positives (true positives + false positives).
import numpy as np
def precision(y_true, y_pred):
y_true = np.array(y_true)
y_pred = np.array(y_pred)
true_positives = np.sum((y_pred == 1) & (y_true == 1))
predicted_positives = np.sum(y_pred == 1)
if predicted_positives == 0:
return 0.0
return float(true_positives / predicted_positives)