Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I'm trying to break down a program line by line. Y is a matrix of data but I can't find any concrete data on what .shape[0] does exactly.

for i in range(Y.shape[0]): 

if Y[i] == -1:

This program uses numpy, scipy, matplotlib.pyplot, and cvxopt.

1 Answer

0 votes
by (106k points)

The shape attribute for numpy arrays returns the dimensions of the array. If Y has n rows and m columns, then Y.shape is (n,m). So Y.shape[0] is n.

>>Y = np.arange(12).reshape(3,4) 


array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) 


(3, 4) 



To know more about this you can have a look at the following video tutorial:-

Browse Categories