O.rka O.rka - 1 year ago 267
Python Question

np.sum for row axis not working (Python | Numpy)

I wrote a softmax regression function

def softmax_1(x)
that essentially takes in a
m x n
matrix, exponentiates the matrix, then sums the exponentials of each column.

x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])
#scores shape is (3, 80)

def softmax_1(x):
"""Compute softmax values for each sets of scores in x."""

Converting it into a DataFrame I have to transpose

DF_activation_1 = pd.DataFrame(softmax_1(scores).T,index=x,columns=["x","1.0","0.2"])

So I wanted to try and make a version of the softmax function that takes in the transposed version and computes the softmax function

scores_T = scores.T
#scores_T shape is (80,3)

def softmax_2(y):

DF_activation_2 = pd.DataFrame(softmax_2(scores_T),index=x,columns=["x","1.0","0.2"])

Then I get this error:

Traceback (most recent call last):
File "softmax.py", line 22, in <module>
DF_activation_2 = pd.DataFrame(softmax_2(scores_T),index=x,columns=["x","1.0","0.2"])
File "softmax.py", line 18, in softmax_2
ValueError: operands could not be broadcast together with shapes (80,3) (80,)

Why doesn't this work when I transpose and switch the axis in the

Answer Source




np.exp(y)/np.sum(np.exp(y),axis=1, keepdims=True)

This will mean that np.sum will return an array of shape (80, 1) rather than (80,), which will broadcast correctly for the division. Also note the correction to the bracket closing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download