我的商店里有26597种独特的产品。
我用来导入产品的数据看起来像这样:
{ "description":"AH Uien rood", "category":"/Aardappel, groente, fruit/Kruiden, uien, knoflook/Uien/", "brand":"AH"}, {...}
在这26597个产品中,有530个产品没有brand
值。然而,品牌名称确实存在于描述中。对于上面的示例产品,在"description":"AH Uien rood"
中,AH
就是品牌名称。品牌名称总是描述中的前一个或多个词。但品牌名称的长度和词数各不相同,且通常中间有空格。因此,我不能简单地从描述中提取第一个词并将其指定为产品的品牌名称。
我考虑使用机器学习来帮助我根据描述和类别对产品品牌名称进行分类。
这是我第一次真正接触机器学习,我决定使用ai4r Ruby gem。它看起来不错,维护良好,并且有适当的文档这里。
对于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(', ')endnb = []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...' endendclassified.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(', ')endnb = []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 featuresDATA_SET.each do |record| begin cls.train(record[2], record[0]) rescue end end# print 10 random classifications1.upto(10){ random_entry = DATA_SET.sample[0] puts "#{random_entry} - Classified as - #{cls.classify(random_entry)}"}
结果:
-
Organix Goodies squeezy banaan, aardbei & zuivel – Classified as – Organix
-
AH Dames hipster elastisch zwart maat M => John Cabot / AH
-
Piramide Sterrenmix fair trade => – Piramide
-
Royal Club Bitter lemon => Royal Club
-
AH Fruitbiscuit yoghurt/ aardbei => AH
-
Toni & Guy Mask reconstruction treatment => Toni & Guy
-
AH Kinder enkelsok wit mt 23-26 => AH
-
Theramed Aardbei junior 6+ jaar => Theramed
-
Arla Bio drinkyoghurt limoen/ munt => Arla
-
AH Rauwkost Amsterdamse ui => AH