Softmax regression cost function code

I really do not understand what does this code do

M = sparse.coo_matrix(([1]*n, (Y, range(n))), shape=(k,n)).toarray()

The code is related to calculating the sparse function in this equation, but I am really confused and I do not know how it iterates through it and what is: 1- sparse.coo_matrix 2- (Y, range(n))) 3-shape=(k,n)).toarray() ??

Also, What exactly does this term means in the equation and how to interpret it into code:

Thank you , and please forgive my poor English.

Topic sparse softmax multilabel-classification scipy machine-learning

Category Data Science


It's a loss function applied to a regression with l2 penalty on the parameters. The first square brackets can be interpreted in the following way:

  • $ - \frac{1}{n} $ has the minus because it wants to minimize.
  • $\sum_{i=1}^{n}$ means for each data point.
  • $\sum_{j=0}^{k-1} $ means for each class.
  • $y_i == j$ means that the fraction after this term is calculated only for true class. This check makes sense because y true is an OHE vector like [0, 0, 1], therefore you want to evaluate only the predicted probability associated with the true class. Take into account softmax function: if you increase the probability of a single output in output in the softmax you are implicitly reducing the probabilities of the other outputs.
  • the log division is the softmax function.
  • $ + \frac{\lambda}{2} \dots $ is a l2 regularization term on the model parameters.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.