Esempio n. 1
0
 def test_calculate_probability(self):
     k = 3
     model = kNN.train(xs, ys, k)
     weights = kNN.calculate(model, [6, -173.143442352])
     self.assertAlmostEqual(weights[0], 0.0, places=6)
     self.assertAlmostEqual(weights[1], 3.0, places=6)
     weights = kNN.calculate(model, [309, -271.005880394])
     self.assertAlmostEqual(weights[0], 3.0, places=6)
     self.assertAlmostEqual(weights[1], 0.0, places=6)
     weights = kNN.calculate(model, [117, -267.13999999999999])
     self.assertAlmostEqual(weights[0], 2.0, places=6)
     self.assertAlmostEqual(weights[1], 1.0, places=6)
Esempio n. 2
0
 def test_calculate_probability(self):
     k = 3
     model = kNN.train(xs, ys, k)
     weights = kNN.calculate(model, [6, -173.143442352])
     self.assertAlmostEqual(weights[0], 0.0, 6)
     self.assertAlmostEqual(weights[1], 3.0, 6)
     weights = kNN.calculate(model, [309, -271.005880394])
     self.assertAlmostEqual(weights[0], 3.0, 6)
     self.assertAlmostEqual(weights[1], 0.0, 6)
     weights = kNN.calculate(model, [117, -267.13999999999999])
     self.assertAlmostEqual(weights[0], 2.0, 6)
     self.assertAlmostEqual(weights[1], 1.0, 6)
Esempio n. 3
0
    def classify(self, sound_fft, som, knn_model):
        """
        Classify a new sound. Takes in a sound FFT, a SOM model and kNN model.
        Returns a tuple containing a highest probability label and a dictionary of
        all label probabilities.
        """
        sequence = self.sound_fft_to_string(sound_fft, som)

        weights = kNN.calculate(knn_model,
                                self.stringify_sequence(sequence),
                                weight_fn=self.knn_weight_fn,
                                distance_fn=self.sound_seq_distance_str)

        sum_weights = float(sum(weights.values()))

        most_class = None
        most_weight = None

        for klass, weight in weights.items():
            weights[klass] = weight / sum_weights

            if most_class is None or weight > most_weight:
                most_class = klass
                most_weight = weight

        # make sure there are no 0's
        for k, v in weights.items():
            if v == 0: weights[k] = 1.0e-2

        # renormalize
        tot = sum(weights.values())
        for k, v in weights.items():
            weights[k] = v / float(tot)

        return most_class, weights
Esempio n. 4
0
 def classify(self, sound_fft, som, knn_model):
     """
     Classify a new sound. Takes in a sound FFT, a SOM model and kNN model.
     Returns a tuple containing a highest probability label and a dictionary of
     all label probabilities.
     """
     sequence = self.sound_fft_to_string(sound_fft, som)
     
     weights = kNN.calculate(knn_model,
                             self.stringify_sequence(sequence),
                             weight_fn=self.knn_weight_fn,
                             distance_fn=self.sound_seq_distance_str)
                           
     sum_weights = float(sum(weights.values()))
     
     most_class = None
     most_weight = None
     
     for klass, weight in weights.items():
         weights[klass] = weight / sum_weights
         
         if most_class is None or weight > most_weight:
             most_class = klass
             most_weight = weight
             
     # make sure there are no 0's
     for k,v in weights.items():
         if v == 0: weights[k] = 1.0e-2
         
     # renormalize
     tot = sum(weights.values())
     for k,v in weights.items():
         weights[k] = v / float(tot)
         
     return most_class, weights
Esempio n. 5
0
 def classify(self, sound_fft):
     """
     Classify a new sound. Takes in a sound FFT, a SOM model and kNN model.
     Returns a tuple containing a highest probability label and a dictionary of
     all label probabilities.
     """
     sequence = []
     
     for col in range(sound_fft.shape[1]):
         sequence.append(self.som.bmu(sound_fft[:,col]))
         
     weights = kNN.calculate(self.knn_model,
                             self.stringify_sequence(sequence),
                             weight_fn=self.knn_weight_fn,
                             distance_fn=self.sound_seq_distance_str)
                           
     sum_weights = float(sum(weights.values()))
     
     most_class = None
     most_weight = None
     
     for klass, weight in weights.items():
         weights[klass] = weight / sum_weights
         
         if most_class is None or weight > most_weight:
             most_class = klass
             most_weight = weight
             
     return most_class, weights
Esempio n. 6
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)
Esempio n. 7
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)