Exemple #1
0
    def test_car_data(self):
        """Simple example using car data."""
        # Car data from example 'Naive Bayes Classifier example'
        # by Eric Meisner November 22, 2003
        # http://www.inf.u-szeged.hu/~ormandi/teaching/mi2/02-naiveBayes-example.pdf
        xcar = [
            ["Red", "Sports", "Domestic"],
            ["Red", "Sports", "Domestic"],
            ["Red", "Sports", "Domestic"],
            ["Yellow", "Sports", "Domestic"],
            ["Yellow", "Sports", "Imported"],
            ["Yellow", "SUV", "Imported"],
            ["Yellow", "SUV", "Imported"],
            ["Yellow", "SUV", "Domestic"],
            ["Red", "SUV", "Imported"],
            ["Red", "Sports", "Imported"],
            ]

        ycar = [
            "Yes",
            "No",
            "Yes",
            "No",
            "Yes",
            "No",
            "Yes",
            "No",
            "No",
            "Yes",
            ]

        carmodel = NaiveBayes.train(xcar, ycar)
        self.assertEqual("Yes", NaiveBayes.classify(carmodel, ["Red", "Sports", "Domestic"]))
        self.assertEqual("No", NaiveBayes.classify(carmodel, ["Red", "SUV", "Domestic"]))
    def test_car_data(self):
        """Simple example using car data."""
        # Car data from example 'Naive Bayes Classifier example'
        # by Eric Meisner November 22, 2003
        # http://www.inf.u-szeged.hu/~ormandi/teaching/mi2/02-naiveBayes-example.pdf
        xcar = [
            ['Red', 'Sports', 'Domestic'],
            ['Red', 'Sports', 'Domestic'],
            ['Red', 'Sports', 'Domestic'],
            ['Yellow', 'Sports', 'Domestic'],
            ['Yellow', 'Sports', 'Imported'],
            ['Yellow', 'SUV', 'Imported'],
            ['Yellow', 'SUV', 'Imported'],
            ['Yellow', 'SUV', 'Domestic'],
            ['Red', 'SUV', 'Imported'],
            ['Red', 'Sports', 'Imported'],
            ]

        ycar = [
            'Yes',
            'No',
            'Yes',
            'No',
            'Yes',
            'No',
            'Yes',
            'No',
            'No',
            'Yes',
            ]

        carmodel = NaiveBayes.train(xcar, ycar)
        self.assertEqual("Yes", NaiveBayes.classify(carmodel, ['Red', 'Sports', 'Domestic']))
        self.assertEqual("No", NaiveBayes.classify(carmodel, ['Red', 'SUV', 'Domestic']))
Exemple #3
0
 def test_train_function_with_priors(self):
     model = NaiveBayes.train(self.xs,
                              self.ys,
                              priors={
                                  'male': 0.1,
                                  'female': 0.9
                              })
     result = NaiveBayes.calculate(model, self.test, scale=True)
     expected = -692.0
     self.assertEqual(expected, round(result['male']))
Exemple #4
0
 def setUp(self):
     # Using example from https://en.wikipedia.org/wiki/Naive_Bayes_classifier
     # height (feet), weight (lbs), foot size (inches)
     self.xs = [
         [6, 180, 12],
         [5.92, 190, 11],
         [5.58, 170, 12],
         [5.92, 165, 10],
         [5, 100, 6],
         [5.5, 150, 8],
         [5.42, 130, 7],
         [5.75, 150, 9],
     ]
     self.ys = [
         'male',
         'male',
         'male',
         'male',
         'female',
         'female',
         'female',
         'female',
     ]
     self.model = NaiveBayes.train(self.xs, self.ys)
     self.test = [6, 130, 8]
 def setUp(self):
     # Using example from https://en.wikipedia.org/wiki/Naive_Bayes_classifier
     # height (feet), weight (lbs), foot size (inches)
     self.xs = [
         [6, 180, 12],
         [5.92, 190, 11],
         [5.58, 170, 12],
         [5.92, 165, 10],
         [5, 100, 6],
         [5.5, 150, 8],
         [5.42, 130, 7],
         [5.75, 150, 9],
     ]
     self.ys = [
         'male',
         'male',
         'male',
         'male',
         'female',
         'female',
         'female',
         'female',
     ]
     self.model = NaiveBayes.train(self.xs, self.ys)
     self.test = [6, 130, 8]
Exemple #6
0
 def test_calculate_function_with_scale(self):
     result = NaiveBayes.calculate(self.model, self.test, scale=True)
     expected = -689.0
     self.assertEqual(expected, round(result['male']))
Exemple #7
0
 def test_classify_function(self):
     expected = "female"
     result = NaiveBayes.classify(self.model, self.test)
     self.assertEqual(expected, result)
Exemple #8
0
    def get(self):
        offset = int(self.get_argument('o', default='1'))
        rowcount = int(self.get_argument('r', default='10'))
        offset = (offset - 1) * rowcount
        no = self.get_argument('no', default='')
        model_id = self.get_argument('model_id', default='')
        model_type = self.get_argument('model_type', default='')
        package = self.get_argument('model_name', default='')
        cur = self.db.getCursor()
        rowdata = {}
        #查询
        if no == '1':
            if model_type == '1':
                cur.execute(
                    " select b.name,a.create_id,a.name,a.note,a.beta from public.logistis a "
                    " left join public.account b on a.create_id = b.id "
                    "where a.id='%s'  " % (model_id))
                rows = cur.fetchall()
                print(rows)
                rowdata['struct'] = "id,create_id,name,note,beta "
                rowdata['rows'] = rows
            else:
                cur.execute(
                    " select b.name,a.create_id,a.name,a.note,c.name,a.file_name from public.pymodel a "
                    " left join public.account b on a.create_id = b.id "
                    " left join public.model c on a.type = c.type "
                    " where a.id='%s' and a.type='%s' " %
                    (model_id, model_type))
                rows = cur.fetchall()
                rowdata['struct'] = "id,create_id,name,note,type,filename "
                rowdata['rows'] = rows
            self.response(rowdata)
        elif no == '2':
            if model_type == '1':
                beta = self.get_argument('beta', default='')
                model_data = self.get_argument('model', default='')
                a = []
                q = 0
                print(model_data)
                a = (list(eval(model_data)))
                model = LogisticRegression.LogisticRegression()
                model.beta = (list(eval(beta)))
                rowdata = {}
                rowdata['op'] = LogisticRegression.calculate(model, a)
                rowdata['rows'] = LogisticRegression.classify(model, a)
            elif model_type == '2':
                pack = 'data_mining.' + package
                import importlib
                bb = importlib.import_module(pack)
                ma = kNN.kNN()
                model = bb.model.knn(ma)
                model_data = self.get_argument('model', default='')
                a = []
                a = (list(eval(model_data)))
                rowdata = {}
                rowdata['op'] = kNN.calculate(model, a)
                rowdata['rows'] = kNN.classify(model, a)
            elif model_type == '3':
                pack = 'data_mining.' + package
                import importlib
                bb = importlib.import_module(pack)
                ma = NaiveBayes.NaiveBayes()
                model = bb.model.bayes(ma)
                model_data = self.get_argument('model', default='')
                a = []
                a = (list(eval(model_data)))
                rowdata = {}
                rowdata['op'] = NaiveBayes.calculate(model, a)
                rowdata['rows'] = NaiveBayes.classify(model, a)

            self.response(rowdata)
 def test_classify_function(self):
     expected = "female"
     result = NaiveBayes.classify(self.model, self.test)
     self.assertEqual(expected, result)
 def test_train_function_with_priors(self):
     model = NaiveBayes.train(self.xs, self.ys, priors={'male': 0.1, 'female': 0.9})
     result = NaiveBayes.calculate(model, self.test, scale=True)
     expected = -692.0
     self.assertEqual(expected, round(result['male']))
 def test_calculate_function_with_scale(self):
     result = NaiveBayes.calculate(self.model, self.test, scale=True)
     expected = -689.0
     self.assertEqual(expected, round(result['male']))
Exemple #12
0
	def get(self):
		offset   = int(self.get_argument('o',default='1'))
		rowcount = int(self.get_argument('r',default='10'))
		offset=(offset-1)*rowcount
		no = self.get_argument('no', default='')
		model_id = self.get_argument('model_id', default='')
		model_type = self.get_argument('model_type', default='')
		package=self.get_argument('model_name', default='')
		cur=self.db.getCursor()
		rowdata={}
		#查询
		if no=='1':
			if model_type =='1':
				cur.execute(" select b.name,a.create_id,a.name,a.note,a.beta from public.logistis a "
				            " left join public.account b on a.create_id = b.id "
						"where a.id='%s'  "% (model_id) )
				rows = cur.fetchall()
				print(rows)
				rowdata['struct']="id,create_id,name,note,beta "
				rowdata['rows']= rows
			else:
				cur.execute(" select b.name,a.create_id,a.name,a.note,c.name,a.file_name from public.pymodel a "
					    " left join public.account b on a.create_id = b.id "
				            " left join public.model c on a.type = c.type "
					    " where a.id='%s' and a.type='%s' "% (model_id,model_type) )
				rows = cur.fetchall()
				rowdata['struct']="id,create_id,name,note,type,filename "
				rowdata['rows']= rows				
			self.response(rowdata)
		elif no=='2':
			if model_type=='1':
				beta = self.get_argument('beta', default='')
				model_data=self.get_argument('model', default='')
				a=[]
				q=0
				print(model_data)
				a=(list(eval(model_data)))	
				model=LogisticRegression.LogisticRegression()
				model.beta=(list(eval(beta)))
				rowdata={}
				rowdata['op']=LogisticRegression.calculate(model,a)
				rowdata['rows']=LogisticRegression.classify(model,a)
			elif model_type=='2':
				pack='data_mining.'+package
				import importlib
				bb=importlib.import_module(pack)
				ma=kNN.kNN()
				model=bb.model.knn(ma)
				model_data=self.get_argument('model', default='')
				a=[]
				a=(list(eval(model_data)))	
				rowdata={}
				rowdata['op']=kNN.calculate(model,a)
				rowdata['rows']=kNN.classify(model,a)			
			elif model_type=='3':
				pack='data_mining.'+package
				import importlib
				bb=importlib.import_module(pack)
				ma=NaiveBayes.NaiveBayes()
				model=bb.model.bayes(ma)
				model_data=self.get_argument('model', default='')
				a=[]
				a=(list(eval(model_data)))	
				rowdata={}
				rowdata['op']=NaiveBayes.calculate(model,a)
				rowdata['rows']=NaiveBayes.classify(model,a)				
		
			self.response(rowdata)
Exemple #13
0
	def bayes(self):
		xs = [[-53.0, -200.78], [117.0, -267.14], [57.0, -163.47], [16.0, -190.3], [11.0, -220.94], [85.0, -193.94], [16.0, -182.71], [15.0, -180.41], [-26.0, -181.73], [58.0, -259.87], [126.0, -414.53], [191.0, -249.57], [113.0, -265.28], [145.0, -312.99], [154.0, -213.83], [147.0, -380.85], [93.0, -291.13]]
		ys =[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
		model = NaiveBayes.train(xs,ys)
		return model