- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是Python新手,正在研究建筑行业契约(Contract)文件的多类文本分类。我在代码中实现 n 元语法时遇到问题,这些代码是我通过从不同的在线资源获得帮助而生成的。我想在我的代码中实现一元语法、二元语法和三元语法。在这方面的任何帮助都将受到高度赞赏。
我在代码的 Tfidf 部分尝试了二元组和三元组,但它有效。
df = pd.read_csv('projectdataayes.csv')
df = df[pd.notnull(df['types'])]
my_types = ['Requirement','Non-Requirement']
#converting to lower case
df['description'] = df.description.map(lambda x: x.lower())
#Removing the punctuation
df['description'] = df.description.str.replace('[^\w\s]', '')
#splitting the word into tokens
df['description'] = df['description'].apply(tokenize.word_tokenize)
#stemming
stemmer = PorterStemmer()
df['description'] = df['description'].apply(lambda x: [stemmer.stem(y) for y in x])
print(df[:10])
## This converts the list of words into space-separated strings
df['description'] = df['description'].apply(lambda x: ' '.join(x))
count_vect = CountVectorizer()
counts = count_vect.fit_transform(df['description'])
X_train, X_test, y_train, y_test = train_test_split(counts, df['types'], test_size=0.3, random_state=39)
tfidf_vect_ngram = TfidfVectorizer(analyzer='word',
token_pattern=r'\w{1,}', ngram_range=(2,3), max_features=5000)
tfidf_vect_ngram.fit(df['description'])
X_train_Tfidf = tfidf_vect_ngram.transform(X_train)
X_test_Tfidf = tfidf_vect_ngram.transform(X_test)
model = MultinomialNB().fit(X_train, y_train)
文件“C:\Users\fhassan\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py”,第 328 行,位于 tokenize(预处理(self.decode(doc))), stop_words)
文件“C:\Users\fhassan\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py”,第 256 行,位于 返回 lambda x: strip_accents(x.lower())
文件“C:\Users\fhassan\anaconda3\lib\site-packages\scipy\sparse\base.py”,第 686 行,getattr 引发 AttributeError(attr + "未找到")
属性错误:未找到下层
最佳答案
首先,您在文本上安装矢量化器:
tfidf_vect_ngram.fit(df['description'])
然后尝试将其应用于计数:
counts = count_vect.fit_transform(df['description'])
X_train, X_test, y_train, y_test = train_test_split(counts, df['types'], test_size=0.3, random_state=39)
tfidf_vect_ngram.transform(X_train)
您需要将矢量化器应用于文本,而不是计数:
X_train, X_test, y_train, y_test = train_test_split(df['description'], df['types'], test_size=0.3, random_state=39)
tfidf_vect_ngram.transform(X_train)
关于python - 在Python代码中实现多类文本分类的n-gram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555159/
我正在阅读有关 n 元语法的内容,我想知道在实践中是否存在优先使用单元语法而不是双元语法的情况(或更高的N-grams)。据我了解,N越大,计算概率和建立向量空间的复杂性就越大。但除此之外,还有其他原
我一直在为我的一个新项目使用 ElasticSearch。我已将默认分析器设置为使用 ngram 标记过滤器。这是我的 elasticsearch.yml 文件: index: analysis:
我正在做一个项目,我需要一个 skip-gram 模型的预训练向量。我听说还有一个名为 skip-n-gram 模型的变体可以提供更好的结果。 我想知道自己训练模型需要什么?因为我只需要它们来为我的模
我正在尝试使用 Lucene (5.5.0) 进行一些字符串标记化(无索引)。我需要: 完全删除包含数字的单词,例如像 log4j 这样的单词应该从字符串中删除 我想将字符串拆分为单字术语和 2 克术
我正在尝试在 Scala 中编写基于 n-gram 的分离新闻算法。 如何为大文件生成 n-gram: 例如,对于包含“蜜蜂是蜜蜂的蜜蜂”的文件。 首先它必须选择一个随机的 n-gram。例如,蜜蜂。
给定一个大文本文件,我想使用 Apache Spark 提取字符 n-gram(并行执行任务)。 示例输入(2 行文本): 第 1 行:(Hello World,它) 第 2 行:(是美好的一天) 输
对我以前的post进行了更新,并做了一些更改: 说我有100条微博。 在这些推特中,我需要提取:1)食品名称,2)饮料名称。我还需要附上类型(饮料或食品)和一个id号(每个项目有一个唯一的id)为每个
我是Python新手,正在研究建筑行业契约(Contract)文件的多类文本分类。我在代码中实现 n 元语法时遇到问题,这些代码是我通过从不同的在线资源获得帮助而生成的。我想在我的代码中实现一元语法、
Gram-Schmidt 正交化算法的计算复杂度是多少? 假设一个m 行和k 列的矩阵,计算正交化需要多少次操作? 如果可能的话,我想要乘法和加法的确切数量。 编辑:在我看来,操作总数(乘法 + 加法
在Keras中可以使用n-gram吗? 例如,句子包含在X_train数据框中,带有“句子”列。 我通过以下方式使用Keras的 token 生成器: tokenizer = Tokenizer(lo
给定一个具有独立列的矩阵 A(不一定是正方形),我能够应用 Gram-Schmidt 迭代并使用 Matlab 的函数 qr 为其列空间(以正交矩阵 Q 的形式)生成正交基 A=[1,1;1,0;1,
我有一个 n-gram 列表 ('allo', 'stesso', 'modo', 'dell’italia,', 'che') ('stesso', 'modo', 'dell’italia,',
我有一个已经被标记化的文档列表: dat result[[2]] [1] "argentine" "crude" "oil"
使用什么算法来查找 ngram? 假设我的输入数据是一个单词数组和我想要查找的 ngram 的大小,我应该使用什么算法? 我要求提供代码,优先选择 R。数据存储在数据库中,因此也可以是 plgpsql
我有一个使用关键字 token 生成器(整个术语是 token )的字段类型,具有小写和边缘n-gram过滤器,例如“Robert Longo”存储为“r”,“ro”,...,“robert l”,“
我正在尝试使用三元组进行下一个单词预测。 我已经能够上传一个语料库并通过它们的频率识别最常见的三元组。我在 R 中使用了“ngrams”、“RWeka”和“tm”包。我按照这个问题寻求指导: What
我正在研究一个增强和替代通信 (AAC) 程序。我当前的目标是存储输入/口语文本的历史记录并搜索常用短语片段或单词 n-gram。我目前使用的是基于 lzw 压缩算法的实现,如 CodeProject
我正在构建一个基于 OpenGL3 的免费开源 3D 游戏引擎(这不是学校作业,而是为了个人技能发展和回馈开源社区)。我已经到了需要学习大量相关数学的阶段,所以我正在阅读一本很棒的教科书,名为“3D
我试图从从文本文档中提取的模式中提取 ngram,模式具有不同数量的术语。 例如:如果模式 p={t1,t2,t3} 我们需要提取 ngram 3 应该是这样的 t1 t2 t3 t1, t2 t2,
我正在编写一个 C 程序,以便在某个字符串中找到最频繁的 n-gram。 一个n-gram是一个 contiguous sequence of n items from a given sequenc
我是一名优秀的程序员,十分优秀!