【问题标题】:PyML: graphing the decision surfacePyML:绘制决策面
【发布时间】:2013-02-11 14:20:56
【问题描述】:

PyML 具有绘制决策面的功能。

首先,您需要告诉 PyML 使用哪些数据。在这里,我将 sparsevectordata 与我的特征向量一起使用。这是我用来训练我的 SVM 的那个。

demo2d.setData(training_vector)

然后你需要告诉它你想使用哪个分类器。我给它一个训练有素的 SVM。

demo2d.decisionSurface(best_svm, fileName = "dec.pdf")

但是,我收到以下错误消息:

Traceback (most recent call last):
**deleted by The Unfun Cat**
    demo2d.decisionSurface(best_svm, fileName = "dec.pdf")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyML/demo/demo2d.py", line 140, in decisionSurface
    results = classifier.test(gridData)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyML/evaluators/assess.py", line 45, in test
    classifier.verifyData(data)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyML/classifiers/baseClassifiers.py", line 55, in verifyData
    if len(misc.intersect(self.featureID, data.featureID)) != len(self.featureID) :
AttributeError: 'SVM' object has no attribute 'featureID'

【问题讨论】:

    标签: python data-visualization svm pyml


    【解决方案1】:

    我将直接深入研究源代码,因为我从未使用过 PyML。试图在网上找,但是在网上的PyML 0.7.2中找不到verifyData方法,只好去下载源搜索。

    分类器的featureID 仅在baseClassifier 类的train 方法中设置(第77-78 行):

    if data.__class__.__name__ == 'VectorDataSet' :
            self.featureID = data.featureID[:]
    

    在您的代码中,data.__class__.__name__ 的计算结果为 "SparseDataSet"(或您正在使用的任何其他类),而表达式的计算结果为 False(从不设置 featureID)。

    然后在demo2d.decisionSurface:

    gridData = VectorDataSet(gridX)
    gridData.attachKernel(data.kernel)
    results = classifier.test(gridData)
    

    尝试使用VectorDataSet 测试您的分类器。在这种情况下,classifier.test 等效于调用 assess.test 方法,该方法尝试验证数据是否与使用 baseClassifier.verifyData 的训练数据具有相同的特征:

    def verifyData(self, data) :
      if data.__class__.__name__ != 'VectorDataSet' :
          return
      if len(misc.intersect(self.featureID, data.featureID)) != len(self.featureID) :
           raise ValueError, 'missing features in test data'
    

    然后测试传递数据的类,现在是"VectorDataSet",并继续尝试访问从未创建的featureID 属性。

    基本上,它要么是一个错误,要么是一个隐藏的功能。

    长话短说,您必须将数据转换为VectorDataSet,因为SVM.featureID 没有另外设置。

    此外,您无需向其传递经过训练的数据集,该函数会为您训练分类器。

    编辑:

    我还想提一下setData 方法:

    def setData(data_) :
        global data
        data = data_
    

    根本没有类型检查。所以有人可能会将data 设置为anything,例如一个整数、一个字符串等,这将导致decisionSurface 中的错误。

    如果你要使用setData,你必须小心使用它(只能使用VectorDataSet),因为代码没有你想的那么灵活。

    【讨论】:

    • 感谢您为这么少的一笔钱做了出色的代码检测工作。
    • 当我有时间查看您的答案时,将尝试添加一些示例等等。
    猜你喜欢
    • 2014-02-26
    • 2018-12-20
    • 2013-10-03
    • 2020-08-15
    • 2017-02-21
    • 2014-08-30
    • 2016-11-09
    • 2021-07-30
    相关资源
    最近更新 更多