[Numpy-discussion] result shape from dot for 0d, 1d, 2d scalar

Sebastian Berg sebastian@sipsolutions....
Tue Nov 27 10:16:31 CST 2012


On Mon, 2012-11-26 at 13:54 -0500, Skipper Seabold wrote:
> I discovered this because scipy.optimize.fmin_powell appears to
> squeeze 1d argmin to 0d unlike the other optimizers, but that's a
> different story.
> 
> 
> I would expect the 0d array to behave like the 1d array not the 2d as
> it does below. Thoughts? Maybe too big of a pain to change this
> behavior if indeed it's not desired, but I found it to be unexpected.

I don't quite understand why it is unexpected. A 1-d array is considered
a vector, a 0-d array is a scalar.

> [255]: np.version.full_version # same on 1.5.1
> [255]: '1.8.0.dev-8e0a542'
> 
> 
> [262]: arr = np.random.random((25,1))
> 
> 
> [~/]
> [263]: np.dot(arr, np.array([1.])).shape
> [263]: (25,)
> 
Matrix times vector = vector
> 
> [~/]
> [264]: np.dot(arr, np.array([[1.]])).shape
> [264]: (25, 1)
> 
Matrix times matrix = matrix
> 
> [~/]
> [265]: np.dot(arr, np.array(1.)).shape
> [265]: (25, 1)
> 
matrix times scalar = matrix (of same shape)
> 
> [~/]
> [271]: np.dot(arr.squeeze(), np.array(1.)).shape
> [271]: (25,)
> 
vector times scalar = vector (of same shape)
> 
> Huh? 0d arrays broadcast with dot?
> 
Remember a 0-d array is a scalar, there is no actual broadcasting
involved here. (except that vectors (1-d arrays) are special)

> [~]
> [279]: arr = np.random.random((25,2))
> 
> 
> [~/]
> [280]: np.dot(arr.squeeze(), np.array(2.)).shape
> [280]: (25, 2)
> 
> 
> Skipper
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list