Question

In: Computer Science

Stochastic Gradient Ascent (SGA) for Logistic Regression. In the exer- cise, you will implement logistic regression...

Stochastic Gradient Ascent (SGA) for Logistic Regression. In the exer- cise, you will implement logistic regression algorithm using SGA, similar to the logistic regression algorithm that you have seen in class. You will work with the datasets attached to the assignment and complete the lo- gisticRegression.py file to learn the coefficients and predict binary class labels. The data comes from breast cancer diagnosis where each sample (30 features) is labeled by a diagnose: either M (malignant) or B (be- nign) (recorded in the 31-st column in the datasets). Read the main code, check the configuration parameters, and make sure the data is loaded and augmented correctly. Do not use logistic regression packages.

(a) Complete the function predict(x, w), gradient(x, y, w), and cross entropy(y hat, y) functions according to the instructions in lo- gisticRegression.py. These functions will be used in the main SGA algorithm (logisticRegression SGA).

Solutions

Expert Solution

I am using sigmoid function as the activation funcion.

predict(x,w)

              #I am writing only the main formulae and I am using numpy#

                m = x.shape[1]            
               Y_prediction = np.zeros((1,m))
s= np.dot(w.T,x)+b
y_hat= 1/(1+np.exp(-s))

for i in range(y_hat.shape[1]):
  
# Convert probabilities A[0,i] to actual predictions p[0,i]
  
if y_hat[0,i]>0.5:
Y_prediction[0,i]=1
else:
Y_prediction[0,i]=0
  
  
assert(Y_prediction.shape == (1, m))
  
return Y_prediction

gradient(x, y, w)

m = x.shape[1]
  
  
# FORWARD PROPAGATION (FROM X TO COST)
s=(np.dot(w.T,x))+b
y_hat= 1/(1+np.exp(-s)) # compute activation
  
  
  
# BACKWARD PROPAGATION (TO FIND GRAD)
  
dw= (np.dot(X,(A-Y).T))/m
db= (np.sum(A-Y))/m

cross entropy(y_hat, y)

cost = (-np.sum(y*np.log(y_hat)+(1-y)*np.log(1-y_hat)))/m                      # compute cost

return cost


Related Solutions

Stochastic Gradient Ascent (SGA) for Logistic Regression. In the exer- cise, you will implement logistic regression...
Stochastic Gradient Ascent (SGA) for Logistic Regression. In the exer- cise, you will implement logistic regression algorithm using SGA, similar to the logistic regression algorithm that you have seen in class. You will work with the datasets attached to the assignment and complete the lo- gisticRegression.py file to learn the coefficients and predict binary class labels. The data comes from breast cancer diagnosis where each sample (30 features) is labeled by a diagnose: either M (malignant) or B (be- nign)...
In the exer- cise, you will implement logistic regression algorithm using SGA, similar to the logistic...
In the exer- cise, you will implement logistic regression algorithm using SGA, similar to the logistic regression algorithm that you have seen in class. You will work with the datasets attached to the assignment and complete the lo- gisticRegression.py file to learn the coefficients and predict binary class labels. The data comes from breast cancer diagnosis where each sample (30 features) is labeled by a diagnose: either M (malignant) or B (be- nign) (recorded in the 31-st column in the...
Logistic Regression In logistic regression we are interested in determining the outcome of a categorical variable....
Logistic Regression In logistic regression we are interested in determining the outcome of a categorical variable. In most cases, we deal with binomial logistic regression with the binary response variable, for example yes/no, passed/failed, true/false, and others. Recall that logistic regression can be applied to classification problems when we want to determine a class of an event based on the values of its features.    In this assignment we will use the heart data located at   http://archive.ics.uci.edu/ml/datasets/Statlog+%28Heart%29 Here is the...
When should logistic regression be used for data analysis? What is the assumption of logistic regression?...
When should logistic regression be used for data analysis? What is the assumption of logistic regression? How to explain odds ratio?
compute the first three iterations of the gradient ascent algorithm applied to the function f(x) =...
compute the first three iterations of the gradient ascent algorithm applied to the function f(x) = -0.2 + x + x^2 - 5.5x^3 +4x^4. Assume initial value for x0 = 0.11 and alpha = 0.1.
define the logistic regression model.
define the logistic regression model.
What is the main purpose of logistic regression? Do you know other regression that can provide...
What is the main purpose of logistic regression? Do you know other regression that can provide similar estimates?
how would you check the robustness and validity of results in logistic regression
how would you check the robustness and validity of results in logistic regression
If a dependent variable is binary, is it optimal to use linear regression or logistic regression?...
If a dependent variable is binary, is it optimal to use linear regression or logistic regression? Explain your answer and include the theoretical and practical concerns associated with each regression model. Provide a business-related example to illustrate your ideas.
What is binary logistic regression, and how to use it?
What is binary logistic regression, and how to use it?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT