Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introduction to NumPy Array Organization Indexing

spencer tintorri
spencer tintorri
6,184 Points

Appending [fake_log] at ~11:30

Appending fake_log fails, because the shapes don't match

study_minutes = np.append(study_minutes, fake_log, axis=0)

The workaround is to append [fake_log]

study_minutes = np.append(study_minutes, [fake_log], axis=0)

I wasn't clear on why this works. The best I can come up with is:

# the previous study_minutes was shape (2, 100)
# np.append returns a new array
# we're seeding this new array with the original study_minutes
# and appending a new, 3rd row of 100 items, which is [fake_log]
# the new returned array is shape (3, 100)
# trying to append fake_log (without brackets) will error
# because study_minutes is 2d and fake_log is 1d
# the workaround is to append [fake_log] (with brackets)
# [fake_log] is a list, whose contents are 1 array: fake_log
# [fake_log] is a 2d object, and matches the 2d shape of
# study_minutes, and can be appended using np.append

Can someone help explain this part a bit better? Thank you.