【问题标题】:Classify strings via class using sklearn使用 sklearn 通过类对字符串进行分类
【发布时间】:2016-10-27 05:35:54
【问题描述】:

我在以 sklearn 可以接受的形式呈现我的数据时遇到问题 我的原始数据是几百个字符串,它们被分为 5 个类别之一,我有一个我想分类的字符串列表,以及它们各自类别的平行列表。我正在使用GaussianNB()

示例数据:

For such a large, successful business, I really feel like they need to be 
either choosier in their employee selection or teach their employees to 
better serve their customers.|||Class:4

代表一个给定的“特征”和一个分类

在分类器中使用之前,字符串本身必须转换为向量,我尝试使用DictVector 来执行此任务

dictionaryTraining = convertListToSentence(data)
vec = DictVectorizer()
print(dictionaryTraining)
vec.fit_transform(dictionaryTraining)

但是为了做到这一点,我必须将数据的实际分类附加到字典中,否则我会收到错误 'str' object has no attribute 'items' 我理解这是因为 .fit_transform 需要特征和索引,但我不完全了解索引的目的

fit_transform(X[, y])   Learn a list of feature name -> indices mappings and transform X.

我的问题是,我如何获取字符串列表和表示其分类的数字列表,并将它们提供给gaussianNB() 分类器,以便我将来可以使用类似的字符串呈现它,它会估计字符串类?

【问题讨论】:

  • 你能分享dictionaryTraining 变量中数据的格式吗?
  • 除非我将格式设置为字符串 -> 分类,否则该格式不起作用,但显然这不是分类器所需要的

标签: python string scipy scikit-learn gaussian


【解决方案1】:

由于您的输入数据是原始文本格式,而不是像 {"word":number_of_occurrences, } 这样的字典格式,我相信您应该使用CountVectorizer,它将您的输入文本拆分为白色空间并将其转换为您需要的输入向量。

这种转换的一个简单示例是:

from sklearn.feature_extraction.text import CountVectorizer
corpus = ['This is the first document.', 'This is the second second document.', 
          'And the third one.', 'Is this the first document?',]
x = CountVectorizer().fit_transform(corpus)
print x.todense() #x holds your features. Here I am only vizualizing it 

【讨论】:

  • 你有一个例子,说明我将如何获取字符串列表和字符串分类的并行列表,将它们转换为 countvectorisor 并输入到分类器?
猜你喜欢
  • 1970-01-01
  • 2014-06-11
  • 2018-12-06
  • 2015-11-20
  • 2022-01-01
  • 2018-01-07
  • 2016-05-09
  • 2017-11-22
  • 1970-01-01
相关资源
最近更新 更多