Beispiel #1
0
def testSimplePredictFor2():
	lexicons = loadLexiconTable()
	buildVectors(lexicons)
	testReviews = yelpReview.readTestData()
	total = 0
	correct = 0
	realArray = []
	predictArray = []
	for review in testReviews:
		if review['text'] == None or len(review['text'])==0:
			print review
			continue
		star = review['stars']-1
		if star == 2:
			continue
		total+=1
		if star == 0 or star == 1:
			star = 0
		elif star == 3 or star == 4:
			star = 1
		predictStar = simplePredictFor2(review, lexicons)
		realArray.append(star)
		predictArray.append(predictStar)
		#print star, preditStar
		if star == predictStar:
			correct+=1
	print 'correct:%d, total:%d'%(correct, total)
	precision = 1.0*correct/total*100
	print 'Precision: %f%%'%precision
	diffArray = numpy.subtract(realArray, predictArray)
	plt.plot(diffArray)
	plt.show()
Beispiel #2
0
def testSvmPredict():
	lexicons = loadLexiconTable()
	buildVectors(lexicons)
	clf = svmTrain(lexicons)
	testReviews = yelpReview.readTestData()
	total = 0
	correct = 0
	realArray = []
	predictArray = []
	for review in testReviews:
		if review['text'] == None or len(review['text'])==0:
			print review
			continue
		total+=1
		star = review['stars']-1
		predictStar = svmPredict(clf, review, lexicons)[0]
		print star, predictStar
		if predictStar < 0.18:
			predictStar = 0
		elif predictStar >=0.18 and predictStar<0.49:
			predictStar = 1
		elif predictStar >=0.49 and predictStar<2.37:
			predictStar = 2
		elif predictStar >=2.37 and predictStar<3.8:
			predictStar = 3
		elif predictStar >=3.8:
			predictStar = 4
		print star, predictStar
		realArray.append(star)
		predictArray.append(predictStar)
		#print star, preditStar
		if star == predictStar:
			correct+=1
	print 'correct:%d, total:%d'%(correct, total)
	precision = 1.0*correct/total*100
	print 'Precision: %f%%'%precision
	diffArray = numpy.subtract(realArray, predictArray)
	plt.plot(diffArray)
	plt.show()