在使用train_test_split之前和之后应用SMOTETomek时的不同得分

我正在尝试将文本分类到6个不同的类别。由于我的数据集不平衡,我还使用了SMOTETomek方法,该方法应该通过添加人工样本来合成平衡数据集。

我注意到在通过管道应用它与“逐步”应用之间的巨大得分差异,唯一的区别是(我认为)我使用train_test_split的位置

这是我的特征和标签:

for curr_features, label in self.training_data:    features.append(curr_features)    labels.append(label)algorithms = [    linear_model.SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, random_state=42, max_iter=5, tol=None),    naive_bayes.MultinomialNB(),    naive_bayes.BernoulliNB(),    tree.DecisionTreeClassifier(max_depth=1000),    tree.ExtraTreeClassifier(),    ensemble.ExtraTreesClassifier(),    svm.LinearSVC(),    neighbors.NearestCentroid(),    ensemble.RandomForestClassifier(),    linear_model.RidgeClassifier(),]

使用管道:

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)# Provide Report for all algorithmsscore_dict = {}for algorithm in algorithms:    model = Pipeline([        ('vect', CountVectorizer()),        ('tfidf', TfidfTransformer()),        ('smote', SMOTETomek()),        ('classifier', algorithm)    ])    model.fit(X_train, y_train)    # Score    score = model.score(X_test, y_test)    score_dict[model] = int(score * 100)sorted_score_dict = {k: v for k, v in sorted(score_dict.items(), key=lambda item: item[1])}for classifier, score in sorted_score_dict.items():    print(f'{classifier.__class__.__name__}: score is {score}%')

使用逐步方法:

vectorizer = CountVectorizer()transformer = TfidfTransformer()cv = vectorizer.fit_transform(features)text_tf = transformer.fit_transform(cv).toarray()smt = SMOTETomek()X_smt, y_smt = smt.fit_resample(text_tf, labels)X_train, X_test, y_train, y_test = train_test_split(X_smt, y_smt, test_size=0.2, random_state=0)self.test_classifiers(X_train, X_test, y_train, y_test, algorithms)def test_classifiers(self, X_train, X_test, y_train, y_test, classifiers_list):    score_dict = {}    for model in classifiers_list:        model.fit(X_train, y_train)        # Score        score = model.score(X_test, y_test)        score_dict[model] = int(score * 100)           print()    print("SCORE:")    sorted_score_dict = {k: v for k, v in sorted(score_dict.items(), key=lambda item: item[1])}    for model, score in sorted_score_dict.items():        print(f'{model.__class__.__name__}: score is {score}%')

我得到的最佳分类器模型的得分,使用管道大约是65%,使用逐步方法大约是90%。不确定我错过了什么。


回答:

在这种情况下,正确的方法在数据科学SE线程的我的答案中详细描述:为什么你不应该在交叉验证前上采样(虽然答案是关于CV的,但对于train/test分割的情况,原理是相同的)。简而言之,任何重采样方法(包括SMOTE)都应该只应用于训练数据,而不应用于验证或测试数据。

鉴于此,你在这里的管道方法是正确的:你在分割后只对训练数据应用SMOTE,并且根据imblearn管道的文档:

采样器仅在fit期间应用。

因此,在model.score期间实际上没有对测试数据应用SMOTE,这正是应该的做法。

另一方面,你的逐步方法在许多层面上都是错误的,SMOTE只是其中之一;所有这些预处理步骤都应该在train/test分割之后应用,并且仅在数据的训练部分上进行拟合,这在这里并非如此,因此结果是无效的(难怪它们看起来“更好”)。关于如何以及为什么这样的预处理步骤应该只应用于训练数据的一般讨论(和实际演示),请参见我在特征选择应该在训练-测试分割之前还是之后进行?中的(2)个答案(再次强调,那里的讨论是关于特征选择的,但它也适用于像计数向量化和TF-IDF转换这样的特征工程任务)。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注