Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need help for the below question. And my code that I was submitted is not correct. I don't know how to do it.

Hi, I need help for the below question. And my code that I was submitted is not correct. I don't know how to do it. Please help me on the code. Thank you

Question:

Part D [5 pts, Peer Review]: Here we will use the trained model coefficients and generate the classification probabilities using the gen_logistic function we built. The goal of this section is to make you understand how logistic regression classifies data points during and after training. Using the predictions from the generated probabilities, you will compute the precision and recall metrics (defined below).

The gen_logistic function that mentioned in the above is below, and it is correct for the gen_function(I found the code in Chegg here) after my submission.

def gen_logistic(x, w=1, b=0): """ outputing the logistic output for an input x :param x: scalar or numpy array of shape (n_samples, n_features). If only one feature, it must have the shape of (n_samples,1). :param w: weight(s); either scalar or numpy array of shape (1, n_features) :param b: bias; either scalar or numpy array of shape (1,) returns y of shape (n_samples,) """ # TODO: Finish this function to return the output of applying the sigmoid # function to the input x (Please do not use external libraries) store # the output in y and return y. Do not change the default parameter values. # Hint: This function will be used in any input shape scalar (0d), 1d vector, and 2d arrays. Please make sure it can handle all those. Following reshaping codes might help. # Hint2: You may use design matrix using concatenation, but it is not necesary. y =0 if np.isscalar(x): x = np.array(x).reshape((1,1)) if np.isscalar(w): w = np.array(w).reshape((1,1)) if np.isscalar(b): b = np.array(b).reshape((1,1)) if b.shape==(1,): b= b.reshape((1,1))

# your code here y=[] m=1; s=x.shape for i in s: m*=i a=x.reshape(1,m)

for i in a[0]: y.append(max(0,1/(1+np.exp(-i)))) y=np.array(y).reshape(x.shape) return y

x=np.array([[1,4,90,567,-76,-2,78],[56,9,-9,67,-12,0,45]]) print(gen_logistic(x))

For the question stated above Part D:Part D [5 pts, Peer Review]: Here we will use the trained model coefficients and generate the classification probabilities using the gen_logistic function we built. The goal of this section is to make you understand how logistic regression classifies data points during and after training. Using the predictions from the generated probabilities, you will compute the precision and recall metrics (defined below).

My Submission code:

def calculate_precision(y_true, y_pred, pos_label_value=1.0): ''' This function accepts the labels and the predictions, then calculates precision for a binary classifier. Args y_true: np.ndarray y_pred: np.ndarray pos_label_value: (float) the number which represents the postiive label in the y_true and y_pred arrays. Other numbers will be taken to be the non-positive class for the binary classifier. Returns precision as a floating point number between 0.0 and 1.0 ''' # your code here yhat_probs = LogReg.predict(data.x_test) precision = precision_score(data.y_test, yhat_probs) print('Precision: %f' % precision) return 0.0

def calculate_recall(y_true, y_pred, pos_label_value=1.0): ''' This function accepts the labels and the predictions, then calculates recall for a binary classifier. Args y_true: np.ndarray y_pred: np.ndarray pos_label_value: (float) the number which represents the postiive label in the y_true and y_pred arrays. Other numbers will be taken to be the non-positive class for the binary classifier. Returns precision as a floating point number between 0.0 and 1.0 ''' # your code here yhat_probs = LogReg.predict(data.x_test) recall = recall_score(data.y_test, yhat_probs) print('Recall: %f' % recall) #metrics.recall_score(y_true, y_pred) return 0.0

This part is another cell after the above cell from the Lab and cannot change:

# testing cell # Sanity check tests ut_true = np.array([1.0, 1.0, 0.0, 0.0]) ut_pred = np.array([1.0, 1.0, 1.0, 1.0]) prec = calculate_precision(ut_true, ut_pred, 1.0) recall = calculate_recall(ut_true, ut_pred, 1.0)

The error after my submission:

AssertionError  in  Traceback Redacted AssertionError: Checks the prec value returned from your calculate_precision function.

Please help on the code, I don't know how to do it. Thank you so much.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4. Are there any disadvantages?

Answered: 1 week ago

Question

3. What are the main benefits of using more information technology?

Answered: 1 week ago

Question

start to review and develop your employability skills

Answered: 1 week ago