我在易趣上刮了很多类似这样的标题:
Apple iPhone 5 White 16GB Dual-Core
我已经用这种方式手动标记了所有这些
B M C S NA
其中B=品牌(苹果)M=型号(iPhone 5)C=颜色(白色)S=尺寸(大小)NA=未分配(双核)
现在我需要使用python中的libsvm库来训练一个支持向量机分类器,以学习ebay标题中出现的序列模式。
我需要为这些属性(品牌、型号、颜色、尺寸)提取新的值,将问题视为一个分类问题。这样我就可以预测新的模型。
我想考虑以下特点:
* Position
- from the beginning of the title
- to the end of the listing
* Orthographic features
- current word contains a digit
- current word is capitalized
....
我不明白我怎么能把这些信息都给图书馆。官方文件缺少很多信息
我的班级是品牌、型号、尺码、颜色、不适用
SVM算法的输入文件必须包含什么?
如何创建它?我能举一个例子来说明这个文件吗?考虑到我在问题中举例说明的4个特性?我也可以有一个代码的例子,我必须用来详细说明输入文件吗?
*更新* 我想代表这些特征。。。我该怎么办?
我想我可以这样解释
0 --> Brand
1 --> Model
2 --> Color
3 --> Size
4 --> NA
如果我知道这个词是一个品牌,我会把这个变量设为1(真)。 在训练测试中这样做是可以的(因为我已经标记了所有的单词),但是我怎样才能在测试集上这样做呢?我不知道一个词的类别是什么(这就是我学习这个词的原因:D)。
当前单词的N-克子串特征(N=4,5,6) 不知道,这是什么意思?
当前单词前两个单词的同一性。 如何对该功能建模?
考虑到我为第一个特性创建的图例,我有5^(5)个组合)
00 10 20 30 40
01 11 21 31 41
02 12 22 32 42
03 13 23 33 43
04 14 24 34 44
如何将其转换为libsvm(或scikit learn)可以理解的格式?
再说一遍,我该怎么做? 有4个字典(颜色、大小、型号和品牌),我想我必须创建一个bool变量,只有在4个字典中有一个与当前单词匹配时,我才会将其设置为true。
我觉得这和第四部一样。特性我必须使用bool变量。你同意吗?
我赞同@MarcoPashkov的评论,但我将尝试详细说明LibSVM文件格式。我发现文档很全面但很难找到,对于Python库,我建议使用README on GitHub。
需要注意的一点是,存在一种稀疏格式,其中0的所有特征都将被删除,而密集格式中0的特征不会被删除。这两个都是自述文件中的等效示例。
y
变量存储数据的所有类别的列表。x
变量存储特征向量。assert len(y) == len(x), "Both lists should be the same length"
在Heart Scale Example中找到的格式是一种稀疏格式,其中dictionary键是特征索引,dictionary值是特征值,而第一个值是类别。
稀疏格式在为特征向量使用Bag of Words Representation时非常有用。
作为一个使用你开始使用的特征向量的例子,我训练了一个基本的LibSVM 3.20模型。这段代码并不打算使用,但可能有助于展示如何创建和测试模型。
我希望这个例子有用,它不应该用于你的训练。它之所以被当作一个例子,仅仅是因为它效率低下。
下面是一个分步指导,教你如何使用数据训练支持向量机,然后使用相同的数据集进行评估。也可以在http://nbviewer.ipython.org/gist/anonymous/2cf3b993aab10bf26d5f找到。在url中,您还可以看到中间数据的输出和结果的准确性(这是一个iPython notebook)
步骤0:安装依赖项
您需要安装以下库:
从命令行:
步骤1:加载数据
我们将使用熊猫来加载我们的数据。 pandas是一个易于加载数据的库。为了说明这一点,我们首先保存 示例数据到csv,然后加载它。
我们将用
train.csv
训练支持向量机,并用test.csv
获取测试标签步骤2:处理数据
我们将把数据帧转换成numpy数组,这是scikit的格式- 学会理解。
我们需要转换标签“B”、“M”、“C”,。。。也因为支持向量机 不懂弦乐。
然后用数据训练一个线性支持向量机
我们在这里看到,
train_labels
(5)的长度正好匹配多少行 我们有trainfeatures
。train_labels
中的每个项对应一行。步骤3:训练SVM
步骤4:在一些测试数据上评估支持向量机
链接和提示
您应该能够使用此代码并用您的培训数据替换
train.csv
,用您的测试数据替换test.csv
,并获得测试数据的预测和准确结果。请注意,由于您使用的是您培训过的数据,因此评估的准确性将异常高。
相关问题 更多 >
编程相关推荐