Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStephen Cole
Courses Plus Student 15,809 PointsMy list of of an array can be appended but my slice can not. I don't understand why.
This works:
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
1 Answer
Louise St. Germain
19,424 PointsHi Stephen,
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([[132],
[122],
[128],
[ 44],
[136],
[129],
... etc ...
[110],
[ 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)
But [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!
Stephen Cole
Courses Plus Student 15,809 PointsStephen Cole
Courses Plus Student 15,809 PointsThank you.
This makes so much sense now. I looked at the shape of the arrays and saw the numbers 100 and 1 and didn't recognize that they were out of order.
There are some things that I can see in my head and there are things that will forever elude me. I'll add this to a list of things that I have to diagram... Time Zones, currency conversion, and now matrix shapes.
Louise St. Germain
19,424 PointsLouise St. Germain
19,424 PointsGlad to be able to help!
I'm also a visual person so I diagram things too, and write down all kinds of stuff. As long as it works for you, it's a good way to go! :-) Happy programming!