计算学习算法的yScore

朱利奥·塞拉(Giulio Serra)

我是ML python环境的新手,我需要绘制精度/调用图,如这篇文章所述:[ 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)

所以问题是:如何使用多项式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为我提供正确的解决方案,我还打印了无技巧行,以使图形更加可读。

ignoring_gravity

他们所谓y_score的只是ML算法输出的预测概率。

在多项式nb和决策树中(我想这就是LearningTree的意思吗?),您可以使用以下方法执行此操作.predict_proba

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Keras Adam优化器学习率超参数与网络参数的单独计算学习率有何关系?

来自分类Dev

计算学校总人数

来自分类Dev

使用php计算学年

来自分类Dev

如何使用php中的循环计算学习同一分支,年份,学期的学生

来自分类Dev

C:用于计算学生的GPA的程序

来自分类Dev

计算学校收取的总费用

来自分类Dev

计算学生成绩的Python程序

来自分类Dev

学习如何设计算法的资源,例如Nest Thermostat使用哪种算法?

来自分类Dev

学习如何设计算法的资源,例如Nest Thermostat使用哪种算法?

来自分类Dev

Python计算学生统计数据

来自分类Dev

计算学生同龄人的平均成绩

来自分类Dev

Q学习算法问题

来自分类Dev

Adaline学习算法

来自分类Dev

选择监督学习算法或无监督学习算法

来自分类Dev

深度学习涉及哪些算法?

来自分类Dev

Scikit学习算法表现极差

来自分类Dev

感知器学习算法

来自分类Dev

指定执行自动学习的算法

来自分类Dev

计算学生化的删除残差和outlier_test()以预测OLS

来自分类Dev

R:R脚本可根据字母等级计算学生的GPA

来自分类Dev

SQL Server-计算学生GPA并将其插入表变量

来自分类Dev

我正在使用 MYSQL 来计算学生表中每个科目的 TOP-N。

来自分类Dev

如何解决计算学生水平等级(例如 11 年级)的问题

来自分类Dev

计算法线向量

来自分类Dev

Kosaraju算法-计算SCC

来自分类Dev

计算器算法

来自分类Dev

矩阵计算的高效算法

来自分类Dev

计算法线向量

来自分类Dev

Q学习算法的实现是递归的吗?