2017-09-26 7 views
0

再び、私は非常にPythonに慣れています。以下は、私のコード(機能選択による分類)であり、かなり高次元なのでデータではありませんが、問題はデータにはまったく依存していないと考えています。私の質問は二つ折りです:私はすべてのサブプロットの軸ラベルが必要です。私はサブプロットの数が行ごとに異なることができるサブプロットをどのようにすることができますか(私は14のサブプロットを現在3行にしています):Pythonの行ごとに異なる数のサブプロットでサブプロットする

import matplotlib.pyplot as plt 
from sklearn.svm import SVC 
from sklearn.model_selection import StratifiedKFold 
from sklearn.feature_selection import RFECV 
from sklearn.datasets import make_classification 
from sklearn import preprocessing 
import scipy.io as sio 
import numpy as np 
import os 

allData = sio.loadmat('Alldatav2.mat') 
allFeatures = allData['featuresAll2'] 

# loop over subjects 
n_subject = [0,1,2,3,4,5,6,7,8,9,10,11,12,13] 

fig, axs = plt.subplots(3,5,figsize=(15, 6)) 
plt.xlabel("Number of features selected") 
plt.ylabel("Cross validation score (nb of correct classifications)") 
fig.subplots_adjust() 
axs = axs.ravel() 

for i, j in zip(n_subject, range(15)): 
    #print("For Subject : ", i+1) 
    y = allData['labels'] 
    X = allFeatures[i*120:(i+1)*120,:] 

    svc = SVC(kernel="linear",C=1) 
    rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(2), 
       scoring='accuracy') 
    rfecv.fit(X, y.ravel()) 


    axs[j].plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_) 
plt.show() 


# loop over subjects 
def mean(numbers): 
    return float(sum(numbers))/max(len(numbers), 1) 

n_subject = [0,1,2,3,4,5,6,7,8,9,10,11,12,13] 
avg_scores = [] 

for i in n_subject: 
    print("For Subject : ", i+1) 
    y = allData['labels'] 
    X = allFeatures[i*120:(i+1)*120,:] 

    svc = SVC(kernel="linear",C=1) 
    rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(10), 
       scoring='accuracy') 
    rfecv.fit(X, y.ravel()) 
    print("Optimal number of features : %d" % rfecv.n_features_) 
    print("Ranking of Features : ", rfecv.ranking_) 
    avg_score = rfecv.grid_scores_.max() 
    print("Best CV Score : ", avg_score) 
    avg_scores.append(avg_score) 
    print("------------------------------------------") 
print("Average Accuracy over all Subjects : ", mean(avg_scores)) 

答えて

1

各サブプロットのラベルについては、まずそれらのラベルを含むリストを作成できます。

xlabelList = [xlabel0, xlabel1 ....,xlabel13] 
ylabelList = [ylabel0, ylabel1,....,ylabel13] 

また、ループに余分な変数n_subjectを定義する必要はありません。プロットについては、以下の変更を行います:

for j in range(14): 

    #print("For Subject : ", j+1) 
    y = allData['labels'] 
    X = allFeatures[j*120:(j+1)*120,:] 

    svc = SVC(kernel="linear",C=1) 
    rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(2), 
      scoring='accuracy') 
    rfecv.fit(X, y.ravel()) 

    locInd = np.unravel_index(j, (3,5))  
    axs[locInd].plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_) 
    axs[locInd].set_xlabel(xlabelList[j]) 
    axs[locInd].set_ylabel(ylabelList[j]) 
plt.show() 
+0

ありがとう! xとyのラベルはすべて同じですが、手動で入力せずにこのテキストを14回繰り返してもいいですか?また、軸ラベルを(a、a、a、a、a、a、a、a、a、a、a、a、a、a)のようにテストしようとしましたが、これは無効な構文? – TestGuest

+0

ラベルは文字列でなければなりません。すべてのラベルが同じであれば、axs [locInd] .set_xlabel(xlabel)、リストは不要です。 –

関連する問題