Пример #1
0
def testKNN(filename, k):
    from random import shuffle
    from math import ceil
    data = DataSet.readFromArff(filename)
    data.normalize()
    data.simb2num(-1)
    d = data.data
    shuffle(d)
    ntrain = int(ceil(0.7 * len(d)))
    k = KNN(k, d[:ntrain])
    error_rate = 0
    i = 0
    for test in d[ntrain:len(d)]:
        i += 1
        answer = k.classify(test[:-1])
        expected = test[-1]
        error = abs(expected - answer)
        error_rate += error
    return float(error_rate) / float(i)
Пример #2
0
def testKNN(filename, k):
	from random import shuffle
	from math import ceil
	data = DataSet.readFromArff(filename)
	data.normalize()
	data.simb2num(-1)
	d = data.data
	shuffle(d)
	ntrain = int(ceil(0.7*len(d)))
	k = KNN(k, d[:ntrain])
	error_rate = 0
	i = 0
	for test in d[ntrain:len(d)]:
		i += 1
		answer = k.classify(test[:-1])
		expected = test[-1]
		error = abs(expected - answer)
		error_rate += error
	return float(error_rate) / float(i)
Пример #3
0
def testPerceptron(filename, alpha, nepochs):
    from random import shuffle
    from math import ceil
    data = DataSet.readFromArff(filename)
    data.normalize()
    data.simb2num(-1)
    d = data.data
    shuffle(d)
    ntrain = int(ceil(0.7 * len(d)))
    p = Perceptron(len(d[0]) - 1)
    p.train(d[:ntrain], alpha, nepochs)
    error_rate = 0
    i = 0
    for test in d[ntrain:len(d)]:
        i += 1
        answer = p.classify(test[:-1])
        expected = test[-1]
        error = abs(expected - answer)
        error_rate += error
    return float(error_rate) / float(i)
Пример #4
0
def testPerceptron(filename, alpha, nepochs):
	from random import shuffle
	from math import ceil
	data = DataSet.readFromArff(filename)
	data.normalize()
	data.simb2num(-1)
	d = data.data
	shuffle(d)
	ntrain = int(ceil(0.7*len(d)))
	p = Perceptron(len(d[0])-1)
	p.train(d[:ntrain], alpha, nepochs)
	error_rate = 0
	i = 0
	for test in d[ntrain:len(d)]:
		i += 1
		answer = p.classify(test[:-1])
		expected = test[-1]
		error = abs(expected - answer)
		error_rate += error
	return float(error_rate) / float(i)