wesm / pydata-book

Materials and IPython notebooks for "Python for Data Analysis" by Wes McKinney, published by O'Reilly Media
Other
22.27k stars 15.19k forks source link

The problem of chapter A3.1 #198

Open MaxforCherubim opened 7 months ago

MaxforCherubim commented 7 months ago

The trick can not work:

def demean_axis(arr, axis):

    means = arr.mean(axis)
    indexer = [slice(None)] * arr.ndim
    indexer[axis] = np.newaxis

    return arr - means[indexer]

Here is the way to figue out:

def demean_axis(arr, axis):

    means = arr.mean(axis)
    indexer = [slice(None)] * arr.ndim
    indexer[axis] = np.newaxis

    return arr - means[*indexer]