I have a function that, among other things, calculates the mean of rows a ndarray (2d or 1d). This is by way of ndarray.mean(axis=0)
For the 1d array, I'd like it to just return itself since there is only 1 "row", instead of averaging the elements and returning a scalar.
Is there a pythonic way to do this other than just checking the ndim attribute before taking the average?
def d_Error(X, y, weights, bias):
y_hat = probability(X, weights, bias)
dE_matrix = (X.T * (y - y_hat)).T # each row is the gradient at that sample
dEdw = np.mean(dE_matrix, axis=0) # get average gradient
dEdb = (y - y_hat).mean() # gives scalar
dEdz = np.append(dEdw, dEdb)
return dEdz