2016-10-31 7 views
0

私は6つのファイル(AWA、REMなど)を混乱マトリクスセクションに送る前にそれを呼び出すforループを持っています。 生成された各リスト(6リスト)を保存しておきたいので、後で呼び出すことができ、混乱行列をプロットすることができます。多くのリストを別のファイルに保存するにはどうすればいいですか?

私はピクルスを使用しました。

ここ

コード:

#Here I load the matlab files 
for name in ["AWA", "Rem", "S1","S2","SWS","stades"]: 
    x=sio.loadmat('/home/{}_FeaturesAll.mat'.format(name))['x'] 
    s_y=sio.loadmat('/home/{}_FeaturesAll.mat'.format(name))['y'] 
    y=np.ravel(s_y) 

    print(name, x.shape, y.shape) 
    print("") 

#Here come code of the classifier and the cross validation 
. 
. 
. 

##########################Confusion Matrix######################### 
from sklearn.metrics import confusion_matrix 
for train_index, test_index in sss:                                
    x_train, x_test = x[train_index], x[test_index] 
    y_train, y_test = y[train_index], y[test_index] 

    y_pred = clf.fit(x_train, y_train).predict(x_test) 

    cm = confusion_matrix(y_test, y_pred) 
    np.set_printoptions(precision=3) 

    list_cm.append(cm) #I WANT TO SAVE THIS LIST FOR EACH FILE: AWA_list_cm, Rem_list_cm, etc 


    #MY ATTEMPT SAVING 1 FILE 
    ##################The pickling#################### 
    with open("list_cm.txt", "wb") as fp: 
      pickle.dump(list_cm, fp) 


#MY ATTEMPT RECOVERING MY SAVED FILE 
####Here I call the list to do the plotting##### 
with open("list_cm.txt", "rb") as fp: 
    list_cm = pickle.load(fp) 
+0

私はコードレビューから修正したコードを認識:) –

+0

私はそれを解決するのに役立ちます。はい、私はもう一度です。 – Aizzaac

+0

私は大好きですが、質問を編集して1つのファイルを保存しようとする試みを表示できますか?私はscipyにあまり慣れていないからです。 –

答えて

0

私は唯一の ".format(名)" を追加

############The pickling for any # of files################   
with open("{}_cm.txt".format(name), "wb") 
     pickle.dump(list_cm, fp) 



######################"unpickling for any # of files"#################### 
for name in ["AWA", "Rem", "S1" ,"S2","SWS","stades"]: 

    with open('{}_cm.txt'.format(name), "rb") as fp: 
     list_cm = pickle.load(fp) 
関連する問題