python - Using numpy shape output in logic -
i using python 2.7.5 on windows 7. reason python doesn't when use 1 of dimensions of numpy array comparator in if statement:
a = np.array([1,2,3,4]) # reshapes array has 2 dimensions if len(np.shape(a)) == 1: = np.reshape(a, (1, np.shape(a))) b = np.shape(a)[0] if b <= 3: print 'ok'
i create 1d numpy array (in actuality 'a' input may 1d or 2d). reshape form 2d numpy array. try use size of newly created dimension comparator , error: "typeerror: integer required"
i tried "int(b)" convert long integer plain integer in if statement , gives same error. if "type(b)" gives me "type 'long'". feel though i've done before without problems, can't find examples. how change 1d array 2d array? appreciated.
the problematic line a = np.reshape(a, (1, np.shape(a)))
.
to add axis front of a
i'd suggest using:
a = a[np.newaxis, ...] print a.shape # (1, 4)
or none
same thing np.newaxis
.
Comments
Post a Comment