【问题标题】:NLP text classification CountVectorizer Shape ErrorNLP 文本分类 CountVectorizer 形状错误
【发布时间】:2022-01-15 19:48:23
【问题描述】:

我有一个文本数据集,其中有一列用于评论,另一列用于标签。我想通过使用该数据集来构建决策树模型,我使用了矢量化器,但它给出了ValueError: Number of labels=37500 does not match number of samples=1 错误。 vect.vocabulary_ returns {'review': 0}review 是列名。所以我认为它并不适合所有数据。这是下面的代码,任何帮助表示赞赏。

from sklearn.model_selection import train_test_split
X_train, X_test,y_train, y_test = train_test_split(data.iloc[:,:-1],data.iloc[:,-1:],
test_size = 0.25, random_state = 42)

from sklearn.feature_extraction.text import CountVectorizer
vect = CountVectorizer()
vect.fit(X_train)
X_train_dtm = vect.transform(X_train)
X_train_dtm = vect.fit_transform(X_train)
X_test_dtm = vect.transform(X_test)

from sklearn.tree import DecisionTreeClassifier 
DTC = DecisionTreeClassifier()
DTC.fit(X_train_dtm, y_train)
y1_pred_class = DTC.predict(X_test_dtm)

X_train_dtm.shape 也是<bound method spmatrix.get_shape of <1x1 sparse matrix of type '<class 'numpy.int64'>' with 1 stored elements in Compressed Sparse Row format>>

【问题讨论】:

    标签: python scikit-learn nlp decision-tree text-classification


    【解决方案1】:

    CountVectorizer 需要一维输入,错误提示您的X_train 是二维的。如果是数据框,则简化为系列;如果是 numpy 数组,请使用 reshaperavel

    【讨论】:

      【解决方案2】:

      当我改变这部分时它起作用了:

      X_train, X_test,y_train, y_test = train_test_split(data['text'], 数据['tag'],test_size = 0.25, random_state = 42)

      【讨论】:

        猜你喜欢
        • 2018-02-12
        • 2020-06-12
        • 2021-06-07
        • 1970-01-01
        • 2017-05-12
        • 2015-06-26
        • 2020-02-27
        • 2021-01-31
        • 2019-01-08
        相关资源
        最近更新 更多