我正在使用libsvm库来支持向量机,我试图在一个python代码中包含所有的包(svm-scale,svm-train和svm-predict),如下所示:
#!/usr/bin/env python
import sys
import os
from subprocess import *
from util import *
from svm import *
from svmutil import *
params={"gamma":0.1, "cost":100000, "kernel":2}
model = 0
kernels = ["Linear", "Polynomial", "RBF", "Sigmoid", "Precomputed"]
values={"lower":-1, "upper":1}
def scaleTrainData(trainData):
global model
y, x = readDataSet(trainData)
arg1 = "-l %s -u %s" % (str(values["lower"]), str(values["upper"]))
model = svm_scale(y, x, arg)
print arg
def trainModel( trainData ):
global model
y, x = readDataSet(trainData)
arg = "-s 0 -t %s -g %s -c %s" % (str(params["kernel"]), str(params["gamma"]), str(params["cost"]))
model = svm_train(y, x, arg)
print arg
def readDataSet( dataSet ):
if type(dataSet) == type(""):
y, x = svm_read_problem(dataSet)
else:
y, x = parseDataSet(dataSet)
return y, x
def parseDataSet( dataSet ):
y, x = [], []
for line in dataSet:
line = line.split(None, 1)
if len(line) == 1:
line += ['']
label, features = line
xi = {}
for e in features.split():
ind, val = e.split(":")
xi[int(ind)] = float(val)
y += [float(label)]
x += [xi]
return (y, x)
def predictData( dataSet ):
y, x = readDataSet(dataSet)
label, acc, val = svm_predict(y, x, model)
print label
return label
scaleTrainData("trainWeek")
trainModel("train.scale")
predictData("test.scale")
但我收到以下错误: model = svm_scale(y,x,arg) NameError:全局名称' svm_scale'未定义
此错误表示什么?我应该如何在我的代码中包含svm-scale包,以便使用上面提到的代码来扩展,训练和预测我的数据集?