留学生考试辅导 NumpyPrecisionRecallExtra-1

NumpyPrecisionRecallExtra-1

Extra credit: Model evaluation using numpy (10 points)¶

Copyright By PowCoder代写 加微信 powcoder

Imagine you are building a model where the goal is to predict a binary response variable (0 vs. 1). You have determined that based on business need, the best evaluation metric would be the F1 score, which is a harmonic mean of precision and recall:

Accuracy, Precision, Recall & F1 Score: Interpretation of Performance Measures


Your job is to write a function that would take in two arrays – one is a rank 1 array of predicted probabilities (between 0 and 1) and the other is a 2-dim array of actual/true values of the same length of shape (1, length), and a cutoff, and to compute and return both precision and recall. You cannot use any loops.

def recall_precision(pred, actuals, cutoff):

pred is a an array of shape (length, )
actuals is an array of shape (length, 1) – all consist of 0’s or 1’s
cutoff is the cutoff, greater than which prediction would be considered as ‘1’. For example, if cutoff is 0.5, then
any prediction 0.5 and greater would be predicted as ‘1’
#your code goes next
print(f”Logistic regression model’s recall on test set = {recall:.4f}”)
print(f”Logistic regression model’s precision on test set = {precision:.4f}”)
return recall, precision
#your code goes here

return recall, precision

#example below:
recall_precision(pred, actual, 0.5)

[[0.51 0. ]
[0.3 1. ]
[0.9 1. ]
[0.6 1. ]
[0.59 1. ]
[0.2 0. ]]
Logistic regression model’s recall on test set = 0.7500
Logistic regression model’s precision on test set = 0.7500

(0.75, 0.75)

import numpy as np
pred = np.array([0.51, 0.3, 0.9,0.6,0.59,0.2])
pred.shape

actual = np.array([[0,1,1,1,1,0]])
actual.shape

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com