【问题标题】:how to check if the model in ML is good for the dataset when you apply the model on real data?当您将模型应用于真实数据时,如何检查 ML 中的模型是否适合数据集?
【发布时间】:2020-11-12 14:43:04
【问题描述】:

我有一个 python 脚本,可以对文本进行正面或负面的分类。 我有一个数据集,在对得到的文本进行预处理后,我将其拆分为训练和测试数据

  • 91% 的训练数据准确率
  • 87% 的测试数据准确率

当我尝试使用真实数据时,它给出 20% 的准确度错误在哪里??

训练数据

Accuracy: 91.459%
Best parameters set found on development set:

{'bow__ngram_range': (1, 2), 'tfidf__use_idf': True}

Optimized model achieved an ROC of:  0.9998

测试数据

accuracy score:  0.8704919797610077


confusion matrix: 
 [[3920  699]
 [ 504 4166]]


              precision    recall  f1-score   support

           0       0.89      0.85      0.87      4619
           1       0.86      0.89      0.87      4670

   micro avg       0.87      0.87      0.87      9289
   macro avg       0.87      0.87      0.87      9289
weighted avg       0.87      0.87      0.87      9289

我使用 Logistic Regression 作为 ML 模型,并使用 TfIdf交叉验证

from sklearn.model_selection import GridSearchCV
from sklearn import metrics
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.model_selection import KFold
from sklearn.feature_extraction.text import CountVectorizer,TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn import model_selection

from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
    cross_val = KFold(n_splits=3, random_state=42)
    # create pipeline
    pipeline = Pipeline([
        ('bow', CountVectorizer(strip_accents='ascii',
                                stop_words=['english'],
                                lowercase=True)),  # strings to token integer counts
        ('tfidf', TfidfTransformer()),  # integer counts to weighted TF-IDF scores
        ('classifier', LogisticRegression(C=15.075475376884423,penalty="l2")),  
    ])
    
    parameters = {'bow__ngram_range': [(1, 1), (1, 2)],
                  'tfidf__use_idf': (True, False),
                    
                 }
    
       clf = GridSearchCV(pipeline, param_grid=parameters, cv=cross_val, verbose=1, n_jobs=-1, scoring= 'roc_auc')
    clf.fit(x_train, y_train)

【问题讨论】:

    标签: python scikit-learn nltk sentiment-analysis text-classification


    【解决方案1】:

    检查模型好坏的情况不止一种。我的一堆理由/建议:

    1. 数据在训练集和测试集中的分布
    2. 如果数据分布不平衡,树模型可以提供更好的结果。
    3. 实际数据和训练+测试数据应该在相同的分布中(至少相似,否则就像:你训练猫并检查狗)
    4. 训练和测试的准确度差异通常会提示模型的性能
    5. 如果您的训练数据集太小,也可能导致此问题 结果(实际 %20acc)。您可以应用数据增强并尝试 再次。

    我猜,你的实际数据与训练/测试集不相似。

    【讨论】:

    • 好的,让我们来看看每一点。 1.训练数据 80% 测试数据 20% 2. 数据在正负标签之间平衡 5. 数据集大约有 46000 条记录 我认为您的猜测是正确的,但是如何获得与实际数据相似的数据集?
    • 您需要对训练/测试数据使用相同的来源。如果您的实际数据来自 twitter,请使用 twitter 数据。实际数据:一种特殊的学术出版物,使用来自同一学术领域的数据。否则,使用 ML 方法很难解决这个问题。或者,您可以从不同类型的来源收集更多数据并使用 DL 模型。 (ML 模型不适用于大型数据集)
    • 我在训练和测试中使用 twitter 数据集,而真实数据是 twitter 数据,为什么我的模型中有这个差距?
    • 可能是语言差异,训练/测试数据可能太旧。 Twitter 数据也可以有很多内部话题、讽刺、文字错别字。您应该检查数据中的样本并尝试用眼睛检测。如果你在阅读时能清楚地说出正面和负面的推文,ml 可以找到它。
    • 语言是英文---训练数据不是太旧它包括讽刺和错别字但我对文本进行了预处理,讽刺文本的数量不是很大,所以我认为准确度一定很高超过 20%
    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 2021-07-20
    • 2018-03-02
    • 2020-05-03
    • 1970-01-01
    • 2021-12-26
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多