Sklearn(NLP 文本分类器新手)- 形状和矢量化器问题,X 和 Y 不匹配
Sklearn (NLP text classifier newbie) - issue with shape and vectorizer, X and Y not matching up
我想创建一个文本分类器,根据我拥有的标记数据集,查看研究摘要并确定它们是否专注于获得护理。数据源是一个 Excel 电子表格,包含三个字段(project_number、摘要和 accessclass)和 326 行摘要。 accessclass 为 1 表示访问相关,0 表示不相关(不确定这是否相关)。无论如何,我尝试按照教程进行操作,想通过添加我自己的数据来使其相关,但我的 X 和 Y 数组存在一些问题。任何帮助表示赞赏。
import pandas as pd
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn import naive_bayes
from sklearn.metrics import roc_auc_score
df = pd.read_excel("accessclasses.xlsx")
df.head()
#TFIDF vectorizer
stopset = set(stopwords.words('english'))
vectorizer = TfidfVectorizer(use_idf=True, lowercase=True,
strip_accents='ascii', stop_words=stopset)
y = df.accessclass
x = vectorizer.fit_transform(df)
print(x.shape)
print(y.shape)
#above and below seem to be where the issue is.
x_train, x_test, y_train, y_test = train_test_split(x, y)
您正在使用整个数据帧对预测变量进行编码。转换时记得只用摘要(也可以先把语料词典拟合一下,然后再转换)。
这是一个解决方案:
y = df.accessclass
x = vectorizer.fit_transform(df.abstract)
其他看起来还可以。
我想创建一个文本分类器,根据我拥有的标记数据集,查看研究摘要并确定它们是否专注于获得护理。数据源是一个 Excel 电子表格,包含三个字段(project_number、摘要和 accessclass)和 326 行摘要。 accessclass 为 1 表示访问相关,0 表示不相关(不确定这是否相关)。无论如何,我尝试按照教程进行操作,想通过添加我自己的数据来使其相关,但我的 X 和 Y 数组存在一些问题。任何帮助表示赞赏。
import pandas as pd
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn import naive_bayes
from sklearn.metrics import roc_auc_score
df = pd.read_excel("accessclasses.xlsx")
df.head()
#TFIDF vectorizer
stopset = set(stopwords.words('english'))
vectorizer = TfidfVectorizer(use_idf=True, lowercase=True,
strip_accents='ascii', stop_words=stopset)
y = df.accessclass
x = vectorizer.fit_transform(df)
print(x.shape)
print(y.shape)
#above and below seem to be where the issue is.
x_train, x_test, y_train, y_test = train_test_split(x, y)
您正在使用整个数据帧对预测变量进行编码。转换时记得只用摘要(也可以先把语料词典拟合一下,然后再转换)。
这是一个解决方案:
y = df.accessclass
x = vectorizer.fit_transform(df.abstract)
其他看起来还可以。