• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python svmlight.classify函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中svmlight.classify函数的典型用法代码示例。如果您正苦于以下问题:Python classify函数的具体用法?Python classify怎么用?Python classify使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了classify函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: ball_only_classifier

def ball_only_classifier(circles, color_image, bonus_radius):
    model = svmlight.read_model("./output/best_single_cup_model_for_ball")
    ff = find_features()
    # TODO: fix
    label = 0
    best_classification = 0.5
    best_circle = None
    best_circle_pixels = None
    for c in circles[:6]:
        pixels, circle = find_pixels(c, color_image, bonus_radius)
        # create features for that circle
        features = ff.generate_features(pixels, label)
        features = parse_one_line(features)
        print features
        # run the classifier on that circle
        classification = svmlight.classify(model, [features])
        print classification
        if classification[0] > best_classification:
            best_classification = classification
            best_circle = [c]
            best_circle_pixels = pixels
        # make a decision about whether that circle is circly enough
        # cv2.imshow("Image processed", circle)
        # cv2.waitKey()

    # for the strict form of the classifier, I require that all of the detected circles
    # are in fact circles.  other classifiers may be more lenient
    return best_circle, best_classification, best_circle_pixels
开发者ID:briantoth,项目名称:BeerPongButler,代码行数:28,代码来源:detectCups.py


示例2: main_svmlight

def main_svmlight():
    # copied:
    import svmlight
    import pdb
    
    training_data = syntheticData(30, 1)
    test_data     = syntheticData(30, 1)
    #training_data = __import__('data').train0
    #test_data = __import__('data').test0

    print 'HERE 0'
    print 'training_data is', training_data
    print 'test_data is', test_data

    # train a model based on the data
    #pdb.set_trace()
    print 'HERE 1'
    model = svmlight.learn(training_data, type='regression', kernelType=2, verbosity=3)
    print 'HERE 2'

    # model data can be stored in the same format SVM-Light uses, for interoperability
    # with the binaries.
    svmlight.write_model(model, 'my_model.dat')
    print 'HERE 3'

    # classify the test data. this function returns a list of numbers, which represent
    # the classifications.
    #predictions = svmlight.classify(model, test_data)
    pdb.set_trace()
    predictions = svmlight.classify(model, training_data)
    print 'HERE 4'
    
    for p,example in zip(predictions, test_data):
        print 'pred %.8f, actual %.8f' % (p, example[0])
开发者ID:elgold92,项目名称:QuadraTot,代码行数:34,代码来源:SVMStrategy.py


示例3: predict_proba

    def predict_proba(self, data):
        ''' returns the confidence of being included in the positive class '''
        dummy_target = np.zeros(data.shape[0])
        svm_test_data = npToSVMLightFormat(data, dummy_target)
        predictions = svmlight.classify(self.model, svm_test_data)

        return np.array(predictions)
开发者ID:pyongjoo,项目名称:twitter-research,代码行数:7,代码来源:ml_tsvm.py


示例4: predict_proba

 def predict_proba(self, X):
     y = np.zeros(X.shape[0]).tolist()
     test_data = self.toSvmlight(X, y)
     scores = np.array(svmlight.classify(self.model, test_data))
     scores = 1 / (1 + np.exp(-scores))
     scores.shape = (len(scores),1)
     scores = np.hstack([1-scores, scores])
     return scores
开发者ID:pyongjoo,项目名称:ende,代码行数:8,代码来源:svm2.py


示例5: run_svm

def run_svm(article_count, feature_functions, kernel='polynomial', split=0.9, model_path='svm.model'):
    # https://bitbucket.org/wcauchois/pysvmlight
    articles, total_token_count = preprocess_wsj(article_count, feature_functions)

    dictionary = Dictionary()
    dictionary.add_one('ZZZZZ')  # so that no features are labeled 0
    data = []
    for article in articles:
        for sentence in article:
            for tag, token_features in zip(sentence.def_tags, sentence.data):
                # only use def / indef tokens
                if tag in ('DEF', 'INDEF'):
                    features = dictionary.add(token_features)
                    features = sorted(list(set(features)))
                    feature_values = zip(features, [1]*len(features))
                    data.append((+1 if tag == 'DEF' else -1, feature_values))

    train, test = bifurcate(data, split, shuffle=True)

    # for corpus, name in [(train, 'train'), (test, 'test')]:
        # write_svm(corpus, 'wsj_svm-%s.data' % name)

    #####################
    # do svm in Python...
    model = svmlight.learn(train, type='classification', kernel=kernel)

    # svmlight.learn options
    # type: select between 'classification', 'regression', 'ranking' (preference ranking), and 'optimization'.
    # kernel: select between 'linear', 'polynomial', 'rbf', and 'sigmoid'.
    # verbosity: set the verbosity level (default 0).
    # C: trade-off between training error and margin.
    # poly_degree: parameter d in polynomial kernel.
    # rbf_gamma: parameter gamma in rbf kernel.
    # coef_lin
    # coef_const
    # costratio (corresponds to -j option to svm_learn)
    svmlight.write_model(model, model_path)

    gold_labels, test_feature_values = zip(*test)
    # total = len(gold_labels)

    test_pairs = [(0, feature_values) for feature_values in test_feature_values]
    predictions = svmlight.classify(model, test_pairs)

    correct, wrong = matches(
        [(gold > 0) for gold in gold_labels],
        [(prediction > 0) for prediction in predictions])

    return dict(
        total_articles_count=len(articles),  # int
        total_token_count=total_token_count,  # int
        train_count=len(train),  # int
        test_count=len(test),  # int
        kernel=kernel,
        correct=correct,
        wrong=wrong,
        total=correct + wrong,
    )
开发者ID:Balkanlii,项目名称:nlp,代码行数:58,代码来源:definiteness.py


示例6: test

def test(test_data, fmodel_name):
  print ('[ test ] ===================')
  model = svmlight.read_model(fmodel_name)

  # classify the test data. this function returns a list of numbers, which represent
  # the classifications.
  predictions = svmlight.classify(model, test_data)
  for p in predictions:
      print '%.8f' % p
开发者ID:fangzheng354,项目名称:expert_finding,代码行数:9,代码来源:zmodel.py


示例7: runSVMLight

def runSVMLight(trainName,testName, kerneltype, c_param = 1.0, gamma_param = 1.0, verbosity = 0):
    """
    converts data to python format only if not already in python format 
    (files in python format are of type list, otherwise they are filenames)
    
    inputs: trainName, either the training data in svm-light format or the name of the training data file in LIBSVM/sparse format
            testName, either the test data in svm-light format or the name of the test data file in LIBSVM/sparse format
            kerneltype, (str)the type of kernel (linear, polynomial, sigmoid, rbf, custom)
            c_param, the C parameter (default 1)
            gamma_param, the gamma parameter (default 1)
            verbosity, 0, 1, or 2 for less or more information (default 0)
    
    outputs: (positiveAccuracy, negativeAccuracy, accuracy)
    """
    if type(trainName) == list:
        trainingData = trainName
    else:
        trainingData = sparseToList(trainName)
        
    
    if type(testName) == list:
        testData = testName
    else:
        testData = sparseToList(testName)
        
    if verbosity == 2:
        print "Training svm......."

    # train a model based on the data
    model = svmlight.learn(trainingData, type='classification', verbosity=2, kernel=kerneltype, C = c_param, rbf_gamma = gamma_param )
    
    # model data can be stored in the same format SVM-Light uses, for interoperability
    # with the binaries.
    
    # if type(trainName) == list:
    #     svmlight.write_model(model, time.strftime('%Y-%m-%d-')+datetime.datetime.now().strftime('%H%M%S%f')+'_model.dat')
    # else:
    #     svmlight.write_model(model, trainName[:-4]+'_model.dat')
    
    if verbosity == 2:
        print "Classifying........"

    # classify the test data. this function returns a list of numbers, which represent
    # the classifications.
    predictions = svmlight.classify(model, testData)
    
    # for p in predictions:
    #     print '%.8f' % p
    
    correctLabels = correctLabelRemove(testData)

    # print 'Predictions:'
    # print predictions
    # print 'Correct Labels:'
    # print correctLabels

    return predictionCompare(predictions, correctLabels, verbosity)
开发者ID:lbynum,项目名称:pysvm,代码行数:57,代码来源:functions.py


示例8: create_classifications

def create_classifications(models, test_set):
    '''
    For each supplied model, use svm light to classify the 
    test_set with that model
    '''
    classifications= {}
    for m in models.keys():
        classifications[m]= svmlight.classify(models[m], test_set)

    return classifications
开发者ID:jfein,项目名称:LearningSimilarity,代码行数:10,代码来源:svm_utils.py


示例9: test_model

def test_model(model,ind,n=3):
    test = []
    for i in ind.get_pos_train_ind():
        item = os.listdir("pos")[i]
        test.append((1,[(fmap.getID(item[0]),item[1]) for item in ngrams.ngrams(n, open("pos/"+item).read()).items() if fmap.hasFeature(item[0])]))
    for i in ind.get_neg_test_ind():
        item = os.listdir("neg")[i]
        test.append((-1,[(fmap.getID(item[0]),item[1]) for item in ngrams.ngrams(n, open("neg/"+item).read()).items() if fmap.hasFeature(item[0])]))
    predictions = svmlight.classify(model, test)
    return predictions
开发者ID:Grater,项目名称:Sentiment-Analysis,代码行数:10,代码来源:svm.py


示例10: trainAndTest

def trainAndTest(training, test):
    #trainingNames = [x[0] for x in training] # never used, but might be someday
    trainingData = [d.dataTuple() for d in training]
    testNames = [d.name for d in test]
    testData = [d.dataTuple() for d in test]
    testLabels = [d.label for d in test]
    
    model = svmlight.learn(trainingData)
    predictions = svmlight.classify(model,testData)
    return zip(predictions, testLabels, testNames)
开发者ID:babelon,项目名称:lying-classifiers,代码行数:10,代码来源:validateSVM.py


示例11: tsvm_test0

def tsvm_test0():
    # data processing
    data, target = load_svmlight_file('dataset/following.scale')
    data, target = shuffle(data, target)
    target = binarize(target)[:,0]

    cutoff = int(round(data.shape[0] * 0.8))

    train_data = data[:cutoff]
    train_target = target[:cutoff]

    transductive_train_data = data
    transductive_target = target.copy()
    transductive_target[cutoff:] = 0

    test_data = data[cutoff:]
    test_target = target[cutoff:]

    # convert the data into svmlight format
    svm_train_data = npToSVMLightFormat(train_data, train_target)
    svm_transductive_train_data = npToSVMLightFormat(transductive_train_data,
            transductive_target)
    svm_test_data = npToSVMLightFormat(test_data, test_target)

    print 'labels in the training data'
    print countLabels(svm_transductive_train_data).most_common()

    # svmlight routine
    model = svmlight.learn(svm_train_data,
            j=3.0, kernel='linear', type='classification', verbosity=0)
    trans_model = svmlight.learn(svm_transductive_train_data,
            j=3.0, kernel='linear', type='classification', verbosity=0)

    predictions = svmlight.classify(model, svm_test_data)
    trans_predictions = svmlight.classify(trans_model, svm_test_data)

    print 'inductive learning'
    print accuracy(predictions, test_target)
    print '(recall, precision)', recall_precision(predictions, test_target)

    print 'transductive learning'
    print accuracy(trans_predictions, test_target)
    print '(recall, precision)', recall_precision(trans_predictions, test_target)
开发者ID:pyongjoo,项目名称:twitter-research,代码行数:43,代码来源:ml_tsvm.py


示例12: trainAndTest

def trainAndTest(training, test):
    #trainingNames = [x[0] for x in training] # never used, but might be someday
    trainingData = [(d[1],d[2]) for d in training]

    testNames = [d[0] for d in test]
    testData = [(d[1],d[2]) for d in test]
    testLabels = [d[1] for d in test]
    
    model = svm.learn(trainingData)
    predictions = svm.classify(model,testData)
    return zip(predictions, testLabels, testNames)
开发者ID:babelon,项目名称:lying-classifiers,代码行数:11,代码来源:SVMTrainTest.py


示例13: five_fold_validation

def five_fold_validation(training_sets, validation_sets, c_value):
    total_accuracy= 0.0
    for i in range(len(training_sets)):

        model= svmlight.learn(training_sets[i], type='classification', C=c_value)
        classifications= svmlight.classify(model, validation_sets[i])
        predictions= change_to_binary_predictions(classifications)
        accuracy= find_accuracy(validation_sets[i], predictions)
        total_accuracy += accuracy[0]

    return total_accuracy/len(training_sets)
开发者ID:briantoth,项目名称:BeerPongButler,代码行数:11,代码来源:svm_cross_val_funcs.py


示例14: predict

 def predict(self, X):
   num_data = X.shape[0]
   scores = np.zeros((num_data, self.num_classes_,), dtype=np.float32)
   for i in xrange(self.num_classes_):
     scores[:, i] = svm.classify(
         self.model_[i],
         self.__data2docs(X, np.zeros((num_data,), dtype=np.float32)))
   if self.num_classes_ == 1:
     indices = (scores.ravel() > 0).astype(np.int)
   else:
     indices = scores.argmax(axis=1)
   return self.classes_()[indices]
开发者ID:queqichao,项目名称:FredholmLearning,代码行数:12,代码来源:tsvm.py


示例15: predict

	def predict(self, x_test):
		if self.trained != True:
			raise Exception("first train a model")

		x = self.svmlfeaturise(x_test)
		y_score = []
		for j in xrange(len(self.models)):
			m = np.array(svmlight.classify(self.models[j], x))
			y_score.append(m)

		y_predicted = np.argmax(y_score, axis=0)
		return y_predicted
开发者ID:adhaka,项目名称:kthasrdnn,代码行数:12,代码来源:tsvm.py


示例16: my_cross_val_score

def my_cross_val_score(data_fold, train, c_p):

    scores = []
    for x, y in data_fold:
        data_x = collect_data_qid(x, train)
        data_y = collect_data_qid(y, train)
        model = SVC.learn(data_x, C=c_p, kernel='linear', type='ranking')
        pred = SVC.classify(model, data_y)
        scores.append(
            my_accus(data_y, pred)
            )
    return scores
开发者ID:lacozhang,项目名称:machinelearning,代码行数:12,代码来源:grid.py


示例17: predict

    def predict(self, peptides, alleles=None, **kwargs):

        if isinstance(peptides, Peptide):
            pep_seqs = {str(peptides):peptides}
        else:
            if any(not isinstance(p, Peptide) for p in peptides):
                raise ValueError("Input is not of type Protein or Peptide")
            pep_seqs = {str(p):p for p in peptides}

        if alleles is None:
            al = [Allele("HLA-"+a) for a in self.supportedAlleles]
            allales_string = {conv_a:a for conv_a, a in itertools.izip(self.convert_alleles(al), al)}
        else:
            if isinstance(alleles, Allele):
                alleles = [alleles]
            if any(not isinstance(p, Allele) for p in alleles):
                raise ValueError("Input is not of type Allele")
            allales_string ={conv_a:a for conv_a, a in itertools.izip(self.convert_alleles(alleles),alleles)}

        #group peptides by length and
        result = {}
        for length, peps in itertools.groupby(pep_seqs.iterkeys(), key= lambda x: len(x)):
            #load svm model

            if length not in self.supportedLength:
                warnings.warn("Peptide length of %i is not supported by %s"%(length,self.name))
                continue

            encoding = self.encode(peps)

            for a in allales_string.keys():
                model_path = pkg_resources.resource_filename("Fred2.Data.svms.%s"%self.name, "%s_%i"%(a,length))
                if not os.path.exists(model_path):
                    warnings.warn("No model exists for peptides of length %i or allele %s."%(length,
                                                                                            allales_string[a].name))
                    continue
                model = svmlight.read_model(model_path)


                model = svmlight.read_model(model_path)
                pred = svmlight.classify(model, encoding.values())
                result[allales_string[a]] = {}
                for pep, score in itertools.izip(encoding.keys(), pred):
                    result[allales_string[a]][pep_seqs[pep]] = score

        if not result:
            raise ValueError("No predictions could be made for given input. Check your "
                             "epitope length and HLA allele combination.")
        df_result = EpitopePredictionResult.from_dict(result)
        df_result.index = pandas.MultiIndex.from_tuples([tuple((i, self.name)) for i in df_result.index],
                                                        names=['Seq', 'Method'])
        return df_result
开发者ID:SteffenK12,项目名称:Fred2,代码行数:52,代码来源:SVM.py


示例18: predict

  def predict(self, dataset):
    assert self.svm_list is not None
    
    self._format_test_data(dataset)
    num_samples = dataset.getNumSamples()
    num_features = dataset.getNumFeatures()

    predictions = np.zeros((num_samples, 12))

    for month_ind in range(12):
      # import pdb;pdb.set_trace()
      predictions[:, month_ind] = svmlight.classify(self.svm_list[month_ind], self.formatted_data)
    return predictions
开发者ID:dgboy2000,项目名称:online-sales,代码行数:13,代码来源:SVMRegression.py


示例19: __runSVMModels

 def __runSVMModels(self, img):
     inputfacepixels = list(img.getdata())
     inputface = asfarray(inputfacepixels)
     pixlistmax = max(inputface)
     inputfacen = inputface / pixlistmax        
     inputface = inputfacen - self.__imgdata.avgvals
     usub = self.__imgdata.eigenfaces[:self.__numFaces,:]
     input_wk = dot(usub, inputface.transpose()).transpose()
     data = [(0, self.__makeWeightTuplesList(input_wk))]
     predictions = list()
     for (name, model) in self.__models:
         pred = svmlight.classify(model, data)
         predictions.append((name,pred[0]))
     return predictions    
开发者ID:diederikvkrieken,项目名称:Asjemenao,代码行数:14,代码来源:eigenfaces.py


示例20: _get_svm_classification

    def _get_svm_classification(self, featureset):
        """
        given a set of features, classify them with our trained model
        and return a signed float

        :param featureset: a dict of feature/value pairs in NLTK format, representing a single instance
        """
        instance_to_classify = (0, map_features_to_svm(featureset, self._svmfeatureindex))
        if self._verbose:
            print 'instance', instance_to_classify
        # svmlight.classify expects a list; this should be taken advantage of when writing SvmClassifier.batch_classify / .batch_prob_classify.
        # it returns a list of floats, too.
        [prediction] = svmlight.classify(self._model, [instance_to_classify])
        return prediction
开发者ID:approximatelylinear,项目名称:nltk,代码行数:14,代码来源:svm.py



注:本文中的svmlight.classify函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python svmutil.svm_load_model函数代码示例发布时间:2022-05-27
下一篇:
Python svm.svm_problem函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap