我有一家商店,里面有 26597 种独特的产品。
我用来将产品导入商店的数据如下所示:
{
"description":"AH Uien rood",
"category":"/Aardappel, groente, fruit/Kruiden, uien, knoflook/Uien/",
"brand":"AH"
}, {...}
26597 种产品中有 530 种不具有品牌
值(value)。然而,品牌名称出现在描述中。对于上面的示例产品,在 "description":"AH Uien rood"
中,AH
是 的品牌名称。品牌名称始终是描述中的前 1 个以上单词。但品牌名称的长度和字号各不相同,并且之间通常有空格。因此,我不能简单地从描述中提取第一个单词并将其指定为产品品牌名称。
我想我应该使用机器学习来帮助我根据描述和类别对产品品牌名称进行分类。
这是我第一次真正体验机器学习,我决定使用 ai4r ruby 。它看起来不错,维护良好且记录正确 here .
对于 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
根据描述对产品品牌名称进行分类的更好方法是什么?
最佳答案
在这种情况下,我会选择朴素贝叶斯分类器而不是决策树。它有一颗 gem 。 stuff-classifier
在下面的代码中,我使用 gem 训练了您的数据集,并对 10 个随机条目进行了分类。我使用描述进行训练,而不是类别。看看表现如何。否则,您可以通过将类别组合到描述中来包含类别,但在类别标记前面添加类似 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)}"
}
结果:
Organix Goodies sceezy banaan、aardbei 和 zuivel - 分类为 - Organix
AH Dames 时尚弹性 zwart maat M => John Cabot/AH
Piramide Sterrenmix 公平贸易 => - Piramide
皇家具乐部苦柠檬 => 皇家具乐部
AH Fruitbiscuit 酸奶/aardbei => AH
Toni & Guy 面膜重建治疗 => Toni & Guy
AH Kinder enkelsok wit mt 23-26 => 啊
Theramed Aardbei Junior 6+ jaar => Theramed
Arla Bio Drinkyoghurt limoen/munt => Arla
AH RauwkostAmsterdamse ui => AH
关于ruby - 如何使用机器学习对产品的品牌名称进行分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26438047/