Search code examples
pythonarraysnumpyrow

Cannot select rows out of numpy array to perform an std


import numpy as Np I need to calculate the std on the first three rows of a numPy array I made with y = Np.random(100, size = (5, 3)).

The above produced the array I am working on. Note that I have since calculated the median of the array after having removed the 2 smallest values in the array with: y=Np.delete(y, y.argmin()) y=Np.delete(y, y.argmin()) Np.median(y)

When I call y now it no longer is in a square matrix. It comes all on one line like array([48, 90, 67, 26, 53, 16, 19, 64, 51, 47, 54, 91, 36]).

When I try to slice it and calculate an standard deviation (std) I get an IndexError. I think it is because this array is now a tuple.


Solution

  • As other people suggested the question format is not clear. Here what I tried:

    import numpy as np
    y = np.random.randint(100, size = (5, 3))
    y 
    array([[65, 84, 56],
       [90, 44, 42],
       [51, 58,  9],
       [82,  1, 91],
       [96, 32, 24]])
    

    Now to compute std for each row:

    y.std(axis=1)
    array([11.6714276 , 22.1710522 , 21.63844316, 40.47221269, 32.22145593])
    

    Since you just want the first 3 rows you can slice the result:

    result = y.std(axis=1)[:3]
    result
    array([11.6714276 , 22.1710522 , 21.63844316])
    

    Alternatively you can first select/slice the 1st 3 rows and then use std:

    y[:3].std(axis=1)
    array([11.6714276 , 22.1710522 , 21.63844316])