Week 1 : Data in tensorflow : Error in video and text transcription

In the video at 5 mins 33 secs Andrews says this;

“So this is just a 1D array that has no rows or columns, although by convention we may right x as a column like this.”

when talking about Python this code using Numpy;

x = np.array([200, 17])

Surely this is an array with two columns, not one?

Did you try the experiment? Please post your results.

What experiment?

Does Andrew ask to try an experiment in the lesson?

I refer to doing your own experiment to investigate the results for the question you are asking.

I get this…

Which Looks like a Python List of two elements. Each element appears to be in a separate column, just like the elements in a 2-D Numpy array. So it is confusing for Andrew to say it is a 1-D array with no rows or columns.

Try printing the shape of x.

I get this result;

I have no idea what this result means.

Yes, I think it’s a list also.

Its not a Python List. It looks like a Python Tuple of two values; 2 and no value. Very strange.

A numpy shape is reported via a tuple.

What is a “numpy shape”. Is it a data type?

Numpy data types support a “shape” method.
The shape method tell you the size and orientation of an object.

That means that the array is a one dimensional object and that dimension has size 2.

Now watch this:

y = np.array([[200, 17]])
print(y.shape)
(1, 2)

So that says that y is a 2 dimensional object and the dimensions have sizes 1 and 2, meaning that it is a 1 x 2 array. Which we can also call a row vector.

See the syntactic difference there in terms of what I did differently to get that result?

I’m confused.

Is a numpy shape a method or a class instance or a data type.

When you say it is a “…one dimensional object…”, do you mean it is a class instance?

No.

The numpy ‘shape’ method returns a tuple that gives the dimensions of that object.

Hi @ai_is_cool,

Surely this is an array with two columns, not one?

Not quite! In NumPy, x = np.array([200, 17]) creates a 1D array, which doesn’t have explicit rows or columns – just a single axis.

You can check its shape:

import numpy as np

x = np.array([200, 17])
print(x.shape)  # Output: (2,)

Alternatively I can define x as

x = np.array([200, 
               17])
print(x.shape)  # Output: (2,)

Since the shape is (2,), it means the array has 2 elements along one axis, but no second axis (no explicit “rows” or “columns”).

If you want it to be explicitly a column vector (2 rows, 1 column), you’d reshape it:

x = np.array([200, 17]).reshape(2, 1)  # Shape (2,1)

Or use:

x = np.array([[200], [17]])  # Also Shape (2,1)

If you want a row vector (1 row, 2 columns), you’d reshape it like this:

x = np.array([200, 17]).reshape(1, 2)  # Shape (1,2)

Or:

x = np.array([[200, 17]])  # Also Shape (1,2)
5 Likes

Is a numpy shape a method or a class instance or a data type.

Let’s take a look into __init__.pyi file, which contains function signatures, class definitions, and type hints:

class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
    ...
    @property
    def shape(self) -> _Shape: ...
    @shape.setter
    def shape(self, value: _ShapeLike) -> None: ...

The @property decorator means that shape behaves like an attribute, but it has a getter (def shape(self) -> _Shape) and a setter (@shape.setter). It returns an instance of _Shape, which is a type alias representing a tuple of integers, like (3, 4, 5), which describes the dimensions of the array: _Shape = tuple[int, ...]. It also can be set with _ShapeLike.

import numpy as np

arr = np.zeros((3, 4))
print(arr.shape)  # (3, 4)  <- Returns a tuple (not a method call)

arr.shape = (4, 3)  # Reshapes the array
print(arr.shape)  # (4, 3)
1 Like

An object is a class instance.