valueerroroperands could not be broadcast together with shapes ( , ( figure - broadcasting over axis of array it' quite common to want to perform an arithmetic operation with lower dimensional array across axes other than axis according to the broadcasting rulethe "broadcast dimensionsmust be in the smaller array in the example of row demeaning above this meant reshaping the row means to be shape ( instead of ( ,)in [ ]arr arr mean( reshape(( )out[ ]array([ - ][- - ][- - ] - ]]in the three-dimensional casebroadcasting over any of the three dimensions is only matter of reshaping the data to be shape-compatible see figure - for nice visualization of the shapes required to broadcast over each axis of three-dimensional array very common problemthereforeis needing to add new axis with length specifically for broadcasting purposesespecially in generic algorithms using reshape is one optionbut inserting an axis requires constructing tuple indicating the new shape this can often be tedious exercise thusnumpy arrays offer special syntax for inserting new axes by indexing we use the special np newaxis attribute along with "fullslices to insert the new axisin [ ]arr np zeros(( )in [ ]arr_ arr[:np newaxis:in [ ]arr_ shape out[ ]( in [ ]arr_ np random normal(size= in [ ]arr_ [:np newaxisout[ ]in [ ]arr_ [np newaxis:out[ ]array([[- - ]]broadcasting www it-ebooks info