首页 文章

CountVectorizer矩阵随新的测试数据而变化以进行分类?

提问于
浏览
1

我已经使用python创建了一个文本分类模型 . 我有CountVectorizer,它产生一个2034行和4063列(独特单词)的文档术语矩阵 . 我保存了用于新测试数据的模型 . 我的新测试数据

test_data = ['Love', 'python', 'every','time']

但问题是我将上述测试数据标记转换为特征向量,但形状不同 . 因为该模型期望4063向量 . 我知道如何通过使用CountVectorizer的词汇表来解决它,并在测试数据中搜索每个标记并将其放入该索引中 . 但是有没有简单的方法来处理scikit-learn本身的问题 .

3 回答

  • 0

    您不应该在测试数据上使用新的CountVectorizer,您应该使用适合训练数据的那个并在其上调用 transfrom(test_data) .

  • 4

    你有两种方法来解决这个问题

    1. you can use the same CountVectorizer that you used for your train features like this

    cv = CountVectorizer(parameters desired)
    
    X_train = cv.fit_transform(train_data)
    
    X_test = cf.transform(test_data)
    

    2. You can also creat another CountVectorizer, if you really want to(but not advisable since you would be wasting space and you'd still want to use the same parameters for your CV), and use the same feature.

    cv_train = CountVectorizer(parameters desired)
    
    X_train = cv_train.fit_transform(train_data)
    
    cv_test = CountVectorizer(vocabulary=cv_train.get_feature_names(),desired params)
    
    X_test = cv_test.fit_transform(test_data)
    
  • 2

    尝试使用:

    test_features = inverse_transform(test_data)
    

    这应该归还你想要的 .

相关问题