简体   繁体   中英

How to get mean of matrix columns for a column of matrices inside dataframe?

I have a dataframe with two columns. The first column has the class number (either 1 or 0). The second column holds matrices that are (1999,13). I am trying to figure out how to convert the matrices to (1,13) by getting the mean of each matrix column.

The reason I am doing this is for audio processing. I extracted the MFCCs for each 10 second audio file I have. For each 10 second audio there are 1999 frames, and each frame has 13 cepstral coefficients.

example_df = pd.DataFrame()
example_df['Class'] = [1,0,0]
example_df['MFCCs'] =[np.random.rand(4,2),np.random.rand(4,2),np.random.rand(4,2)]
example_df

when I apply np.mean I am almost always getting the mean of the class as well which is about 0.5, even if I indicate the 'MFCCs' column.

The expected output should be something like

   Class  MFCCs
0  1      [C01,C02]
1  0      [C11,C12]
2  0      [C21,C22]

You can use np.mean and specify the axis across which you want to take the mean, in your case axis=0. For example:

a = np.arange(8).reshape(4,2)
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])

np.mean(a,axis=0)
array([3., 4.])

For your purpose you can do it on one line :

arrays = [np.random.rand(4,2),np.random.rand(4,2),np.random.rand(4,2)]
example_df['MFCCs'] =[np.mean(a,axis=0) for a in arrays]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM