python - CountVectorizer 假设将数据集转换为稀疏矩阵,但它没有这样做
问题描述
我正在努力学习 NLP。所以首先我创建了一个函数,它可以从字符串中提取非停用词并创建这些词的列表。我已经事先从 nltk 下载 shell 下载了停用词列表
import pandas as pd
msg=pd.read_csv('SMSSpamCollection',sep='\t',names=['label','message'])
msg['msg_len']=msg.message.apply(len)
from nltk.corpus import stopwords
import string
def cleantext(text):
nopunc=[c for c in text if c not in string.punctuation]
nopunc=''.join(nopunc)
clean_text=[w for w in nopunc.split() if w.lower()
not in stopwords.words('english')]
return clean_text
该功能工作正常。然后应用 CountVectorizer 以从单词列表 clean_text 中创建稀疏矩阵
from sklearn.feature_extraction.text import CountVectorizer
bow=CountVectorizer(analyzer=cleantext).fit(msg['message'])
print(len(bow))
当尝试使用 len() 打印长度时,它给了我一个错误 TypeError: object of type 'CountVectorizer' has no len()
所以我尝试打印它,看看它到底是什么,我把它作为我的输出 - CountVectorizer(analyzer=<function cleantext at 0x0000026F6D736280>)
这里有什么问题?
解决方案
推荐阅读
- python - 使用python从PDF文件中提取相关文本
- excel - SUMIF 匹配文本添加值
- php - 如何在WordPress中获取特定页面的文档(seo)标题
- python - 当需要来自一列的值来创建另一列的值时,使用 pandas
- c# - 我想在 C# 中使用 char 数组输出 ASCII
- c# - 角色自动移动时冻结
- react-native - 如何在我的 Expo / React Native 项目中禁用发送到 http://127.0.0.1:19000/logs 的日志请求?
- azure - 如何从使用 Azure Spark 创建的 parquet 文件在 Azure ML Studio(通过 GUI)中创建 Azure 数据集
- vue.js - vue multiselect 在更新 v-model 时失去反应
- docker - docker:来自守护进程的错误响应:获取 https://registry-1.docker.io/v2/:proxyconnect tcp:EOF