当前位置: 首页>>代码示例>>Python>>正文


Python SVC.fit方法代码示例

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


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

示例1: main

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def main():
    training = datas.training
    testing  = datas.testing
    classifier = SVC(kernel='linear',C=1.0)
    classifier.fit(training.ix[:,'pclass':],training.ix[:,'survived'])
    result = classifier.predict(testing)
    print result
开发者ID:sweemeng,项目名称:python_bigdataweek2013,代码行数:9,代码来源:svm_model.py

示例2: main

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def main():
    print 'MIFS'
    filename = ['../data/arcene.mat', '../data/gisette.mat', '../data/madelon.mat']
    for f_num in range(len(filename)):
        print filename[f_num]
        mat = scipy.io.loadmat(filename[f_num])
        X = mat['X']    # data
        y = mat['Y']    # label
        y = y[:, 0]
        X = X.astype(float)
        n_sample, n_features = X.shape
        # split data
        ss = cross_validation.KFold(n_sample, n_folds=10, shuffle=True)
        # choose SVM as the classifier
        clf = SVC()
        num_fea = np.linspace(5, 300, 60)
        correct = np.zeros(len(num_fea))
        for train, test in ss:
            # select features
            F = MIFS.mifs(X[train], y[train], n_selected_features=300)
            for n in range(len(num_fea)):
                fea_idx = F[0:num_fea[n]]
                features = X[:, fea_idx]
                clf.fit(features[train], y[train])
                y_predict = clf.predict(features[test])
                acc = accuracy_score(y[test], y_predict)
                correct[n] += acc
        correct.astype(float)
        correct /= 10
        for i in range(len(num_fea)):
            print num_fea[i], correct[i]
开发者ID:Sandy4321,项目名称:PyFeaST-2,代码行数:33,代码来源:test_MIFS.py

示例3: buildAndEvaluateSvm

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def buildAndEvaluateSvm(p_kernel):
    model = SVC(kernel=p_kernel)

    model.fit(features, target)
    expected = target
    predicted = model.predict(features)
    print("SCORE: %f" % metrics.roc_auc_score(expected,predicted))
开发者ID:richgruss,项目名称:CMDA,代码行数:9,代码来源:Gruss_Inclass_12-1-3.py

示例4: PcaGmm

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
class PcaGmm(BaseEstimator):
    def __init__(self, X_all,
                 pca_components = 12, gmm_components = 4,
                 covariance_type = "full", min_covar = 0.1,
                 gamma = 0, C = 1.0):
        self.pca_components = pca_components
        self.gmm_components = gmm_components
        self.covariance_type = covariance_type
        self.min_covar = min_covar
        self.gamma = gamma
        self.C = C
        self.X_all = X_all
        X_all = X_all[:, :pca_components]
        self.gmm = GMM(n_components = gmm_components,
                       covariance_type = covariance_type,
                       min_covar = min_covar)
        self.gmm.fit(X_all)
    def fit(self, X, y):
        X = X[:, :self.pca_components]
        X = self.gmm.predict_proba(X)
        self.svm = SVC(C = self.C, gamma = self.gamma)
        self.svm.fit(X, y)
    def predict(self, X):
        X = X[:, :self.pca_components]
        return self.svm.predict(self.gmm.predict_proba(X))
    def score(self, X, y):
        y_pred = self.predict(X)
        return accuracy_score(y, y_pred)
    def transform(self, X, y = None):
        X = X[:, :self.pca_components]
        return self.gmm.predict_proba(X)
    def __str__(self):
        return "PCA(%d)-GMM(%d, %s, %f)-SVM(C=%f, gamma=%f)" % (self.pca_components, self.gmm_components,self.covariance_type, self.min_covar,self.C, self.gamma)
开发者ID:ivanliu1989,项目名称:Data-Science-London,代码行数:35,代码来源:dsl.py

示例5: supportvector

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def supportvector(C, gamma = 'default'):
    
    from sklearn.svm import SVC
    from sklearn.metrics import accuracy_score
    
    if gamma == 'default':
        clf = SVC(kernel="rbf", C = C)
    else:
        clf = SVC(kernel="rbf", C = C, gamma = gamma)
    
    clf.fit(features_train, labels_train)
    
    t_fit = time()
    clf.fit(features_train, labels_train)
    print "training time:", round(time()-t_fit, 3), "s"
    
    t_pred = time()
    pred = clf.predict(features_test)
    print "predict time:", round(time()-t_pred, 3), "s"
    
    print accuracy_score(pred, labels_test)
    
    try:
        prettyPicture(clf, features_test, labels_test)
    except NameError:
        pass
开发者ID:fluxium,项目名称:DAND-P5-Identify-Fraud-From-Enron-Email,代码行数:28,代码来源:your_algorithm.py

示例6: test_perm

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
    def test_perm(self):
        X, y = datasets.make_classification(n_samples=20, n_features=5, n_informative=2)
        n_perms = 2
        rnd = 0
        # = With EPAC
        wf = Perms(SVC(kernel="linear"), n_perms=n_perms, permute="y", random_state=rnd, reducer=None)
        r_epac = wf.top_down(X=X, y=y)
        # = With SKLEARN
        clf = SVC(kernel="linear")
        r_sklearn = list()
        for perm in Permutations(n=y.shape[0], n_perms=n_perms, random_state=rnd):
            y_p = y[perm, :]
            clf.fit(X, y_p)
            r_sklearn.append(clf.predict(X))
        key2cmp = "y" + conf.SEP + conf.PREDICTION

        # = Comparison
        for iperm in range(n_perms):
            comp = np.all(np.asarray(r_epac[iperm][key2cmp]) == np.asarray(r_sklearn[iperm]))
            self.assertTrue(comp, u"Diff Perm: EPAC vs sklearn")
        # test reduce
        for iperm in range(n_perms):
            r_epac_reduce = wf.reduce().values()[iperm][key2cmp]
            comp = np.all(np.asarray(r_epac_reduce) == np.asarray(r_sklearn[iperm]))
            self.assertTrue(comp, u"Diff Perm: EPAC reduce")
开发者ID:neurospin,项目名称:pylearn-epac,代码行数:27,代码来源:test_primitives.py

示例7: get_optimize_result

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def get_optimize_result(training_data, validation_data, important_cols_result):
    """ Get the number of cols that gets the best score """
    last_score = 0.0
    new_score = 0.0
    number_of_cols = 1
    decreases = 0
    optimal_result = {'score': 0.0, 'number_of_cols': 1}
    # Extract labels from data frames
    training_data_label, training_data = separate_labels(training_data)
    validation_data_label, validation_data = separate_labels(validation_data)
    while True:
        cols = important_cols_result.index[0: number_of_cols]
        # Fit models and test
        clf = SVC()
        clf.fit(training_data.iloc[:, cols], training_data_label)
        predictions = clf.predict(validation_data.iloc[:, cols])
        new_score = accuracy_score(validation_data_label, predictions)
        print(new_score)
        if new_score < optimal_result['score']:
            optimal_result['score'] = new_score
            optimal_result['number_of_cols'] = number_of_cols
            print(optimal_result)
        if last_score > new_score:
            decreases += 1
            if decreases > 5:
                break
        last_score = new_score
        number_of_cols += 5
    cols = important_cols_result.index[0: number_of_cols]
    print(optimal_result)
    export_optimal_result(training_data.iloc[:, cols].columns)
开发者ID:geramirez,项目名称:digit-recognizer,代码行数:33,代码来源:pipeline.py

示例8: main

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def main():
    tweets_fname = 'your_tweets.txt'
    labels_fname = 'your_labels.txt'
    # fill in with your own tweets and labels
    
    dictionary = get_words(tweets_fname)
    feature_vectors = get_features(tweets_fname, dictionary)
    labels = get_vectors(labels_fname)
    first_few_features = feature_vectors[:560]
    first_few_labels = labels[:560]
    last_few_features = feature_vectors[560:]
    last_few_labels = labels[560:]
    k = 5
    
    # this finds how accurate your measure is
    # metrics = [ "accuracy", "f1_score", "auroc", "precision",
    #             "sensitivity", "specificity" ]
    # for metric in metrics : 
    #     print str(metric) + ": "
    #     c, gamma = get_rbf(first_few_features, first_few_labels,
    #                                 k, metric)
    #     print "best c: " + str(c) + ", best gamma: " + str(gamma)
    #     rbf_clf = SVC(kernel='rbf', C=c, gamma=gamma)
    #     rbf_clf.fit(first_few_features, first_few_labels)
    #     perf, lower, upper = get_confidence_interval(rbf_clf, last_70_features,
    #                                         last_70_labels, metric)
    #     print "peformance: " + str(perf) + ", lower: " + str(lower) + ", upper: " + str(upper)
        
    X_test = get_features('your_other_tweets.txt', dictionary)
    clf = SVC(kernel='rbf', gamma=0.01, C=100, probability=True)
    clf.fit(feature_vectors,labels)
    y_pred = np.sign(clf.decision_function(X_test))
    print(y_pred)
开发者ID:ivanamies,项目名称:text_sentiment_analysis,代码行数:35,代码来源:tsa.py

示例9: multi_SVM

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def multi_SVM(needcv = False):
	NeedReFetch = NEED_REFETCH
	OnlyNeedReGenerate = ONLY_NEED_REGENERATE
	allGenreSongsTrain,allGenreSongsTest = fetchData_CM(NUM_NEED_PER_GENRE,GENRES,NeedReFetch,OnlyNeedReGenerate,USED_GENRES)
	# allGenreSongsTrain,allGenreSongsTest = featureSelection (allGenreSongsTrain,allGenreSongsTest,method = 'mean',testmode = True,n_features_to_select = 4)


	# assert(len(allGenreSongsTrain[0][0]) == 106)

	TrainX = []
	TrainY = []
	TestX = []
	TestY = []
	for i in range(sum(USED_GENRES)):
		for j in allGenreSongsTrain[i]:
			TrainX.append(j)
			TrainY.append(i)
		for k in allGenreSongsTest[i]:
			TestX.append(k)
			TestY.append(i)
	confuseMat = [[0 for i in range(sum(USED_GENRES))] for j in range(sum(USED_GENRES))];
	if not needcv:
		print "Start SVM training ... "
		model = SVC(probability=True,decision_function_shape='ovo',kernel = 'rbf',gamma = 0.0078125, C = 8)
		model.fit(TrainX,TrainY)
		print "Start SVM predicting ... "
		PredY = model.predict(TestX)
		for i in range(len(TestY)):
			confuseMat[TestY[i]][PredY[i]] += 1
		print(clfr(TestY, PredY))
	else:
		tuned_parameters = [															## remained to be play with
							{'kernel': ['rbf'], 'gamma': [2**i for i in range(-8,8)], 'C': [2**i for i in range(-8,8)]},
		 					# {'kernel': ['linear'], 'C': [2**i for i in range(-8,9,2)]},
		 					# {'kernel': ['poly'], 'gamma': [2**i for i in range(-8,9,2)], 'C': [2**i for i in range(-8,9,2)], 'degree':[2,3,4]},
		 					]
		print "Start SVM CV ... "
		clf = GSCV(SVC(decision_function_shape='ovo'), tuned_parameters, cv=5)
		clf.fit(TrainX, TrainY)


		print("Best parameters set found on development set:")
		print(clf.best_params_)
		# print("Grid scores on development set:")
		# print()
		# for params, mean_score, scores in clf.grid_scores_:
		# 	print("%0.4f (+/-%0.03f) for %r" % (mean_score, scores.std(), params))
		# print()

		print "Start SVM predicting ... "

		PredY = clf.predict(TestX)


		print(clfr(TestY, PredY))

		for i in range(len(TestY)):
			confuseMat[TestY[i]][PredY[i]] += 1

	return confuseMat
开发者ID:Phonicavi,项目名称:msd-genrecl,代码行数:62,代码来源:Combined_Model_Analysis.py

示例10: evaluate_bow

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def evaluate_bow(data, n_folds, stemmer=NullStemmer()):
    file_name = '.cache/%s_bow_%s.json.bz2' % (subreddit, stemmer)
    if os.path.exists(file_name):
        print('Found a cached copy of %s' % file_name)
        with bz2.BZ2File(file_name, 'r') as fp:
            data = json.load(fp)

        feature_matrix = np.asarray(data['feature_matrix'])
        label_vector = np.asarray(data['label_vector'])
    else:
        print('Generating feature matrix for %s' % file_name)
        feature_matrix, label_vector = bow.generate_feature_matrix(data, stemmer)
        with bz2.BZ2File(file_name, 'w') as fp:
            json.dump({
                'feature_matrix': feature_matrix.tolist(),
                'label_vector': label_vector.tolist(),
            }, fp)

    kf = StratifiedKFold(label_vector, n_folds=n_folds)

    scores = []
    y_test = []
    for index, (train, test) in enumerate(kf):
        classifier = SVC(kernel='linear', C=0.8)
        classifier.fit(feature_matrix[train], label_vector[train])
        scores.append(classifier.decision_function(feature_matrix[test]))
        y_test.append(label_vector[test])

    # Convert from list to single array
    scores = np.concatenate(scores)
    y_test = np.concatenate(y_test)

    return scores, y_test
开发者ID:MichaelAquilina,项目名称:Reddit-Recommender-Bot,代码行数:35,代码来源:evaluate.py

示例11: train_model

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
 def train_model(trainids, trainlabels): 
     clf = SVC(C=1.0, kernel='rbf') 
     train_vectors=np.zeros((len(trainids),NFeatures)) 
     for i in len(trainids): 
         train_vectors[i,:]=feature_vector[trainids[i][0]+'|'+trainids[i][1]]
     clf.fit(train_vectors, trainlabels.toarray() )  
     return(clf) 
开发者ID:chtnverma,项目名称:QuoraSimilarQuestions,代码行数:9,代码来源:Duplicates.py

示例12: svmTest

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def svmTest(tx, ty, rx, ry):
    print "SVM start"
    print strftime("%a, %d %b %Y %H:%M:%S", localtime())
    estimator = SVC()
    cv = ShuffleSplit(tx.shape[0], n_iter=10, test_size=0.2)
    kernels = ["sigmoid", "rbf"]
    classifier = GridSearchCV(
        estimator=estimator,
        cv=cv,
        param_grid=dict(kernel=kernels))

    classifier.fit(tx, ty)
    title = 'SVM ( best kernel = %s)' % (classifier.best_estimator_.kernel)

    estimator = SVC(kernel=classifier.best_estimator_.kernel)

    plot_learning_curve(estimator, title, tx, ty, cv=cv)
    estimator.fit(tx, ty)
    plt.savefig('svm.png', dpi=500)
    print "Classifier score:", classifier.score(rx, ry)
    print "Best number of estimators was", classifier.best_estimator_.kernel
    print "All scores:"
    print classifier.grid_scores_
    with open("svm_results.txt", 'w') as f:
        f.write("Best:" + str(classifier.best_estimator_.kernel))
        f.write("All classifier grid scores:\n\n" + str(classifier.grid_scores_))
    print "SVM end"
    print strftime("%a, %d %b %Y %H:%M:%S", localtime())
开发者ID:mmanguno,项目名称:machine-learning,代码行数:30,代码来源:bank.py

示例13: classifyPerCountry

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def classifyPerCountry(T,V,Y,Y_country_hat):
	Y_country = np.floor(Y / 1000)
	print "\nClassifying per Country"
	Y_city = Y 
	country_codes = list(set(Y_country))
	nCountryCodes = len(country_codes)
	Y_hat = np.zeros(len(Y_country_hat))
	for i in xrange(nCountryCodes):
		print '%s\r' % ' '*20,
		print '   ' , i*100/nCountryCodes,
#		clf = MultinomialNB(0.5)
		clf = SVC()
		country_idx = np.in1d(Y_country,country_codes[i])
		country_idx_sparse = country_idx.nonzero()[0]
		T_country = T[country_idx_sparse,:]
		Y_cityPerCountry = Y_city[country_idx]
		unique_Y_cityPerCountry=list(set(Y_cityPerCountry))
		predict_idx = np.in1d(Y_country_hat,country_codes[i])
		predict_idx_sparse = predict_idx.nonzero()[0]
		if len(unique_Y_cityPerCountry)==1 :
			Y_hat[predict_idx] = unique_Y_cityPerCountry
			continue
		clf.fit(T_country,Y_cityPerCountry)
		if sum(predict_idx) > 1:
			Y_cityPerCountry_hat = clf.predict(V[predict_idx_sparse,:])
			Y_hat[predict_idx] = Y_cityPerCountry_hat
	print "\n"
	return Y_hat
开发者ID:swook,项目名称:KungFuLearning,代码行数:30,代码来源:train.py

示例14: load

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
 def load(self):
     # raise NotImplemented
     print("Entered worker")
     clf = SVC()
     iris = datasets.load_iris()
     clf.fit(iris.data, iris.target_names[iris.target])
     return clf
开发者ID:nave91,项目名称:nanoservice,代码行数:9,代码来源:code_example.py

示例15: svm_solver

# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import fit [as 别名]
def svm_solver(train_data, train_label, validation, test, dimreduce, convertbinary) :
    """
    """
    logging.info ('begin to train the svm classifier')

    # train_data = train_data[:100,:]
    # validation = validation[:100,:]
    # test = test[:100,:]
    # train_label = train_label[:100]
    train_data, validation, test = dimreduce(train_data, train_label, validation, test)
    # print new_train_data.shape
    train_data, validation, test = convertbinary(train_data, validation, test)

    """
    svc = SVC ()
    params_rbf = {"kernel": ['rbf'],
             "class_weight": ['auto'],
             "C": [0.1 ,0.2 ,0.3 ,0.5 ,1, 2, 3, 5, 10],
             "gamma": [0.01, 0.03,  0.05, 0.1, 0.2, 0.3, 0.5],
             "tol": 10.0** -np.arange(1, 5),
             "random_state": [1000000007]}
    logging.info ("Hyperparameter opimization using RandomizedSearchCV...")
    rand_search_result = RandomizedSearchCV (svc, param_distributions = params_rbf, n_jobs = -1, cv = 3, n_iter = 30)
    # rand_search_result = GridSearchCV (svc , param_grid = params_rbf , n_jobs = 8  , cv = 3)
    rand_search_result.fit (train_data , train_label)
    params = tools.report (rand_search_result.grid_scores_)
    """
    params = {'kernel': 'poly', 'C': 0.1, 'random_state': 1000000007, 'tol': 0.001, 'gamma': 0.1, 'class_weight': 'auto'}
    svc = SVC (probability = True, **params)

    svc.fit (train_data , train_label)
    evaluate.get_auc (svc.predict_proba (validation)[:,1])
    return svc.predict_proba (test)[:,1]
开发者ID:cxlove,项目名称:RPPredict,代码行数:35,代码来源:svm.py


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