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

Python svm.NuSVC类代码示例

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

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



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

示例1: predict

def predict(transformed_data, args, trn_label ,tst_label):
  print 'imgpred',
  sys.stdout.flush()
  
  (ndim, nsample , nsubjs) = transformed_data.shape
  accu = np.zeros(shape=nsubjs)

  tst_data = np.zeros(shape = (ndim,nsample))
  trn_data = np.zeros(shape = (ndim,(nsubjs-1)*nsample))
  # image stimulus prediction 
  for tst_subj in range(nsubjs):
    tst_data = transformed_data[:,:,tst_subj]

    trn_subj = range(nsubjs)
    trn_subj.remove(tst_subj)

    for m in range(nsubjs-1):
      trn_data[:,m*nsample:(m+1)*nsample] = transformed_data[:,:,trn_subj[m]]

    # scikit-learn svm for classification
    #clf = NuSVC(nu=0.5, kernel = 'linear')
    clf = NuSVC(nu=0.5, kernel = 'linear')
    clf.fit(trn_data.T, trn_label)

    pred_label = clf.predict(tst_data.T)
      
    accu[tst_subj] = sum(pred_label == tst_label)/float(len(pred_label))

  return accu
开发者ID:cameronphchen,项目名称:pHA,代码行数:29,代码来源:imgpred.py


示例2: svm

class svm():
    def __init__(self):
        # self.clf = SVC(kernel='rbf')
        self.clf = NuSVC()

    def train(self, inputs):
        # Parameters:
        #     inputs: An array of Input objects containing input vectors along with their corresponding labels.

        # Creates lists to use for fitting model
        X = []
        Y = []
        for data in inputs:
            X.append((data.x/np.linalg.norm(data.x)))
            Y.append(data.y)
        # Fit model
        self.clf.fit(X, Y)

    def predict(self, input):
        # Parameters:
        #     input: An Input object containing an input vector to be used for predicting a label.

        x = input.x/np.linalg.norm(input.x)
        if isinstance(input, Input):
            return self.clf.predict(x)
        else:
            x = input/np.linalg.norm(input)
            return self.clf.predict(x)
开发者ID:amagoon,项目名称:Neural-Network-Tools,代码行数:28,代码来源:Backpropagator.py


示例3: predict_loo

def predict_loo(transformed_data, args, trn_label ,tst_label):
  print 'imgpred loo',
  print args.loo,
  sys.stdout.flush()

  (ndim, nsample , nsubjs) = transformed_data.shape

  loo = args.loo
  loo_idx = range(nsubjs)
  loo_idx.remove(loo)

  #tst_data = np.zeros(shape = (ndim,nsample))
  trn_data = np.zeros(shape = (ndim,(nsubjs-1)*nsample))
  # image stimulus prediction
  # tst_data : ndim x nsample
  tst_data = transformed_data[:,:,loo]

  for m in range(len(loo_idx)):
    trn_data[:,m*nsample:(m+1)*nsample] = transformed_data[:,:,loo_idx[m]]
  
  # scikit-learn svm for classification
  clf = NuSVC(nu=0.5, kernel = 'linear')
  clf.fit(trn_data.T, trn_label)
  pred_label = clf.predict(tst_data.T)
      
  accu = sum(pred_label == tst_label)/float(len(pred_label))

  return accu
开发者ID:cameronphchen,项目名称:pHA,代码行数:28,代码来源:imgpred.py


示例4: __init__

class RbfSVM:
	def __init__(self):
		self.clf = NuSVC(nu=0.7, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, verbose=False, max_iter=-1)
		self.pattern ='(?u)\\b[A-Za-z]{3,}'
		self.tfidf = TfidfVectorizer(sublinear_tf=False, use_idf=True, smooth_idf=True, stop_words='english', token_pattern=self.pattern, ngram_range=(1, 3))
	def train(self,fileName):
		print "RbfSVM Classifier is being trained"
		table = pandas.read_table(fileName, sep="\t", names=["cat", "message"])
		X_train = self.tfidf.fit_transform(table.message)
		Y_train = []
		for item in table.cat:
			Y_train.append(int(item)) 
		self.clf.fit(X_train, Y_train)
		print "RbfSVM Classifier has been trained"

	def classify(self,cFileName, rFileName):
		table = pandas.read_table(cFileName, names=["message"])
		X_test = self.tfidf.transform(table.message)
		print "Data have been classified"
		with open(rFileName,'w') as f:
			for item in self.clf.predict(X_test).astype(str):
				f.write(item+'\n')

	def validate(self,fileName):
		table = pandas.read_table(fileName, sep="\t", names=["cat", "message"])
		X_validate = self.tfidf.transform(table.message)
		Y_validated = self.clf.predict(X_validate).astype(str)
		totalNum = len(table.cat)
		errorCount = 0
		for i in range(0,totalNum):
			if int(table.cat[i])!=int(Y_validated[i]):
				errorCount += 1
		print "Data have been validated! Precision={}".format((totalNum-errorCount)/float(totalNum))
开发者ID:richelite,项目名称:classify,代码行数:33,代码来源:lib.py


示例5: fit

 def fit(self, X, Y, W):
     clf = NuSVC(nu=self.nu, kernel=self.kernel, degree=self.degree,
                 gamma=self.gamma, coef0=self.coef0, shrinking=self.shrinking,
                 probability=self.probability, tol=self.tol, cache_size=self.cache_size,
                 max_iter=self.max_iter)
     if W is not None:
         return NuSVMClassifier(clf.fit(X, Y.reshape(-1), W.reshape(-1)))
     return NuSVMClassifier(clf.fit(X, Y.reshape(-1)))
开发者ID:vishnu-locket,项目名称:orange3,代码行数:8,代码来源:svm.py


示例6: SVM_nuSVC

 def SVM_nuSVC(self):
     clf = NuSVC(nu=0.5, kernel=b'rbf', degree=3, gamma='auto', coef0=0.0,
                 shrinking=True, probability=False, tol=0.001,
                 cache_size=200, class_weight=None, verbose=False,
                 max_iter=-1, decision_function_shape=None,
                 random_state=None)
     print('nuSVC Classifier is fitting...')
     clf.fit(self.X_train, self.y_train)
     return clf
开发者ID:yqji,项目名称:MySK,代码行数:9,代码来源:Classifier.py


示例7: svc_nu

def svc_nu(X_train, categories,X_test, test_categories):
    from sklearn.svm import NuSVC

    svm_nu_classifier = NuSVC().fit(X_train, categories)
    y_svm_predicted = svm_nu_classifier.predict(X_test)
    print '\n Here is the classification report for support vector machine classiffier:'
    print metrics.classification_report(test_categories, y_svm_predicted)



    ''''
开发者ID:LewkowskiArkadiusz,项目名称:magistrerka_app,代码行数:11,代码来源:train.py


示例8: NonLinearSupportVectorMachine

def NonLinearSupportVectorMachine(x_train, y_train, x_cv, y_cv):
	"""
	Non Linear Support Vector Machine
	"""
	#print "Classifier: Support Vector Machine"
	clfr = NuSVC(probability=False)
	clfr.fit(x_train, y_train)
	#print 'Accuracy in training set: %f' % clfr.score(x_train, y_train)
	#if y_cv != None:
		#print 'Accuracy in cv set: %f' % clfr.score(x_cv, y_cv)
	
	return clfr
开发者ID:tbs1980,项目名称:Kaggle_DecMeg2014,代码行数:12,代码来源:Classify.py


示例9: testing

def testing():
    plot_x = range(1, 10)
    plot_y = []
    for i in xrange(1,10):
        vals = []
        for _ in xrange(20):
            train_data, validation_data, train_labels, validation_labels = split_data()
            clf = NuSVC(**get_kwargs(i))
            clf.fit(train_data, train_labels)
            vals.append(check_fit(clf.predict(validation_data), validation_labels))
        plot_y.append(np.mean(vals))

    plot_results(plot_x, plot_y)
开发者ID:MathYourLife,项目名称:kaggle-scikitlearn,代码行数:13,代码来源:07-NuSVC-default-parameters.py


示例10: fit_model_7

    def fit_model_7(self,toWrite=False):
        model = NuSVC(probability=True,kernel='linear')

        for data in self.cv_data:
            X_train, X_test, Y_train, Y_test = data
            model.fit(X_train,Y_train)
            pred = model.predict_proba(X_test)[:,1]
            print("Model 7 score %f" % (logloss(Y_test,pred),))

        if toWrite:
            f2 = open('model7/model.pkl','w')
            pickle.dump(model,f2)
            f2.close()
开发者ID:JakeMick,项目名称:kaggle,代码行数:13,代码来源:days_work.py


示例11: test_nusvc

def test_nusvc():    
    # print '==== NuSVC ===='
    # print 'Training...'
    clf = NuSVC()
    clf = clf.fit( train_data, train_labels )
    
    # print 'Predicting...'
    output = clf.predict(test_data).astype(int)
    
    predictions_file = open("CLF.csv", "wb")
    open_file_object = csv.writer(predictions_file)
    open_file_object.writerow(["PassengerId","Survived"])
    open_file_object.writerows(zip(test_id, output))
    predictions_file.close()
    # print 'Done.'
    print 'NuSVC : '
开发者ID:Raphael-De-Wang,项目名称:Semestre02,代码行数:16,代码来源:SVM.py


示例12: __init__

 def __init__(self, recurrence=30, w_size=20,hybrid = False):
     self.learner = NuSVC()
     
     #size of each training batch
     self.batch_size = w_size * (recurrence)
     #size of the sliding window for sharpé ratio
     self.window_size = 5 * self.batch_size
     
     #true if part of a hybrid learner
     self.hybrid = hybrid
     
     # the data matrix of a single batch
     # Data-Vector = r_1, ... r_n
     # with r_n := r_n - r_n-1
     self.returns = list()
     #training data for experimental apporach
     self.train_dat = list()
     self.labels = list()
     self.decisions = list()
     self.recurrence = recurrence
     
     self.last_decision = 0
     self.ready = False
     self.tstep = 0
     self.prices = list()
     return
开发者ID:Cypher42,项目名称:AutoBuffett,代码行数:26,代码来源:NLSVC.py


示例13: __init__

 def __init__(self,adaption = 0.5,transactionCost = 0.001, recurrence=35, realy_recurrent=False, w_size=20,label_par='r'):
     self.learner = NuSVC()
     self.transactionCost = transactionCost
     self.adaption = adaption
     
     #size of each training batch
     self.batch_size = 200 * (recurrence)
     #size of the sliding window for sharpé ratio
     self.window_size = w_size * self.batch_size
     
     # the data matrix of a single batch
     # Data-Vector = r_1, ... r_n, prediction_t-1
     # with r_n := r_n - r_n-1
     self.returns = list()
     self.labels = list()
     self.decisions = [0]
     self.weighted_returns = list()
     
     #self.rng = rnj.Learner()
     
     self.recurrence = recurrence
     self.last_decision = 0
     self.ready = False
     self.tstep = 0
     self.recurrent = realy_recurrent
     self.prices = list()
     self.label_par = label_par
     
     self.sharpeA_old = 1
     self.sharpeB_old = 1
     return
开发者ID:Cypher42,项目名称:AutoBuffett,代码行数:31,代码来源:NLSVM.py


示例14: nu_support_vector_machines

def nu_support_vector_machines(corpus, documents_training, documents_test, words_features, kernel, nu):
    """
    Another implementation of Support Vector Machines algorithm.
    :param corpus:
    :param documents_training:
    :param documents_test:
    :param words_features:
    :param kernel:
    :param nu:
    :return:
    """

    print
    print "----- nu-Support Vector Machines algorithm ------"
    print "Creating Training Vectors..."
    categories = util_classify.get_categories(corpus)  

    array_vector_training = []
    array_categories = []
    for (id, original_category, annotations) in documents_training:
        array_vector_training.append(util_classify.transform_document_in_vector(annotations, words_features, corpus))
        array_categories.append(util_classify.get_categories(corpus).index(original_category))    
        
    print "Training the algorithm..."
    classifier = NuSVC(nu=nu, kernel=kernel)

    X_train_features = []
    y_train_categories = []
    # Train all
    for (id, original_category, annotations) in documents_training:
        X_train_features.append(util_classify.transform_document_in_vector(annotations, words_features, corpus))
        y_train_categories.append(original_category)

    classifier.fit(np.array(X_train_features), np.array(y_train_categories))    

    print "Calculating metrics..."
    estimated_categories = []
    original_categories = []

    for (id, cat_original, annotations) in documents_test:
        cat_estimated = classifier.predict(np.array((util_classify.transform_document_in_vector(annotations, words_features, corpus))))
        estimated_categories.append(categories.index(cat_estimated))
        original_categories.append(categories.index(cat_original))

    return original_categories, estimated_categories
开发者ID:itecsde,项目名称:classification,代码行数:45,代码来源:classify_methods.py


示例15: __init__

class Classifier:
	def __init__(self, objective_data, subjective_data):
		OBJECTIVE = 0
		SUBJECTIVE = 1

		self.objective_data = objective_data
		self.subjective_data = subjective_data

		self.text = objective_data + subjective_data

		self.labels = [OBJECTIVE for i in objective_data] + [SUBJECTIVE for i in subjective_data]

		tuple_list = zip(self.text, self.labels)

		random.shuffle(tuple_list)

		self.text = [x for x,y in tuple_list]
		self.label = [y for x,y in tuple_list]

		self.count_vectorizer = CountVectorizer(stop_words="english", min_df=3)

		# count vectorizer and specific classifier that will be used

		self.counts = self.count_vectorizer.fit_transform(self.text)
		self.classifier = None

		self.tf_transformer = TfidfTransformer(use_idf=True)
		self.frequencies = self.tf_transformer.fit_transform(self.counts)

	def multinomialNB(self):
		self.classifier = MultinomialNB(alpha=.001)
		self.classifier.fit(self.frequencies, self.labels)

	def predict(self, examples):
		example_counts = self.count_vectorizer.transform(examples)
		example_tf = self.tf_transformer.transform(example_counts)
		predictions = self.classifier.predict(example_tf)
		return predictions

	def linearSVC(self):
  		self.classifier = LinearSVC()
  		self.classifier.fit(self.frequencies, self.labels)

  	def nuSVC(self):
  		self.classifier = NuSVC()
  		self.classifier.fit(self.frequencies, self.labels)

  	def accurracy(self, text, labels):
  		prediction = self.predict(text)
  		accurracy = 0
  		for i in range(len(prediction)):
  			if prediction[i] == labels[i]:
  				accurracy += 1
  		return accurracy / float(len(prediction))

  	def f1(self, text, actual):
  		prediction = self.predict(text)
  		return f1_score(actual, prediction)
开发者ID:alokedesai,项目名称:NLP-Final-Assignment,代码行数:58,代码来源:classifier.py


示例16: sigmoidNuSVC

def sigmoidNuSVC():
    maxRandomPerformance = []
    for gamma in xrange(1,200):
        clf = NuSVC(kernel="sigmoid",gamma=gamma)
        clf.fit(trainData, trainLabel)
        maxRandomPerformance.append(clf.score(validationData, validationLabel))

    gammaValue = maxRandomPerformance.index(max(maxRandomPerformance)) + 1
    clfFinal = NuSVC(kernel='sigmoid', gamma=gammaValue)
    clfFinal.fit(trainData,trainLabel)
    score = clfFinal.score(testData,testLabel)

    guideToGraph['Sigmoid Nu-SVC'] = score
开发者ID:RonakSumbaly,项目名称:Malware-Classification,代码行数:13,代码来源:classifications.py


示例17: polyNuSVC

def polyNuSVC():
    maxRandomPerformance = []

    for deg in xrange(1,200):
        clf = NuSVC(kernel="poly",degree=deg)
        clf.fit(trainData, trainLabel)
        maxRandomPerformance.append(clf.score(validationData, validationLabel))

    gammaValue = maxRandomPerformance.index(max(maxRandomPerformance)) + 1
    clfFinal = NuSVC(kernel='poly', gamma=gammaValue)
    clfFinal.fit(trainData,trainLabel)
    score = clfFinal.score(testData,testLabel)

    guideToGraph['Polynomial Nu-SVC'] = score
开发者ID:RonakSumbaly,项目名称:Malware-Classification,代码行数:14,代码来源:classifications.py


示例18: fit_nusvc

def fit_nusvc(X_train, y_train, nu, kernel, gamma=0.1, degree=4, coef0=1):
    print "Training, nu = ", nu
    start = time.time()
    clf = NuSVC(nu=nu, kernel=kernel, degree=degree, coef0=coef0)
    clf.fit(X_train, y_train)
    return clf, time.time() - start
开发者ID:Raz0r,项目名称:lightning,代码行数:6,代码来源:plot_nusvm.py


示例19: range

     y_train = labels[100:172,i]
     X_test = sample2
     y_test = labels[272:,i]
 else:
     X_train = training
     y_train = labels[:172,i]
     X_test = sampletest
     y_test = labels[172:,i]
 #best case: 67, 1
 posterior = np.empty([100,72,6])
 for j in range(1,67):
     for k in range(1,2):
         box = np.zeros([6,6])
         accuracy = np.zeros(72)
         for m in range(0,10):
             nsvc = NuSVC(nu=j/100.0, degree=k)
             nsvc.fit(X_train, y_train)
             y_pred = nsvc.predict(X_test)
             
             n=0
             for i in range(0,len(y_pred)):
                 if y_pred[i] == y_test[i]:
             #print i, y_pred[i], y_test[i]
                     n = n+1
                     accuracy[i] = accuracy[i]+1
                 box[y_test[i]-1,y_pred[i]-1] = box[y_test[i]-1,y_pred[i]-1] + 1
             #posterior[m] =  knc.predict_proba(X_test)
         #print j, k, np.mean(accuracy)/0.72, np.std(accuracy)/0.72
         print j, k, sum(accuracy[0:8])/8.0, sum(accuracy[8:18])/10.0, sum(accuracy[18:30])/12.0, sum(accuracy[56:72])/16.0, sum(accuracy[30:43])/13.0, sum(accuracy[43:56])/13.0, sum(accuracy)/72.0
     '''
 means = np.empty([72,6])
开发者ID:d-giles,项目名称:KeplerML,代码行数:31,代码来源:nusvc.py


示例20: SVC

svc_new = SVC(probability=True, C=.000001, kernel='poly', gamma=4,
                  degree=4)
svc_new.fit(train_x_reduced, train_y_practice)
print svc_new.score(test_x_reduced, test_y_practice)
"""
"""
parameters = {'degree':(1, 3, 6)}
svclass = NuSVC(kernel='poly', probability=True, gamma=0, nu=.5852, tol=.00001)
clf = GridSearchCV(svclass, parameters, cv=10)
clf.fit(train_x_reduced, train_y_practice)
print "SVC"
print clf.best_estimator_
print clf.best_score_
print clf.best_params_
"""
svc_new = NuSVC(kernel='poly', probability=True, gamma=0, nu=.5852, tol=.00001)
svc_new.fit(train_x_reduced, train_y_practice)
print svc_new.score(test_x_reduced, test_y_practice)


print 'Predicting'
estimator = SelectKBest(score_func=f_classif, k=components)
estimator.fit(train_x, train_y_leaderboard)
train_x_reduced = estimator.transform(train_x)
test_x_reduced = estimator.transform(test_x)
print train_x.shape
print train_x_reduced.shape

#svc_new = SVC(probability=True, C=.000001, kernel='poly', gamma=4,
#                  degree=4)
svc_new = NuSVC(kernel='poly', probability=True, gamma=0, nu=.5852, tol=.00001)
开发者ID:rdimaggio,项目名称:kaggle_overfitting,代码行数:31,代码来源:analysis_v1.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python svm.OneClassSVM类代码示例发布时间:2022-05-27
下一篇:
Python svm.LinearSVC类代码示例发布时间: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