我有一家商店,里面有26597种独特的产品。
用于将产品导入商店的数据如下所示:
{
"description":"AH Uien rood",
"category":"/Aardappel, groente, fruit/Kruiden, uien, knoflook/Uien/",
"brand":"AH"
}, {...}26597种产品中有530种没有brand值。然而,品牌名称存在于描述中。对于上面的示例产品,在"description":"AH Uien rood"中,AH是品牌名称。品牌名称总是描述中的第一个1+词。但是品牌名称的长度和字号各不相同,而且往往在两者之间有空格。因此,我不能简单地从描述中提取第一个单词,并将其指定为产品品牌名称。
我想我会用机器学习来帮助我根据描述和分类来分类产品的品牌名称。
这是我第一次真正体验机器学习,我决定使用ai4r Ruby。它看起来很好,维护得很好,并且有适当的文档化的这里。
对于530种产品,只有13种被分类,其余的则返回错误:
Ai4r::Classifiers::ModelFailureError: There was not enough information during training to do a proper induction for the data element ...我不太明白,用来训练模型的DATA_SET的大小是25266。
这就是我的代码的样子:
require 'json'
require 'open-uri'
require 'csv'
require 'ai4r'
r = JSON.parse(open('http://goo.gl/2IHtVU') {|f| f.read }.force_encoding('UTF-8'))
def extract_categories(product)
a = product['category'].split('/')
a.delete('')
b = []
a.each { |category| b << category.gsub(',', ' -') }
c = b.join(', ')
end
nb = []
r.each {|p| nb << p if p['brand'].nil? }
DATA_LABELS = ["title", "category", "brand"]
DATA_SET = []
r.each {|pnb| DATA_SET << [pnb['description'], extract_categories(pnb), pnb['brand']] unless pnb['brand'].nil? || pnb['category'].nil? }
data_set = Ai4r::Data::DataSet.new(:data_items=>DATA_SET, :data_labels=>DATA_LABELS)
id3 = Ai4r::Classifiers::ID3.new.build(data_set)
classified = []
nb.each do |pnb|
begin
classified << id3.eval([ pnb['description'], extract_categories(pnb) ])
rescue => e
puts 'There was not enough information during training to do a proper induction for the data element, moving on...'
end
end
classified.size
# => 13
# Save DATA_SET to csv
# CSV.open('/data_set.csv','wb', :quote_char => '"', encoding: "UTF-8") do |csv|
# csv << DATA_LABELS
#
# DATA_SET.each do |data|
# csv << [data[0], data[1], data[2]]
# end
# end
#
# => https://gist.github.com/narzero/ba8c521a370326a57a68有什么更好的方法来根据描述来分类产品的品牌名称呢?
发布于 2014-10-18 10:51:28
在这种情况下,我会选择朴素的贝叶斯分类器而不是决策树。它有一块宝石。物料分类器
在下面的代码中,我用gem训练了您的数据集,并对10个随机条目进行了分类。我用描述来训练,而不是分类。看表演怎么样。否则,您可以通过将类别组合到desciption中来包含类别,但在类别令牌前面加上类似cattt的内容,以区分类别标记和描述。
require 'json'
require 'open-uri'
require 'stuff-classifier'
r = JSON.parse(open('data_file.json') {|f| f.read }.force_encoding('UTF-8'))
def extract_categories(product)
a = product['category'].split('/')
a.delete('')
b = []
a.each { |category| b << category.gsub(',', ' -') }
c = b.join(', ')
end
nb = []
r.each {|p| nb << p if p['brand'].nil? }
DATA_LABELS = ["title", "category", "brand"]
DATA_SET = []
r.each {|pnb| DATA_SET << [pnb['description'], extract_categories(pnb), pnb['brand']] unless pnb['brand'].nil? || pnb['category'].nil? }
cls = StuffClassifier::Bayes.new("Prodcut Label")
#train the classifier by feeding it the label and then the features
DATA_SET.each do |record|
begin
cls.train(record[2], record[0])
rescue
end
end
# print 10 random classifications
1.upto(10){
random_entry = DATA_SET.sample[0]
puts "#{random_entry} - Classified as - #{cls.classify(random_entry)}"
}结果:
https://stackoverflow.com/questions/26438047
复制相似问题