示例#1
0
def xval(start,stop,data,rows,f,z,k,m,abcd):
    rmax = len(rows)
    test = []
    hypotheses = {}
    temp = ""
    for r in range(0, rmax):
        d = rows[r]
        if r >= start and r < stop:
            test.append(d)
        else:
            temp = klass1(d, z)
            try:
                hypotheses[temp] += 1
                if hypotheses[temp] == 1:
                    makeTable(colname[z],temp)
                addRow(d,temp)
            except KeyError:
                hypotheses[temp] = 1
                if hypotheses[temp] == 1:
                    makeTable(colname[z],temp)
                addRow(d,temp)
    #zeror(test, data, hypotheses, z) 
    #xvalTest1(test,data,hypotheses)
    #return nb(test,data,hypotheses,z,k,m,abcd)
    nb(test,data,hypotheses,z,k,m,abcd)
示例#2
0
def xval(start, stop, data, rows, f, z, k, m, abcd):
    rmax = len(rows)
    test = []
    hypotheses = {}
    temp = ""
    for r in range(0, rmax):
        d = rows[r]
        if r >= start and r < stop:
            test.append(d)
        else:
            temp = klass1(d, z)
            try:
                hypotheses[temp] += 1
                if hypotheses[temp] == 1:
                    makeTable(colname[z], temp)
                addRow(d, temp)
            except KeyError:
                hypotheses[temp] = 1
                if hypotheses[temp] == 1:
                    makeTable(colname[z], temp)
                addRow(d, temp)
    #zeror(test, data, hypotheses, z)
    #xvalTest1(test,data,hypotheses)
    #return nb(test,data,hypotheses,z,k,m,abcd)
    nb(test, data, hypotheses, z, k, m, abcd)
示例#3
0
文件: test_nb.py 项目: aew61/mlpy
 def _setup_lion_classifier(self):
     classifier = nb(feature_labels, class_label)
     classifier._class_node._truth_table = numpy.array([[0.9, 0.1]],
                                                       dtype=float)
     classifier._feature_nodes["has-fur?"]._truth_table = numpy.array(
         [[0.9, 0.1], [0.5, 0.5]], dtype=float)
     classifier._feature_nodes["long-teeth?"]._truth_table = numpy.array(
         [[0.5, 0.5], [0.1, 0.9]], dtype=float)
     classifier._feature_nodes["scary?"]._truth_table = numpy.array(
         [[0.5, 0.5], [0.2, 0.8]], dtype=float)
     return classifier
示例#4
0
文件: test_nb.py 项目: aew61/mlpy
    def test_train(self):
        classifier = nb(feature_labels, class_label)

        dataset = [
            {
                "has-fur?": True,
                "long-teeth?": False,
                "scary?": False
            },
            {
                "has-fur?": False,
                "long-teeth?": True,
                "scary?": True
            },
            {
                "has-fur?": True,
                "long-teeth?": True,
                "scary?": True
            },
        ]
        annotations = [
            False,
            False,
            True,
        ]

        classifier.train(dataset, annotations)
        lion_node = classifier._class_node
        has_fur_node = classifier._feature_nodes["has-fur?"]
        long_teeth_node = classifier._feature_nodes["long-teeth?"]
        scary_node = classifier._feature_nodes["scary?"]

        conditions_table = numpy.array([[0.5, 0.5], [0.0, 1.0]], dtype=float)
        self.assertTrue(
            numpy.array_equal(
                numpy.array([[0.67, 0.33]]),
                numpy.array(
                    [[numpy.round(x, 2) for x in lion_node._truth_table[0]]])))
        self.assertTrue(
            numpy.array_equal(
                conditions_table,
                numpy.array([[numpy.round(y, 2) for y in x]
                             for x in has_fur_node._truth_table])))
        self.assertTrue(
            numpy.array_equal(
                conditions_table,
                numpy.array([[numpy.round(y, 2) for y in x]
                             for x in long_teeth_node._truth_table])))
        self.assertTrue(
            numpy.array_equal(
                conditions_table,
                numpy.array([[numpy.round(y, 2) for y in x]
                             for x in scary_node._truth_table])))
示例#5
0
        
    #Genre!
    else:
        trainFeaturesAndLabels = [(featureExtractor(lyrics, words), genre) for (lyrics, genre) in trainSongs]
        testFeaturesAndLabels = [(featureExtractor(lyrics, words), genre) for (lyrics, genre) in testSongs] 
    
    thisTime = time.clock()
    print "Extract features: ", thisTime - lastTime, ' s'
    lastTime = thisTime    
        
    #Train classifier
    nbClassifier = nbTrain(trainFeaturesAndLabels, labels, words)
    
    thisTime = time.clock()
    print "Train Classifier: ", thisTime - lastTime, ' s'
    lastTime = thisTime
    
    #Test for errors
    trainError = nbClassifier.getErrorRate(trainFeaturesAndLabels)
    testError = nbClassifier.getErrorRate(testFeaturesAndLabels)
    
    thisTime = time.clock()
    print "Error checking: ", thisTime - lastTime, ' s'
    lastTime = thisTime
    
    print "Train Error: ", trainError     
    print "Test Error: ", testError, " with", len(labels), "labels"        
        
if __name__ == "__main__":
    nb()
示例#6
0
文件: test_nb.py 项目: aew61/mlpy
 def test_constructor(self):
     classifier = nb(feature_labels, class_label)
     self.assertEqual(len(classifier._feature_nodes), 3)
     self.assertEqual(classifier._class_node._label.get_label(), "lion")
     for feature_name in ["has-fur?", "long-teeth?", "scary?"]:
         self.assertTrue(feature_name in classifier._feature_nodes.keys())
示例#7
0
def getRandomBinaryChromosome(n):
	ans=[]
	for i in range(n):
		ans.append(choice(["1","0"]))
	return ans

seed(0)
done=False
limit=100
conv_limit=0.1
counter=0
oldfitness=[]
pop_size=10
crossover_rate=0.3
mutation_rate=0.25
n=nb(0)
# Initialize population randomly
population=[getRandomBinaryChromosome(n) for i in range(pop_size)]
while not done and counter<limit:
	counter+=1
	#print population
	done=True
	# Fitness evaluation
	fitness=[nb(None,i) for i in population]
	if oldfitness!=[]:
		for i in range(pop_size):
			if abs(fitness[i]-oldfitness[i])>conv_limit:
				done=False
		if done:
			print "Fitness converged! Iteration %d" % counter
			break
示例#8
0
文件: nbeg.py 项目: madachy/leaner
def _nb1():
  with settings(LIB,seed=2),settings(TABLE,era=3):
    nb("weather.csv")
示例#9
0
文件: nbeg.py 项目: madachy/leaner
def _nb1():
  with settings(LIB,seed=1),settings(TABLE,era=50):
    nb("housingD.csv")
示例#10
0
        trainFeaturesAndLabels = [(featureExtractor(lyrics, words), genre)
                                  for (lyrics, genre) in trainSongs]
        testFeaturesAndLabels = [(featureExtractor(lyrics, words), genre)
                                 for (lyrics, genre) in testSongs]

    thisTime = time.clock()
    print "Extract features: ", thisTime - lastTime, ' s'
    lastTime = thisTime

    #Train classifier
    nbClassifier = nbTrain(trainFeaturesAndLabels, labels, words)

    thisTime = time.clock()
    print "Train Classifier: ", thisTime - lastTime, ' s'
    lastTime = thisTime

    #Test for errors
    trainError = nbClassifier.getErrorRate(trainFeaturesAndLabels)
    testError = nbClassifier.getErrorRate(testFeaturesAndLabels)

    thisTime = time.clock()
    print "Error checking: ", thisTime - lastTime, ' s'
    lastTime = thisTime

    print "Train Error: ", trainError
    print "Test Error: ", testError, " with", len(labels), "labels"


if __name__ == "__main__":
    nb()