本文整理汇总了Python中sklearn.svm.NuSVC.predict方法的典型用法代码示例。如果您正苦于以下问题:Python NuSVC.predict方法的具体用法?Python NuSVC.predict怎么用?Python NuSVC.predict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.svm.NuSVC
的用法示例。
在下文中一共展示了NuSVC.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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))
示例2: svm
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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)
示例3: predict
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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
示例4: predict_loo
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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: __init__
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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)
示例6: svc_nu
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
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)
''''
示例7: testing
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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)
示例8: test_nusvc
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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 : '
示例9: nu_support_vector_machines
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [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
示例10: SelectKBest
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
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)
svc_new.fit(train_x_reduced, train_y_leaderboard)
output = svc_new.predict(test_x_reduced)
"""
"""
print 'Outputting'
open_file_object = csv.writer(open(
"simple" + str(datetime.now().isoformat()) +
".csv", "wb"))
open_file_object.writerow(['case_id', 'Target_Leaderboard'])
i = 0
for row in entries:
open_file_object.writerow([row, output[i].astype(np.uint8)])
i += 1
print 'Done'
print datetime.now() - start
示例11: range
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
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])
stds = np.empty([72,6])
grid = np.empty([6,6])
示例12: LogisticRegression
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
clf_LogisticRegression = LogisticRegression(n_jobs=-1).fit(X_train, y_train)
predicted_LogisticRegression = clf_LogisticRegression.predict(X_test)
accuracy_LogisticRegression = np.mean(predicted_LogisticRegression == y_test)
clf_LogisticRegression_f = open("pickled_algos/clf_LogisticRegression.pickle", "wb")
pickle.dump(clf_LogisticRegression, clf_LogisticRegression_f)
clf_LogisticRegression_f.close()
print('LogisticRegression accuracy: %s' %accuracy_LogisticRegression)
print(metrics.classification_report(predicted_LogisticRegression, y_test))
#NuSVC Classifier
clf_NuSVC = NuSVC().fit(X_train, y_train)
predicted_NuSVC = clf_NuSVC.predict(X_test)
accuracy_NuSVC = np.mean(predicted_NuSVC == y_test)
clf_NuSVC_f = open("pickled_algos/clf_NuSVC.pickle", "wb")
pickle.dump(clf_NuSVC, clf_NuSVC_f)
clf_NuSVC_f.close()
print('NuSVC accuracy: %s' %accuracy_NuSVC)
print(metrics.classification_report(predicted_NuSVC, y_test))
#LinearSVC Classifier
clf_LinearSVC = LinearSVC().fit(X_train, y_train)
predicted_LinearSVC = clf_LinearSVC.predict(X_test)
accuracy_LinearSVC = np.mean(predicted_LinearSVC == y_test)
示例13: range
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
cm = [None] * subjects
for subject in range(subjects):
# Concatenate the subjects' data for training into one matrix
train_subjects = list(range(subjects))
train_subjects.remove(subject)
TRs = image_data_shared[0].shape[1]
train_data = np.zeros((image_data_shared[0].shape[0], len(train_labels)))
for train_subject in range(len(train_subjects)):
start_index = train_subject*TRs
end_index = start_index+TRs
train_data[:, start_index:end_index] = image_data_shared[train_subjects[train_subject]]
# Train a Nu-SVM classifier using scikit learn
classifier = NuSVC(nu=0.5, kernel='linear')
classifier = classifier.fit(train_data.T, train_labels)
# Predict on the test data
predicted_labels = classifier.predict(image_data_shared[subject].T)
accuracy[subject] = sum(predicted_labels == test_labels)/float(len(predicted_labels))
# Create a confusion matrix to see the accuracy of each class
cm[subject] = confusion_matrix(test_labels, predicted_labels)
# Normalize the confusion matrix
cm[subject] = cm[subject].astype('float') / cm[subject].sum(axis=1)[:, np.newaxis]
# Plot and print the results
plot_confusion_matrix(cm, title="Confusion matrices for different test subjects with Probabilistic SRM")
print("SRM: The average accuracy among all subjects is {0:f} +/- {1:f}".format(np.mean(accuracy), np.std(accuracy)))
示例14: float
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
train_x, test_x = x[train_index], x[test_index]
train_y, test_y = y[train_index], y[test_index]
gnb.fit(train_x, train_y)
gnbrec.append( float((gnb.predict(test_x) == test_y).sum())/ len(test_y) )
nonneg_train_x = train_x - train_x.min()
nonneg_test_x = test_x - test_x.min()
mnb.fit(nonneg_train_x, train_y)
mnbrec.append( float((mnb.predict(nonneg_test_x) == test_y).sum())/ len(test_y) )
bnb.fit(train_x, train_y)
bnbrec.append( float((bnb.predict(test_x) == test_y).sum())/ len(test_y) )
svm.fit(train_x, train_y)
svmrec.append( float((svm.predict(test_x) == test_y).sum())/ len(test_y) )
_ = PCA(n_components=20).fit(train_x)
train_x = _.transform(train_x)
test_x = _.transform(test_x)
print train_x.shape
L = lmnn.fit(train_x, train_y, verbose=True).L
lmnnrec.append( knn(np.dot(train_x, L), train_y, np.dot(test_x, L), test_y, K=5) )
print '\tSVM accuracy: {} = {}'.format(svmrec, np.mean(svmrec))
print '\tLMNN accuracy: {} = {}'.format(lmnnrec, np.mean(lmnnrec))
print '\tGaussianNB accuracy: {} = {}'.format(gnbrec, np.mean(gnbrec))
print '\tMultinomiaNB accuracy: {} = {}'.format(mnbrec, np.mean(mnbrec))
print '\tBernoulliNB accuracy: {} = {}'.format(bnbrec, np.mean(bnbrec))
示例15: print
# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import predict [as 别名]
# Time = 7276.782202
# Saving data
joblib.dump(clf, learning_model_path)
########### Testing ####################################
print("Making Testing Data...")
test_data = np.array(p.read_csv(filepath_or_buffer=csv_test_path, header=None, sep=',', index_col=0))[:, :]
test_label = np.ravel(np.array(p.read_csv(filepath_or_buffer=csv_test_path, header=None, sep=',', usecols=[0]))[:, :])
print("Calculating Score...")
predict = clf.predict(test_data)
from sklearn.metrics import accuracy_score
print(accuracy_score(test_label, predict))
from sklearn.metrics import classification_report
print(classification_report(test_label, predict))
from sklearn import metrics
print ( metrics.confusion_matrix(test_label, predict) )
########### Results ####################################
# SVM