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

Python svm.LinearSVC类代码示例

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

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



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

示例1: train

def train(dataset):
    print "Reading dataset ..."

    features = np.array(dataset.data, "int16")
    labels = np.array(dataset.target, "int")
    nExamples = features.shape[0]

    # Compute HOGs for each image in the database
    print "Extracting features for " + str(nExamples) + " training examples ... ",
    sys.stdout.flush()
    startTime = time.clock()
    list_hog_fd = []
    for feature in features:
        fd = hog(
            feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False
        )
        list_hog_fd.append(fd)
    hog_features = np.array(list_hog_fd, "float64")
    elapsedTime = time.clock() - startTime
    print "{0:.3f}s ({1:.4f}s/example)".format(elapsedTime, elapsedTime / nExamples)

    print "Training ... ",
    sys.stdout.flush()
    startTime = time.clock()
    clf = LinearSVC()
    clf.fit(hog_features, labels)
    elapsedTime = time.clock() - startTime
    print "{0:.3f}s".format(elapsedTime)

    print "Saving model to " + MODEL_FILE
    joblib.dump(clf, MODEL_FILE, compress=3)

    print "Training finished ..."
开发者ID:cruz,项目名称:iic1103,代码行数:33,代码来源:train.py


示例2: processMBHval

def processMBHval():
    for featType in ['MBH']:
        names = getnames()
        gtlabels = readpkl('{}data/labels.pkl'.format(baseDir))
        indexs = readpkl('{}data/indexs.pkl'.format(baseDir))
        actionIDs,taxonomy,database = readannos()
        print 'getting training data.... ',
        xtrain,ytrain = getdataVal(database,indexs,gtlabels,'training',featType)
        print 'got it!! and shape is ',np.shape(xtrain)
        #print 'getting validation data.... ',
        #xval,yval = getdata(database,indexs,gtlabels,'validation',featType)
        #print 'got it!! and shape is ',np.shape(xval)
        
    
        if featType == 'IMS':
            jobs = 16
            c = 0.01;
        else:
            jobs = 16
            c = 10;
    
        clf = LinearSVC(C = c)
        clf = clf.fit(xtrain, ytrain)
        
        saveName = '{}data/train-valSVM-{}.pkl'.format(baseDir,featType)
        with open(saveName,'w') as f:
            pickle.dump(clf,f)
开发者ID:gurkirt,项目名称:actNet-inAct,代码行数:27,代码来源:saveCLFs.py


示例3: svm_vecteur

def svm_vecteur():
    "Interprétation des images comme vecteurs de pixels et classification via le SVM"
    best=np.zeros(4)
    
    for npix in range(50,200,50):
        _, data, target, _ = utils.chargementVecteursImages(mer,ailleurs,1,-1,npix)
        X_train,X_test,Y_train,Y_test=train_test_split(data,target,test_size=0.3,random_state=random.seed())
        
        for iterations in range(250,1000,250):
            start_time = time.time()
            svc = LinearSVC(random_state=random.seed(), max_iter=iterations)
            
            x1=np.array(X_train)
            x1 = np.reshape(x1, (x1.shape[0],x1.shape[2]))
            x2=np.array(X_test)
            x2 = np.reshape(x2, (x2.shape[0],x2.shape[2]))
                
            svc.fit(X=x1, y=Y_train)
            score = svc.score(x2,Y_test)
                
            end_time = time.time()
            if score>best[0]:
                best[0] = score
                best[1] = iterations
                best[2] = end_time-start_time
                best[3] = npix
    
    print("| SVM linéaire                   | V.Pix {:4.0f} | iterations={:1.0f}                       | {:10.3f}ms | {:1.3f} |".format(best[3],best[1],best[3]*1000,best[0]))
开发者ID:laiaga,项目名称:TPSM1,代码行数:28,代码来源:classification_images.py


示例4: stump

def stump(X, y):
    score = cross_val_score(LinearSVC(), X, y, cv = 5, n_jobs=5, scoring = 'average_precision')
    clf = LinearSVC()
    clf.fit(X, y)
    coef = clf.coef_[0,0]
    inter = clf.intercept_[0]
    return np.mean(score), np.sign(coef), inter / np.abs(coef)
开发者ID:caomw,项目名称:TGIF-Release,代码行数:7,代码来源:rank_tags.py


示例5: fit

 def fit(self, X, Y, W):
     clf = LinearSVC(penalty=self.penalty, loss=self.loss, dual=self.dual,
                     tol=self.tol, C=self.C, multi_class=self.multi_class,
                     fit_intercept=self.fit_intercept,
                     intercept_scaling=self.intercept_scaling,
                     random_state=self.random_state)
     return LinearSVMClassifier(clf.fit(X, Y.reshape(-1)))
开发者ID:vishnu-locket,项目名称:orange3,代码行数:7,代码来源:svm.py


示例6: run_model

def run_model(X_train, y_train, X_test, y_test, model, layer, C=0):
    """
    Implement sklearn LinearSVC model and fit to training data.
    Predict labels of test data.
    Dump the training data, training labels, test features, test labels, predictions and pickled model.
    :params X_train: array of training features
    :params y_train: array of training labels
    :params X_test: array of test features
    :params y_test: array of test labels
    :params model: Name of the pre-trained CNN used for extracting features
    :params layer: Name of the layer used for extracting features
    :params C: Optimized C value from grid search
    """
    svc = LinearSVC(C=C)
    svc.fit(X_train, y_train)
    predicted_labels = svc.predict(X_test)
    directory_name = "svm_" + model + "_" + "layer_" + layer + "_" + str(datetime.date.today()).replace("-","_")
    directory_path = os.path.join("../models", directory_name)
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)
    X_train.dump(os.path.join(directory_path, "train_data"))
    y_train.dump(os.path.join(directory_path, "train_labels"))
    X_test.dump(os.path.join(directory_path, "test_data"))
    y_test.dump(os.path.join(directory_path, "test_labels"))
    predicted_labels.dump(os.path.join(directory_path, "predicted_labels"))
    joblib.dump(svc, os.path.join(directory_path, "model.pkl"))
    return
开发者ID:simonb83,项目名称:DataScienceIntensive,代码行数:27,代码来源:linear_svm.py


示例7: TrainingTesting

class TrainingTesting() :
	def __init__(self) :
		self.y = []
		self.noa = 0
		self.author_names = []
		self.train_data = []
		self.author_files = os.listdir(path+"/generated_files")
		#print author_names
		for author in self.author_files :
			self.author_names.append(author[:-4])
			text1 = open(path+"/generated_files/"+author,"r").read().split("\n")
			#print text1[1:-1]
			for txt in text1[1:-1] :
				t = []
				self.y.append(self.noa)
				#t.append(self.noa) 
				for i in txt.split(",")[1:-1] :
					t.append(float(i))
				self.train_data.append(t)
			self.noa += 1
		#print self.y
		#print self.train_data
	def train(self) :
		self.clfr = LinearSVC()
		self.clfr.fit(self.train_data,self.y)
		#print self.author_names[clfr.predict(self.train_data[0])[0]]
	def test(self,test_data) :
		self.correct_author_name = self.author_names[self.clfr.predict(test_data)[0]]
开发者ID:deepikapradeep,项目名称:authorship-predictor,代码行数:28,代码来源:1.py


示例8: retrain_models

def retrain_models(username):
	train_x, train_y, body_x, body_y, head_x, head_y = model_retriever.retrieve_data_db(username)

	b_train_x = []
	b_train_y = numpy.concatenate([body_y, train_y])

	for msg in (body_x + train_x):
		b_train_x.append(extract_body_features(msg))

	body_vec = TfidfVectorizer(norm="l2")
	b_train_x = body_vec.fit_transform(b_train_x)

	h_train_x = []
	h_train_y = numpy.concatenate([head_y, train_y])

	for msg in (head_x + train_x):
		h_train_x.append(extract_header_features(msg))

	head_vec = DictVectorizer()
	h_train_x = head_vec.fit_transform(h_train_x)

	body_model = LinearSVC(loss='l2', penalty="l2", dual=False, tol=1e-3)
	head_model = RidgeClassifier(tol=1e-2, solver="lsqr")

	body_model.fit(b_train_x, b_train_y)
	head_model.fit(h_train_x, h_train_y)

        print("Finished training models for "+username+"...")

	store_models(username, body_vec, body_model, head_vec, head_model)
开发者ID:dylanrhodes,项目名称:sigma,代码行数:30,代码来源:offline_updater.py


示例9: __init__

class LinearSVM:
	def __init__(self):
		self.clf = LinearSVC(penalty='l2', loss='l1', dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None)
		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 "LinearSVM 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 "LinearSVM 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


示例10: benchmark

def benchmark(k, epochs):
  print("*" * 80)
  print("k: %d, epochs: %d\n" % (k, epochs))

  #select = SelectKBest(score_func=chi2, k=k)
  select = TruncatedSVD(n_components=k)
  X_train_trunc = select.fit_transform(X_train, Y_train)
  X_test_trunc = select.transform(X_test)

  print('done truncating')

  parameters = {'C': [1, 10, 100, 1000, 10000],  'class_weight': ['auto', None], 'tol':[0.001,0.0001]}
  clf = LinearSVC(C=100000)
  #clf = grid_search.GridSearchCV(svc, parameters)
  clf.fit(X_train_trunc, Y_train)
  pred = clf.predict(X_test_trunc)

  if CREATE_SUBMISSION:
    X_submit_trunc = select.transform(X_submit)
    pred_submit = clf.predict(X_submit_trunc)
    dump_csv(pred_submit, k, epochs)

  score = metrics.f1_score(Y_test, pred)
  print("f1-score:   %0.3f" % score)

  print("classification report:")
  print(metrics.classification_report(Y_test, pred))

  print("confusion matrix:")
  print(metrics.confusion_matrix(Y_test, pred))
开发者ID:alireza-saberi,项目名称:Applied_MachineLearning_COMP_598_MiniProject2,代码行数:30,代码来源:svm_test.py


示例11: svm_binary_svc_probability

def svm_binary_svc_probability(X, Y, C):
    allp = np.sum(Y>0);
    alln = len(Y) - allp;
    nr_fold = 5;
    perm = list(range(len(Y)));
    random.shuffle(perm);
    dec_values = np.zeros(len(Y), dtype=np.float32);
    for i in range(nr_fold):
        start = i * len(Y) // nr_fold;
        end   = (i+1) * len(Y) // nr_fold;
        trainL = [perm[j] for j in range(len(Y)) if j not in range(start, end)];
        testL  = perm[start:end];
        trainX = X[trainL,:];
        trainY = Y[trainL];
        p_count = np.sum(trainY>0);
        n_count = len(trainY) - p_count;
        if p_count==0 and n_count==0:
            dec_values[start:end] = 0.0;
        elif p_count > 0 and n_count == 0:
            dec_values[start:end] = 1.0;
        elif p_count == 0 and n_count > 0:
            dec_values[start:end] = -1.0;
        else :
            subclf = LinearSVC(C=C, class_weight={1:allp,-1:alln});
            subclf.fit(trainX, trainY);
            dec_values[testL] = subclf.decision_function(X[testL,:]).ravel();
    return sigmoid_train(dec_values, Y);
开发者ID:domainxz,项目名称:pytools,代码行数:27,代码来源:dv2p.py


示例12: svm_for_multiclass

def svm_for_multiclass():
    text_file = "/home/web_server/wangyuanfu/age/temp1"
    dataset = np.loadtxt(text_file, delimiter=" ")
    X = dataset[:,1:]
    y = dataset[:,0:1]
    min_max_scaler = preprocessing.MinMaxScaler()
    normalized_X = min_max_scaler.fit_transform(X)
    print len(normalized_X)

    X_train, X_test, y_train, y_test = train_test_split(normalized_X, y, test_size=0.1, random_state=7)


    clf = LinearSVC(random_state=0, C=1, multi_class='ovr', penalty='l2')
    clf = clf.fit(X_train, y_train.reshape(-1))
    # print the training scores
    print("training score : %.3f " % (clf.score(X_train, y_train)))

    # make predictions
    predicted = clf.predict(X_test)
    length_predicted = len(predicted)
    print predicted.shape
    #for i in range(0,length_predicted):
    #    print predicted[i],y_test[i]
        #print X_test[i,:],predicted[i],y_test[i],probability[i]
    # summarize the fit of the model
    print(metrics.classification_report(y_test, predicted))
    print(metrics.confusion_matrix(y_test, predicted))
    print(metrics.precision_score(y_test, predicted, average='micro'))
开发者ID:wangyf5996,项目名称:python_learning,代码行数:28,代码来源:classify.py


示例13: do_SVM

def do_SVM(x,y,xt,yt):
	Cs = [0.01,0.1,10,100,1000]
	for C in Cs:
		print "El valor de C que se esta probando: %f"%C
		model = LinearSVC(C=C)
		model = model.fit(x, y)
		score_the_model(model,x,y,xt,yt,"SVM")
开发者ID:Paulinyta,项目名称:Tarea3_AID,代码行数:7,代码来源:pregunta2.py


示例14: MyComposition

class MyComposition(object):
    def __init__(self, n_estimators=60):
        self.n_estimators = n_estimators
        self.lsvc = LinearSVC(penalty='l1', dual=False)

        self.estimators_ = None
        self.weights_ = None

    def fit(self, x_train, y_train, x_train2, y_train2):
        # svr = SVR(**svr_params)
        svr = LinearSVR(**svr_params2)
        gbr = GradientBoostingRegressor(n_estimators=self.n_estimators,
                                        learning_rate=0.1)
        abr = AdaBoostRegressor(n_estimators=self.n_estimators,
                                learning_rate=0.01)

        estimators = [gbr, abr, svr]
        self.estimators_ = [e.fit(x_train, y_train) for e in estimators]

        x_pred = np.vstack([e.predict(x_train2) for e in self.estimators_]).T

        self.lsvc.fit(x_pred, y_train2)
        self.weights_ = np.array(self.lsvc.coef_ / np.sum(self.lsvc.coef_)).ravel()

        return self

    def predict(self, x):
        x_pred = np.vstack([e.predict(x) for e in self.estimators_]).T
        return np.sum(x_pred * self.weights_, axis=1).ravel()
开发者ID:KopBob,项目名称:technosphere,代码行数:29,代码来源:my_composition.py


示例15: SVC_on_fold

def SVC_on_fold(feature_sets, train, test, y, y_all, X, dim, dimsum, learn_options):
    y_bin = y_all[learn_options["binary target name"]].values[:, None]
    clf = LinearSVC(penalty="l2", dual=False)
    clf.fit(X[train], y_bin[train].flatten())
    # y_pred = clf.predict(X[test])[:, None] # this returns 0/1
    y_pred = clf.decision_function(X[test])[:, None]
    return y_pred, clf
开发者ID:MicrosoftResearch,项目名称:Azimuth,代码行数:7,代码来源:baselines.py


示例16: __init__

class Classifier:
  def __init__(self, ctype):
    self.ctype = ctype

  def train(self, data, labels):
    if self.ctype == "SVM":
      self.model = LinearSVC()
      self.model.fit(data, labels)
    elif self.ctype == "Decision":
      print "Unsupported"
    elif self.ctype == "Chi-Squared":
      self.model_data = data
      self.model_labels = labels

  def predict(self, data):
    if self.ctype == "SVM":
      return self.model.predict(data)
    elif self.ctype == "Decision":
      print "Unsupported"
    elif self.ctype == "Chi-Squared":
      predictions = []
      for sample, test_hist in enumerate(data):
        #Storing the first distance by default
        lowest_score = cv2.compareHist(np.array(self.model_data[0], dtype = np.float32),
                np.array(test_hist, dtype = np.float32), method = 1)
        predictions.append(self.model_labels[0])
        #Going through the rest of data
        for index, train_hist in enumerate(self.model_data):
          score = cv2.compareHist(np.array(train_hist, dtype = np.float32),
                np.array(test_hist, dtype = np.float32), method = 1)
          if score < lowest_score:
            lowest_score = score
            predictions[sample] = self.model_labels[index]
      return predictions
开发者ID:kevin-george,项目名称:surface-characterization,代码行数:34,代码来源:classifier.py


示例17: test_class_weights_rescale_C

def test_class_weights_rescale_C():
    # check that our crammer-singer implementation with class weights and
    # rescale_C=True is the same as LinearSVC's c-s class_weight implementation
    from sklearn.svm import LinearSVC
    X, Y = make_blobs(n_samples=210, centers=3, random_state=1, cluster_std=3,
                      shuffle=False)
    X = np.hstack([X, np.ones((X.shape[0], 1))])
    X, Y = X[:170], Y[:170]

    weights = 1. / np.bincount(Y)
    weights *= len(weights) / np.sum(weights)
    pbl_class_weight = MultiClassClf(n_features=3, n_classes=3,
                                     class_weight=weights, rescale_C=True)
    svm_class_weight = OneSlackSSVM(pbl_class_weight, C=10, tol=1e-5)
    svm_class_weight.fit(X, Y)

    try:
        linearsvm = LinearSVC(multi_class='crammer_singer',
                              fit_intercept=False, class_weight='auto', C=10)
        linearsvm.fit(X, Y)

        assert_array_almost_equal(svm_class_weight.w, linearsvm.coef_.ravel(),
                                  3)
    except TypeError:
        # travis has a really old sklearn version that doesn't support
        # class_weight in LinearSVC
        pass
开发者ID:DerThorsten,项目名称:pystruct,代码行数:27,代码来源:test_crammer_singer_svm.py


示例18: linearSVM

    def linearSVM(self):
        '''
            線形SVMを用いた2クラス分類
            args :      -> 
            dst  :      -> 
            param:      -> 
        '''
        # 学習データ
        data_training_tmp = np.loadtxt('../../../data/statistical_data/CodeIQ_auth.txt', delimiter=' ')
        data_training = [[x[0], x[1]] for x in data_training_tmp]
        label_training = [int(x[2]) for x in data_training_tmp]

        # 試験データ
        data_test = np.loadtxt('../../../data/statistical_data/CodeIQ_auth.txt', delimiter=' ')

        print np.array(data_test).shape

        # 学習
        estimator = LinearSVC(C=1.0)
        estimator.fit(data_training, label_training)

        # 予測
        label_prediction = estimator.predict(data_test[:,0:2])
        print(label_prediction)

        print 
开发者ID:DriesDries,项目名称:shangri-la,代码行数:26,代码来源:svm.py


示例19: _compute_svmnormalvector

def _compute_svmnormalvector((cache_dir, images, control_images, 
                              normalization_name, preprocess_file, rfe)):
    #try:
        import numpy as np 
        import sys
        from cpf.profiling.cache import Cache
        from cpf.profiling.normalization import RobustLinearNormalization, normalizations
        from sklearn.svm import LinearSVC
        from cpf.profiling.profile_svmnormalvector import _compute_rfe

        cache = Cache(cache_dir)
        normalization = normalizations[normalization_name]
        normalizeddata, normalized_colnames, _ = cache.load(images, normalization=normalization)
        control_data, control_colnames, _ = cache.load(control_images, normalization=normalization)
        if preprocess_file:
            preprocessor = cpf.util.unpickle1(preprocess_file)
            normalizeddata = preprocessor(normalizeddata)
            control_data = preprocessor(control_data)
        assert len(control_data) >= len(normalizeddata)
        downsampled = control_data[np.random.randint(0, len(control_data), len(normalizeddata)), :]
        x = np.vstack((normalizeddata, downsampled))
        y = np.array([1] * len(normalizeddata) + [0] * len(downsampled))
        clf = LinearSVC(C=1.0)
        m = clf.fit(x, y)
        normal_vector = m.coef_[0]
        if rfe:
            # Copy because it is immutable (normal_vector.flags.weriteable == False)
            normal_vector = np.array(normal_vector)
            normal_vector[~_compute_rfe(x, y)] = 0
        return normal_vector
开发者ID:pawni,项目名称:meoir,代码行数:30,代码来源:profile_svmnormalvector.py


示例20: train

def train(train_input, train_output, test_input, test_output):
    # 训练模块

    # 选择模型
    # model = MultinomialNB()
    # model = GaussianNB()
    # model = SGDClassifier()
    # model = SVC(kernel='linear') # 这个很慢
    model = LinearSVC()
    # model = RandomForestClassifier(max_depth=2, n_estimators=500)
    # model = AdaBoostClassifier(n_estimators=500,base_estimator=DecisionTreeClassifier(max_depth=10))

    # 训练 & 评测
    model.fit(train_input,train_output)
    pred_train = model.predict(train_input)
    pred_test = model.predict(test_input)

    label_size = max(train_output)+1
    train_ratio = cal_accuracy(pred_train, train_output)
    train_recal = cal_recall(pred_train, train_output, label_size)
    # print(test_output)
    print(list(pred_test))
    test_ratio = cal_accuracy(pred_test, test_output)
    test_recal = cal_recall(pred_test, test_output, label_size)
    print('%f\t%f'%(train_ratio, test_ratio))
    print('%f\t%f'%(train_recal, test_recal))
开发者ID:multiangle,项目名称:PyNLP,代码行数:26,代码来源:tfidf_sklearn.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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