Esempio n. 1
0
labels = []
# fileIn = open('../data/testSet.txt')
fileIn = open('../data/lr_data')
for line in fileIn.readlines():
    lineArr = line.strip().split('\t')
    dataSet.append([float(lineArr[0]), float(lineArr[1])])
    labels.append(float(lineArr[2]))

dataSet = mat(dataSet)
labels = mat(labels).T
train_x = dataSet[0:81, :]
train_y = labels[0:81, :]
test_x = dataSet[80:101, :]
test_y = labels[80:101, :]

## step 2: training...
print "step 2: training..."
C = 0.6
toler = 0.001
maxIter = 50
svmClassifier = svm.trainSVM(train_x, train_y, C, toler, maxIter, kernelOption=('linear', 0))

## step 3: testing
print "step 3: testing..."
accuracy = svm.testSVM(svmClassifier, test_x, test_y)

## step 4: show the result
print "step 4: show the result..."
print 'The classify accuracy is: %.3f%%' % (accuracy * 100)
svm.showSVM(svmClassifier)
Esempio n. 2
0
    lineArr = line.strip().split('\t')
    dataSet.append([float(lineArr[0]), float(lineArr[1])])
    labels.append(float(lineArr[2]))

dataSet = mat(dataSet)
labels = mat(labels).T
train_x = dataSet[0:81, :]
train_y = labels[0:81, :]
test_x = dataSet[80:101, :]
test_y = labels[80:101, :]

## step 2: training
print("step 2: training...")
C = 0.6
toler = 0.001
maxIter = 50
svmClassifier = svm.trainSVM(train_x,
                             train_y,
                             C,
                             toler,
                             maxIter,
                             kernelOption=('linear', 0))

## step 3: testing
print("step 3: testing...")
accuracy = svm.testSVM(svmClassifier, test_x, test_y)

## step 4: show the result
print("step 4: show the result...")
print('The classify accuracy is: %.3f%%' % (accuracy * 100))
svm.showSVM(svmClassifier)
Esempio n. 3
0
from knn import knn_test
from naiveBayes import testNaiveBayes
from svm import testSVM

if __name__ == "__main__":
    # Naive Bayes Test
    print("Running Naive Bayes.")
    print("Accuracy: ")
    testNaiveBayes()

    # KNN Test
    print("Running KNN. This will take some time.")
    print("Accuracy: ")
    knn_test()

    # SVM Test
    print("Running SVM.")
    print("Accuracy: ")
    testSVM()
Esempio n. 4
0
    # print(label.shape)
    # print(datas.shape)


if __name__ == '__main__':
    # data,label = svm.loadDataSet("testSet.txt")
    print("step 1:load data...")
    train, test, train_label, test_label = loadDataSet("testSet.txt")
    print("step 2: training...")
    C = 0.6
    toler = 0.001
    maxIter = 40
    svmClassifier, b, alpha = svm.trainSVM(train,
                                           train_label,
                                           C,
                                           toler,
                                           maxIter,
                                           kernelOption=('linear', 0))
    # # print(b)
    # # print(alpha[alpha>0])
    joblib.dump(svmClassifier, "train_model.m")  # 保存模型
    # svmClassifier = joblib.load("train_model.m")#加载模型
    # # ## step 3: testing
    print("step 3: testing...")
    test_accuracy = svm.testSVM(svmClassifier, test, test_label)
    # print(accuracy)
    print('The classify test_accuracy is: %.3f%%' % (test_accuracy * 100))
    ## step 4: show the result
    print("step 4: show the result...")
    svm.showSVM(svmClassifier)