1
\$\begingroup\$

This is for machine learning. I have 5 matlab files: AWA, REM, S1, S2, SWS. Each one has 2 arrays: x and y. y are the labels. I want to know the number of instances for each category in each file:

  1. I load the matlab file

  2. I convert from f8 to int32

  3. If I do not use ravel, bincount will not work

  4. I count the instances for each category in the file

    I repeat these 4 steps 5 times (as there are 5 files). Is there a way to do it better?

  5. Then I just join all the vectors in one array to send them to PANDAS.

#AWA
awa_y=sio.loadmat('/home/AWA_FeaturesAll.mat')['y']
awa_y=awa_y.astype('int') #from f8 to int32 
awa=np.ravel(awa_y) #needed in order to use np.bincount()
AWA=np.bincount(awa)

#Rem
rem_y=sio.loadmat('/home/Rem_FeaturesAll.mat')['y']
rem_y=rem_y.astype('int') 
rem=np.ravel(rem_y)
REM=np.bincount(rem)

#S1
s1_y=sio.loadmat('/home/S1_FeaturesAll.mat')['y']
s1_y=s1_y.astype('int') 
s1=np.ravel(s1_y)
S1=np.bincount(s1)

#S2
s2_y=sio.loadmat('/home/S2_FeaturesAll.mat')['y']
s2_y=s2_y.astype('int')
s2=np.ravel(s2_y)
S2=np.bincount(s2)

#SWS
sws_y=sio.loadmat('/home/SWS_FeaturesAll.mat')['y']
sws_y=sws_y.astype('int')
sws=np.ravel(sws_y)
SWS=np.bincount(sws)

#Joining the 5 vectors 
table= (np.vstack((AWA, REM, S1, S2, SWS))).T 
TABLE = np.delete(table, (0), axis=0) 
print(TABLE)
\$\endgroup\$
1
  • \$\begingroup\$ sio appears to be scipy.io. \$\endgroup\$ Commented Sep 27, 2016 at 20:30

1 Answer 1

5
\$\begingroup\$

You felt it right, there's a lot of unnecessary copy/paste in the first part.

Your 5 data structures are similar, and you create a lot of variables to perform the same processing 5 times.

I did a quick factorisation which brings back the code to a reasonable 12 lines (I could not test, obviously, but that seems OK):

result_list = []

for name in ["AWA","Rem","S1","S2","SWS"]:
    xx_y=sio.loadmat('/home/{}_FeaturesAll.mat'.format(name))['y'].astype('int')
    xx=np.ravel(xx)
    result_list.append(np.bincount(xx))

#Joining the 5 vectors
table= (np.vstack(result_list)).T
TABLE = np.delete(table, (0), axis=0)
print(TABLE)

As the names are logical, I don't have to make a list of full names, only a part is enough. I just apply 5 times the same operation in the correct order, then store in result_list.

Joining the vectors is now a piece of cake. Just pass the result list.

Had you needed the individual vectors further in your code, then a collections.OrderedDict would be better to store them by name and recall them afterwards.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.