機械学習とscikitの新機能です。私はどのようにシコースを使って10倍のクルーズバリデーションでconfusin matrixを計算できるのか知りたい。 y_testとy_predを見つけるにはどうすればいいですか? http://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html:scikitでの10倍交差検証のための混同行列
2
A
答えて
2
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float')/cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max()/2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
from sklearn import datasets
from sklearn.cross_validation import cross_val_score
from sklearn import svm
from sklearn.metrics import confusion_matrix
import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn import cross_validation
iris = datasets.load_iris()
class_names = iris.target_names
# shape of data is 150
cv = cross_validation.KFold(150, n_folds=10,shuffle=False,random_state=None)
for train_index, test_index in cv:
X_tr, X_tes = iris.data[train_index], iris.data[test_index]
y_tr, y_tes = iris.target[train_index],iris.target[test_index]
clf = svm.SVC(kernel='linear', C=1).fit(X_tr, y_tr)
y_pred=clf.predict(X_tes)
cnf_matrix = confusion_matrix(y_tes, y_pred)
np.set_printoptions(precision=2)
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names,
title='Confusion matrix, without normalization')
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
title='Normalized confusion matrix')
plt.show()
関連する問題
- 1. LibSVMで10倍交差検証を実装する方法
- 2. dplyrのk倍交差検証?
- 3. GLMMのk倍交差検証
- 4. なぜ10倍交差検証のためにWekaは11回学習アルゴリズムを実行しますか?
- 5. Scikit-learnのKNNクラシファイアでグリッド検索パラメータと交差検証されたデータセット
- 6. Rのgbmの欠落データと層別k倍交差検証
- 7. scikit-learnで10×10倍のクロスバリデーション?
- 8. Ridge回帰の100倍交差検証R
- 9. キャレットを使用したバランスのとれたk倍交差検証R
- 10. 交差検証分類エラー
- 11. キャレットパッケージ:列車機能の階層化交差検証
- 12. 私のGLMのn倍交差検証中に `predict`エラーが発生しました
- 13. 2行の交差のための関数の設計
- 14. 検索交差点
- 15. K倍検証のための分割RDD:pyspark
- 16. 列交差点
- 17. Linq配列との交差
- 18. Ruby配列の交差
- 19. マクロと配列の交差
- 20. cでの時差交差#
- 21. VB.NET配列交差
- 22. Scikit - 複数の混乱行列を作成するためのしきい値の変更
- 23. 列の見出しを取得するために、見出し行とターゲットセルの列のVba交差点
- 24. 2クラスの分類子のための混同行列を作成する
- 25. scikit-learnでのクラスタリングのハイパーパラメータ評価のためのグリッド検索
- 26. セットの交差
- 27. 2つのx配列データセットの交差
- 28. 配列からの矩形の交差
- 29. 単一列のLINQとの交差
- 30. leetcode 349:2つの配列の交差
あなたはsklearnで最初sklearn.pipeline.Pipelineメソッドを使用する必要があります。次に、skフォルン・クロス・バリデーションからKFoldをインポートする必要があります。混乱マトリックスについては、これを見ることができます:http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html。素敵な機能の中でこれらの3つのステップをすべて組み合わせてください。 – PJay
こんにちはPジェイ。サンプルコードを参考にしてください。 –