如何强制 sklearn 的 CountVectorizer 不移除特殊字符(例如 #, @, , $ 或 %)

这是我的代码:

count = CountVectorizer(lowercase = False)vocabulary = count.fit_transform([words])print(count.get_feature_names())

例如,如果:

 words = "Hello @friend, this is a good day. #good."

我希望它被分隔成这样:

['Hello', '@friend', 'this', 'is', 'a', 'good', 'day', '#good']

目前,它被分隔成这样:

['Hello', 'friend', 'this', 'is', 'a', 'good', 'day']

回答:

你可以使用 CountVectorizer 中的 token_pattern 参数,如文档中所述:

传递一个正则表达式来告诉 CountVectorizer 什么应该被视为一个词。假设在这种情况下,我们告诉 CountVectorizer,即使包含 #@ 的词也应该被视为一个词。然后这样做:

count = CountVectorizer(lowercase = False, token_pattern = '[a-zA-Z0-9$&+,:;=?@#|<>.^*()%!-]+')

输出:

['#good', '@friend', 'Hello', 'a', 'day', 'good', 'is', 'this']

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注