Вычислить yScore алгоритма обучения - PullRequest
0 голосов
/ 01 ноября 2019

Я довольно новичок в среде Python ML, мне нужно построить график точности / отзыва, как указано в этом посте: [https://scikit -learn.org / stable / auto_examples / model_selection / plot_precision_recall. html] [1] вам нужно вычислить y_score:

    # Create a simple classifier
classifier = svm.LinearSVC(random_state=random_state)
classifier.fit(X_train, y_train)
y_score = classifier.decision_function(X_test)

Итак, вопрос: как я могу вычислить счет, используя Multinomial NaiveBayes или LearningTree? В моем коде у меня есть:

 print("MultinomialNB - countVectorizer")

    xTrain, xTest, yTrain, yTest=countVectorizer(db)

    classifier = MultinomialNB()
    model = classifier.fit(xTrain, yTrain)
    yPred = model.predict(xTest)

    print("confusion Matrix of MNB/ cVectorizer:\n")
    print(confusion_matrix(yTest, yPred))
    print("\n")
    print("classificationReport Matrix of MNB/ cVectorizer:\n")
    print(classification_report(yTest, yPred))

    elapsed_time = time.time() - start_time
    print("elapsed Time: %.3fs" %elapsed_time) 

Функция графика:

def plotLearningAlgorithm(yTest,yScore,algName):

    precision, recall, _ = precision_recall_curve(yTest, yScore)

    plt.step(recall, precision, color='b', alpha=0.2,
         where='post')
    plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs)

    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.0])
    plt.title('2-class Precision-Recall'+ algName +'curve: AP={0:0.2f}'.format(average_precision))

Ошибка с графиком:

<ipython-input-43-d07c3365bfc2> in MultinomialNaiveBayesOPT()
     11     yPred = model.predict(xTest)
     12 
---> 13     plotLearningAlgorithm(yTest,model.predict_proba(xTest),"MultinomialNB - countVectorizer")
     14 
     15     print("confusion Matrix of MNB/ cVectorizer:\n")

<ipython-input-42-260aac9918f2> in plotLearningAlgorithm(yTest, yScore, algName)
      1 def plotLearningAlgorithm(yTest,yScore,algName):
      2 
----> 3     precision, recall, _ = precision_recall_curve(yTest, yScore)
      4 
      5     step_kwargs = ({'step': 'post'}

/opt/anaconda3/lib/python3.7/site-packages/sklearn/metrics/ranking.py in precision_recall_curve(y_true, probas_pred, pos_label, sample_weight)
    522     fps, tps, thresholds = _binary_clf_curve(y_true, probas_pred,
    523                                              pos_label=pos_label,
--> 524                                              sample_weight=sample_weight)
    525 
    526     precision = tps / (tps + fps)

/opt/anaconda3/lib/python3.7/site-packages/sklearn/metrics/ranking.py in _binary_clf_curve(y_true, y_score, pos_label, sample_weight)
    398     check_consistent_length(y_true, y_score, sample_weight)
    399     y_true = column_or_1d(y_true)
--> 400     y_score = column_or_1d(y_score)
    401     assert_all_finite(y_true)
    402     assert_all_finite(y_score)

/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in column_or_1d(y, warn)
    758         return np.ravel(y)
    759 
--> 760     raise ValueError("bad input shape {0}".format(shape))
    761 
    762 

ValueError: bad input shape (9000, 2)

Где db содержит мой набор данных, уже разделенный между наборами поездови тестовый набор. Любые предложения?

Решение:

def plot_pr(y_pred,y_true,l):
    precision, recall, thresholds = precision_recall_curve(y_true, y_pred,pos_label=l)
    return precision,recall


def plotPrecisionRecall(xTest,yTest,yPred,learningName,model):
    yPred_probability = model.predict_proba(xTest)
    yPred_probability = yPred_probability[:,1];
    no_skill_probs = [0 for _ in range(len(yTest))]
    ns_precision,ns_recall,_=precision_recall_curve(yTest,no_skill_probs,pos_label="L")
    precision, rec= plot_pr(yPred_probability,yTest,"L");
    plt.title(learningName)
    plt.plot(ns_recall,ns_precision,linestyle='--',label='No Skill')
    plt.plot(rec,precision,Label='Skill')
    plt.xlabel("Recall")
    plt.ylabel("Precision")
    plt.legend()
    plt.show()

Так как оказывается, что y_Pred необходимо преобразовать с:

yPred_probability = yPred_probability[:,1];

Такой большойспасибо @ignoring_gravity за предоставление мне правильного решения, я также напечатал строку без навыков для дополнительной читаемости графика.

Ответы [ 2 ]

1 голос
/ 01 ноября 2019

То, что они называют y_score, - это просто предсказанные вероятности, выведенные вашим алгоритмом ML.

В многочлене nb и в дереве решений (я полагаю, это то, что вы подразумеваете под LearningTree?), Вы можете сделать этопо методу .predict_proba:

    classifier = MultinomialNB()
    model = classifier.fit(xTrain, yTrain)
    yPred = model.predict_proba(xTest)
0 голосов
/ 04 ноября 2019
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split

# countvectorizer is not used for train and test split, instead use train_test_split
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.33, random_state=42)  # here x is going to be your textual data, whereas y will be your target 

countMatrix_train = vect.fit_transform(train_x)  # you have to fit with your train data


countMatrix_test =  vect.transform(test_x)  # now have to transform( and not fit_transform) according to your train data


classifier = MultinomialNB()
classifier.fit(countMatrix_train, train_y)


ypred = classifier.predict(countMatrix_test)   # this will give you class for your test data, now use this for making classification report
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...