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.
Stephen ColeCourses Plus Student 15,592 Points
My list of of an array can be appended but my slice can not. I don't understand why.
new_fake_log = np.append(study_minutes, [fake_log], axis=0)
I get an array of arrays.
However, when I want to use the example presented in the notes using a slice, it does not.
new_fake_log = np.append(study_minutes, fake_log[:, np.newaxis], axis=0)
If I remove
axis=0 from the line, the
fake_log array is appended but the array is flattened.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-61-947f56cecdd6> in <module> ----> 1 new_fake_log = np.append(study_minutes, fake_log[:, np.newaxis], axis=0) 2 new_fake_log /anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in append(arr, values, axis) 4526 values = ravel(values) 4527 axis = arr.ndim-1 -> 4528 return concatenate((arr, values), axis=axis) ValueError: all the input array dimensions except for the concatenation axis must match exactly
Louise St. Germain19,412 Points
Yes, I can see where the confusion would come from! In the Teacher's Notes, I think Craig was trying to get at the general fact (not related to the specific example in the video) that there is more than one way to get a 2D array from a 1D array - I don't think he was trying to propose this particular code (with the nd.newaxis) as an equivalent substitute to what was laid out in the video.
The issue with the second line you mention above, with nd.newaxis, is that it does create a 2D array, but it creates it in the opposite direction to what you need. In other words, if I isolate just
fake_log[:, np.newaxis], I get:
array([, , , [ 44], , , ... etc ... , [ 58], [ 93], [ 79]], dtype=uint16)
And if I do np.shape of that, I get (100, 1), i.e. 100 rows that are all 1 column each. This is the opposite of [fake_log] in the original example, which is (1, 100), or 1 row that has 100 columns.
The error you get when you're trying to append the (100, 1) matrix to the study_minutes, which contains arrays that are all (1, 100) is that the number of rows and columns don't match up like they need to. In theory, you could fix the problem by swapping the np.newaxis with the : (i.e., the rows and columns), like so:
new_fake_log = np.append(study_minutes, fake_log[np.newaxis, :], axis=0)
[fake_log] is easier and gives the same result, so might as well just do that. :-)
The flattened array when you don't specify an axis is the typical default behaviour for np.append. This would have happened even if the array dimensions had matched, like:
new_fake_log = np.append(study_minutes, [fake_log])
I hope this helps clarify things a bit!