2016-09-23 6 views
0

私は大丈夫ですが、生の部分がうまくいかないようにラベルに接続されていない混乱マトリックスを作成しました。混乱行列の生の部分が一致しません

私は電車やテストのセクションに分割された文字列のいくつかのリストを持っている:

train + test: 
positive: 16 + 4 = 20 
negprivate: 53 + 14 = 67 
negstratified: 893 + 224 = 1117 

混乱行列がテストデータに基づいて構築されています。ここでは

[[ 0 14 0] 
[ 3 220 1] 
[ 0 4 0]] 

はコードです:

my_tags = ['negprivate', 'negstratified', 'positive'] 

def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues): 
    logging.info('plot_confusion_matrix') 
    plt.imshow(cm, interpolation='nearest', cmap=cmap) 
    plt.title(title) 
    plt.colorbar() 
    tick_marks = np.arange(len(my_tags)) 
    target_names = my_tags 
    plt.xticks(tick_marks, target_names, rotation=45) 
    plt.yticks(tick_marks, target_names) 
    plt.tight_layout() 
    plt.ylabel('True label') 
    plt.xlabel('Predicted label') 
    plt.show() 

def evaluate_prediction(target, predictions, taglist, title="Confusion matrix"): 
    logging.info('Evaluate prediction') 
    print('accuracy %s' % accuracy_score(target, predictions)) 
    cm = confusion_matrix(target, predictions) 
    print('confusion matrix\n %s' % cm) 
    print('(row=expected, col=predicted)') 
    print 'rows: \n %s \n %s \n %s ' % (taglist[0], taglist[1], taglist[2]) 

    cm_normalized = cm.astype('float')/cm.sum(axis=1)[:, np.newaxis] 
    plot_confusion_matrix(cm_normalized, title + ' Normalized') 

...

test_targets, test_regressors = zip(
    *[(doc.tags[0], doc2vec_model.infer_vector(doc.words, steps=20)) for doc in alltest]) 
logreg = linear_model.LogisticRegression(n_jobs=1, C=1e5) 
logreg = logreg.fit(train_regressors, train_targets) 
evaluate_prediction(test_targets, logreg.predict(test_regressors), my_tags, title=str(doc2vec_model)) 

しかし、実際には、結果として得られる行列の数値を見て、お互いに一致するようにmy_tagsの順序を変更する必要があります。そして、私が理解する限り、これは何らかの自動的な方法で行われるべきです。 どちらが、私は不思議ですか?

答えて

0

整数のクラスラベルを持つことが常にベストです。すべてが少しスムーズに実行されるようです。あなたはこれらだから今、あなたの新しいタグとして[0 1 2]を持つことになりますLabelEncoder、すなわち

from sklearn import preprocessing 
my_tags = ['negprivate', 'negstratified', 'positive'] 
le = preprocessing.LabelEncoder() 
new_tags = le.fit_transform(my_tags) 

を使用して得ることができます。私は= test_regressors、それはlinetest_targetsで起こると仮定し

'negprivate' 
+0

ありがとう、私はLabelEncoderについて聞いたことがない、私はそれを試してみましょう。 – Talka

0

ラベルのソート順、つまり出力がnp.unique(target)だと思います。

+0

:あなたはあなたのプロットを行うと、あなたはあなたのラベルが直感的になりたいので、あなたはすなわち

le.inverse_transform(0) 

出力、あなたのラベルを取得するためにinverse_transformを使用することができますl – Talka

+0

最初の文にこのラベルよりも「negstratified」というラベルが付いている場合、つまり、このラベルよりもzip(*([行列の中で最初に生かされ、等々。私はコードを使って注文を操作する方法をキャッチすることはできません。 – Talka

関連する問題