Python ‘return vector if vector.shape else vector.item()’

I’m learning gradient descent algorithm reading this article https://realpython.com/gradient-descent-algorithm-python/#minibatches-in-stochastic-gradient-descent and I’m stuck with one peace of Python code written by author. There is Python code in ‘Minibatches in Stochastic Gradient Descent’ chapter. I will post here only part I’m stuck with.

def sgd(
    gradient, x, y, start, learn_rate=0.1, batch_size=1, n_iter=50,
    tolerance=1e-06, dtype="float64", random_state=None
):
...
# Initializing the values of the variables
vector = np.array(start, dtype=dtype_)
...
return vector if vector.shape else vector.item()

I’m not experienced with Python and cannot realize when vector.shape may give false in if-else block so it would return vector.item() instead of vector. Any ideas would be appreciated. Thanks

Answer

This is explained by the author of the tutorial:

This is an interesting trick [refering to vector = np.array(start, dtype=dtype_)]: if start is a Python scalar, then it’ll be transformed into a corresponding NumPy object (an array with one item and zero dimensions). If you pass a sequence, then it’ll become a regular NumPy array with the same number of elements.

You can see the two different cases here:

>>> x = np.array([2021]) # shape (1,)
>>> x if x.shape else x.item()
array([2021])

Whereas a scalar will be returned as a scalar using item on the np.array:

>>> x = np.array(2021) # shape ()
>>> x if x.shape else x.item()
2021