【发布时间】: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