本文整理汇总了Python中sklearn.svm.NuSVC.fit方法的典型用法代码示例。如果您正苦于以下问题:Python NuSVC.fit方法的具体用法?Python NuSVC.fit怎么用?Python NuSVC.fit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.svm.NuSVC
的用法示例。
在下文中一共展示了NuSVC.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例2: __init__
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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))
示例3: svm
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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)
示例4: predict_loo
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例5: fit
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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)))
示例6: SVM_nuSVC
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例7: NonLinearSupportVectorMachine
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例8: sigmoidNuSVC
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例9: fit_model_7
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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()
示例10: testing
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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)
示例11: polyNuSVC
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例12: nu_support_vector_machines
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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
示例13: __init__
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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)
示例14: test_nusvc
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
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 : '
示例15: svmClassifier
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import fit [as 别名]
def svmClassifier():
for deg in xrange(1,200):
print deg
print "RBF Nu-SVC"
clf = NuSVC(gamma=deg)
clf.fit(trainData, trainLabel)
print(clf.score(testData,testLabel))
print "LINEAR Nu-SVC"
clf = NuSVC(kernel="linear")
clf.fit(trainData, trainLabel)
print(clf.score(testData,testLabel))
print "POLYNOMIAL Nu-SVC"
clf = NuSVC(kernel="poly",gamma=deg)
clf.fit(trainData, trainLabel)
print(clf.score(testData,testLabel))
print "SIGMOID Nu-SVC"
clf = NuSVC(kernel="sigmoid",gamma=deg)
clf.fit(trainData, trainLabel)
print(clf.score(testData,testLabel))