numpy .shape

·

1 min read

I started learning numpy and matplot these days for Machine learning, so I would like to jot down some of the stuff I learned while studying them.

numpy .shape

At first, I found numpy shape function confusing.

If the numpy array is x_train = np.array([1.0, 2.0, 3.0]), the x_train.shape returns (3,). Actually, we have to deal with more than a one-dimensional array.

x_train = np.array([[1.0, 2.0, 3.0], [1.0, 2.0, 4.0]])
y_train = np.array([300.0, 500.0])
print(f"x_train = {x_train}")
print(f"y_train = {y_train}")
print(x_train.shape)  # Output: (2, 3)

In this example, x_train is a two-dimensional array with 2 rows and 3 columns, and x_train.shape returns the tuple (2, 3). One this I stuck with was that np.array is matrix, so you can do x_train = np.array([[1.0, 2.0, 3.0], [1.0, 2.0]]). In this case, print(x_train.shape) just returns the number of rows, which is two.

Also, if you print x_train.shape[0], you can get the number of rows and by printing x_train.shape[1], you can get that of columns when x_train is a two dimensional array.