本文整理汇总了Python中sklearn.svm.predict函数的典型用法代码示例。如果您正苦于以下问题:Python predict函数的具体用法?Python predict怎么用?Python predict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了predict函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_svc_invalid_break_ties_param
def test_svc_invalid_break_ties_param(SVCClass):
X, y = make_blobs(random_state=42)
svm = SVCClass(kernel="linear", decision_function_shape='ovo',
break_ties=True, random_state=42).fit(X, y)
with pytest.raises(ValueError, match="break_ties must be False"):
svm.predict(y)
开发者ID:kevin-coder,项目名称:scikit-learn-fork,代码行数:8,代码来源:test_svm.py
示例2: testLinear
def testLinear(self):
## 加载数据
dataArr, labelArr = self.loadDataSet('data/dataset2svm/testSet.txt')
svm = SVMLib()
## 训练一个线性分类器
ws, b = svm.fit(dataArr, labelArr, 0.6, 0.001, 40)
print ws
dataMat = mat(dataArr)
## 前半部分计算值为分类结果,后面为实际结果
## SVM分类器是个二元分类器,其结果为-1或1
## 因此训练时,训练集的值也为-1或1
print '-----------------'
print svm.predict(dataMat[0], ws, b), labelArr[0]
开发者ID:freedream520,项目名称:machine-learning,代码行数:13,代码来源:SVMTest.py
示例3: testMultiLinear
def testMultiLinear(self):
## 加载数据
dataArr, labelArr = self.loadMultiDataSet('data/dataset2svm/horseColicTest.txt')
svm = SVMLib()
## 训练一个线性分类器
ws, b = svm.fit(dataArr, labelArr, 0.6, 0.001, 40)
print ws
dataMat = mat(dataArr)
## 前半部分计算值为分类结果,后面为实际结果
## SVM分类器是个二元分类器,其结果为-1或1
## 因此训练时,训练集的值也为-1或1
print '-----------------'
## 根据SVM判断第4个数据的分类,大于0为1,小于0为-1
print svm.predict(dataMat[3], ws, b), labelArr[3]
开发者ID:freedream520,项目名称:machine-learning,代码行数:15,代码来源:SVMTest.py
示例4: k_fold_cv
def k_fold_cv(gram_matrix, labels, folds = 10, alg = 'SVM', shuffle = True):
"""
K-fold cross-validation
"""
pdb.set_trace()
scores = []
preds = []
loo = sklearn.cross_validation.KFold(len(labels), folds, shuffle = shuffle, random_state = random.randint(0,100))
#loo = sklearn.cross_validation.LeaveOneOut(len(labels))
for train_index, test_index in loo:
X_train, X_test = gram_matrix[train_index][:,train_index], gram_matrix[test_index][:, train_index]
y_train, y_test = labels[train_index], labels[test_index]
if(alg == 'SVM'):
svm = sklearn.svm.SVC(kernel = 'precomputed')
svm.fit(X_train, y_train)
preds += svm.predict(X_test).tolist()
score = svm.score(X_test, y_test)
elif(alg == 'kNN'):
knn = sklearn.neighbors.KNeighborsClassifier()
knn.fit(X_train, y_train)
preds += knn.predict(X_test).tolist()
score = knn.score(X_test, y_test)
scores.append(score)
print "Mean accuracy: %f" %(np.mean(scores))
print "Stdv: %f" %(np.std(scores))
return preds, scores
开发者ID:svegapons,项目名称:PyBDGK,代码行数:29,代码来源:Classification.py
示例5: leave_one_out_cv
def leave_one_out_cv(gram_matrix, labels, alg = 'SVM'):
"""
leave-one-out cross-validation
"""
scores = []
preds = []
loo = sklearn.cross_validation.LeaveOneOut(len(labels))
for train_index, test_index in loo:
X_train, X_test = gram_matrix[train_index][:,train_index], gram_matrix[test_index][:, train_index]
y_train, y_test = labels[train_index], labels[test_index]
if(alg == 'SVM'):
svm = sklearn.svm.SVC(kernel = 'precomputed')
svm.fit(X_train, y_train)
preds += svm.predict(X_test).tolist()
score = svm.score(X_test, y_test)
elif(alg == 'kNN'):
knn = sklearn.neighbors.KNeighborsClassifier()
knn.fit(X_train, y_train)
preds += knn.predict(X_test).tolist()
score = knn.score(X_test, y_test)
scores.append(score)
print "Mean accuracy: %f" %(np.mean(scores))
print "Stdv: %f" %(np.std(scores))
return preds, scores
开发者ID:svegapons,项目名称:PyBDGK,代码行数:26,代码来源:Classification.py
示例6: svm_iterkernel
def svm_iterkernel(train_data, train_labels, test_data, test_labels, op_name_dir):
label_set=np.unique(train_labels)
if op_name_dir != ('None' or 'none'):
fo=open(op_name_dir,'a')
predict_list={}
for kernel in ['linear']: #, 'poly', 'rbf']:
t0=time.time()
svm = SVC(C=1., kernel=kernel, cache_size=10240)
svm.fit(train_data, train_labels)
prediction=svm.predict(test_data)
predict_list[kernel]=prediction
pred_acc_tot =(float(np.sum(prediction == test_labels)))/len(test_labels)
print time.time() - t0, ',kernel = '+kernel, ',pred acc = '+str(round(pred_acc_tot*100))
if op_name_dir != ('None' or 'none'):
fo.write('time='+str(time.time() - t0)+'sec,kernel='+kernel+',pred acc='+str(round(pred_acc_tot*100))+'\n')
for lab_unq in label_set:
pred_acc=(prediction == lab_unq) & (test_labels == lab_unq)
pred_acc=float(pred_acc.sum())/(len(test_labels[test_labels == lab_unq]))
print 'pred_'+str(lab_unq)+','+str(round(pred_acc*100))
if op_name_dir != ('None' or 'none'):
fo.write('pred_'+str(lab_unq)+','+str(round(pred_acc*100))+'\n')
if op_name_dir != ('None' or 'none'):
fo.close()
return predict_list
开发者ID:DaveOC90,项目名称:Tissue-Segmentation,代码行数:30,代码来源:svm_iterkernel.py
示例7: get_error
def get_error(svm, X, y):
err = 0
N = y.shape[0]
for i in range(N):
if y[i] != svm.predict(X[i])[0]:
err += 1
return err*1. / N
开发者ID:pjhades,项目名称:coursera,代码行数:7,代码来源:1.py
示例8: run_model
def run_model(train_data, train_labels, test_data, test_labels):
'''
Algorithm which will take in a set of training text and labels to train a bag of words model
This model is then used with a logistic regression algorithm to predict the labels for a second set of text
Method modified from code available at:
https://www.kaggle.com/c/word2vec-nlp-tutorial/details/part-1-for-beginners-bag-of-words
Args:
train_data_text: Text training set. Needs to be iterable
train_labels: Training set labels
test_data_text: The text to
Returns:
pred_labels: The predicted labels as determined by logistic regression
'''
#use Logistic Regression to train a model
svm = SVC()
# we create an instance of Neighbours Classifier and fit the data.
svm.fit(train_data, train_labels)
#Now that we have something trained we can check if it is accurate with the test set
pred_labels = svm.predict(test_data)
perform_results = performance_metrics.get_perform_metrics(test_labels, pred_labels)
#Perform_results is a dictionary, so we should add other pertinent information to the run
perform_results['vector'] = 'Bag_of_Words'
perform_results['alg'] = 'Support_Vector_Machine'
return pred_labels, perform_results
开发者ID:aflower15,项目名称:pythia,代码行数:29,代码来源:svm.py
示例9: plotSVM
def plotSVM(svm,n,title):
plt.subplot(2,2,n)
Z = svm.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.title(title)
开发者ID:ictmaster,项目名称:svm,代码行数:8,代码来源:Example.py
示例10: test_accuracy
def test_accuracy(svm,x,y):
"""determines the accuracy of a svm classifier on validation set"""
hypothesis = svm.predict(x)
flat_y = y.ravel()
misclassification_count = 0
for i in xrange(len(flat_y)):
if not( hypothesis[i] == flat_y[i] ):
misclassification_count += 1
return misclassification_count
开发者ID:dennis-chen,项目名称:machine_learning,代码行数:9,代码来源:SVM_exercise.py
示例11: increment_svm
def increment_svm(svm, L_ids, baseline_accuracy):
L = X[L_ids]
y_l = y[L_ids]
U_ids = np.array(list((set(instance_ids) - set(L_ids))))
U = X[U_ids]
y_u = y[U_ids]
ordered_indices = np.argsort(svm.decision_function(U))
smallest_indices = ordered_indices[:500]
smallest_ids = U_ids[smallest_indices]
largest_indices = ordered_indices[-500:]
largest_ids = U_ids[largest_indices]
high_confidence_unlabeled = scipy.sparse.vstack([U[smallest_indices], U[largest_indices]])
high_confidence_ids = np.concatenate([smallest_ids, largest_ids])
high_confidence_predicted_labels = svm.predict(high_confidence_unlabeled)
high_confidence_true_labels = y[high_confidence_ids]
splits = sklearn.cross_validation.StratifiedShuffleSplit(high_confidence_predicted_labels, n_iter=2, test_size=0.9)
saved_L_primes = []
saved_L_prime_ids = []
saved_cv_accuracies = []
for augment_indices, test_indices in splits:
augment = high_confidence_unlabeled[augment_indices]
test = high_confidence_unlabeled[test_indices]
augment_ids = high_confidence_ids[augment_indices]
test_ids = high_confidence_ids[test_indices]
augment_labels = high_confidence_predicted_labels[augment_indices]
test_labels = high_confidence_predicted_labels[test_indices]
L_prime = scipy.sparse.vstack([L, augment])
y_l_prime = np.concatenate([y_l, augment_labels])
L_prime_ids = np.concatenate([L_ids, augment_ids])
saved_L_primes.append(L_prime)
saved_L_prime_ids.append(L_prime_ids)
svm_prime = sklearn.svm.LinearSVC(penalty='l2', C=10, dual=False)
accuracy = sklearn.cross_validation.cross_val_score(svm_prime, L_prime, y_l_prime, cv=5, n_jobs=7).mean()
saved_cv_accuracies.append(accuracy)
best_index = np.argmax(saved_cv_accuracies)
best_L_prime_ids = saved_L_prime_ids[best_index]
best_accuracy = saved_cv_accuracies[best_index]
return best_L_prime_ids, best_accuracy
开发者ID:CalculatedContent,项目名称:tsvm,代码行数:55,代码来源:incremental_tsvm_news.py
示例12: predict_embedded_attributes_labels
def predict_embedded_attributes_labels(data_mat, svms):
"""
Calculate class label predictions for each feature vector (=row) in data_mat.
@return: Matrix with each column containing class labels for one feature vector.
"""
num_attributes = len(svms)
num_examples = data_mat.shape[0]
A = np.zeros(shape=(num_attributes, num_examples))
log.d("Classifying {} examples...".format(num_examples))
for att_idx, svm in enumerate(svms):
log.update_progress(att_idx + 1, num_attributes)
if svm is not None:
if sklearn.__version__ == '0.14.1':
A[att_idx] = svm.predict(data_mat)
else:
# the return format of this function was changed in 0.15...
A[att_idx] = svm.predict(data_mat).T
print("")
return A
开发者ID:cwiep,项目名称:query-by-online-handwriting,代码行数:20,代码来源:svm.py
示例13: test_svc_ovr_tie_breaking
def test_svc_ovr_tie_breaking(SVCClass):
"""Test if predict breaks ties in OVR mode.
Related issue: https://github.com/scikit-learn/scikit-learn/issues/8277
"""
X, y = make_blobs(random_state=27)
xs = np.linspace(X[:, 0].min(), X[:, 0].max(), 1000)
ys = np.linspace(X[:, 1].min(), X[:, 1].max(), 1000)
xx, yy = np.meshgrid(xs, ys)
svm = SVCClass(kernel="linear", decision_function_shape='ovr',
break_ties=False, random_state=42).fit(X, y)
pred = svm.predict(np.c_[xx.ravel(), yy.ravel()])
dv = svm.decision_function(np.c_[xx.ravel(), yy.ravel()])
assert not np.all(pred == np.argmax(dv, axis=1))
svm = SVCClass(kernel="linear", decision_function_shape='ovr',
break_ties=True, random_state=42).fit(X, y)
pred = svm.predict(np.c_[xx.ravel(), yy.ravel()])
dv = svm.decision_function(np.c_[xx.ravel(), yy.ravel()])
assert np.all(pred == np.argmax(dv, axis=1))
开发者ID:kevin-coder,项目名称:scikit-learn-fork,代码行数:21,代码来源:test_svm.py
示例14: hw1q18
def hw1q18():
print "----------------------------------------"
print " Homework 1 Question 18 "
print "----------------------------------------"
Y_train_0 = (Y_train == 0).astype(int)
Y_test_0 = (Y_test == 0).astype(int)
print "in the training set:"
print "n(+) =", np.count_nonzero(Y_train_0 == 1), "n(-) =", np.count_nonzero(Y_train_0 == 0)
print "in the test set:"
print "n(+) =", np.count_nonzero(Y_test_0 == 1), "n(-) =", np.count_nonzero(Y_test_0 == 0)
for C in (0.001, 0.01, 0.1, 1, 10):
svm = sklearn.svm.SVC(C=C, kernel="rbf", gamma=100, tol=1e-7, shrinking=True, verbose=False)
svm.fit(X_train, Y_train_0)
print "----------------------------------------"
print "C =", C
support = svm.support_
coef = svm.dual_coef_[0]
b = svm.intercept_[0]
print "nSV =", len(support)
Y_predict = svm.predict(X_test)
print "in the prediction:"
print "n(+) =", np.count_nonzero(Y_predict == 1), "n(-) =", np.count_nonzero(Y_predict == 0)
print "E_out =", np.count_nonzero(Y_test_0 != Y_predict)
print
fig = plt.figure()
plt.suptitle("C =" + str(C))
plt.subplot(311)
plt.title("Training data: green +, red -")
plot_01(X_train, Y_train_0)
plt.tick_params(axis="x", labelbottom="off")
plt.subplot(312)
plt.title("Prediction on test data: green +, red -")
plot_01(X_test, Y_predict)
plt.tick_params(axis="x", labelbottom="off")
plt.subplot(313)
plt.title("Support vectors: blue")
plt.plot(X_train[:, 0], X_train[:, 1], "r.")
plt.plot(X_train[support, 0], X_train[support, 1], "b.")
plt.show()
开发者ID:huayue21,项目名称:Machine-Learning-Techniques-NTU,代码行数:52,代码来源:hw1q15.py
示例15: testSVM
def testSVM(svm,zero,one):
numcorrect = 0
numwrong = 0
for correct,testing in ((0,zero),(1,one)):
for d in testing:
import pdb;pdb.set_trace()
r = svm.predict(d)[0]
if(r==correct):
numcorrect += 1
else:
numwrong += 1
print "Correct",numcorrect
print "Wrong",numwrong
开发者ID:ictmaster,项目名称:svm,代码行数:13,代码来源:Example.py
示例16: runSVM
def runSVM(self):
"""
Runs the SVM on 5 different splits of cross validation data
"""
for train, test in self.kf:
svm = self.models["SVM"]
train_set, train_labels = self.getCurrFoldTrainData(train)
test_set, test_labels = self.getCurrFoldTestData(test)
svm.fit(train_set, train_labels)
preds = svm.predict(test_set)
acc = self.getAccuracy(test_labels, preds)
print "(SVM) Percent correct is", acc
开发者ID:urielmandujano,项目名称:ensemble_santander,代码行数:14,代码来源:ensemble.py
示例17: test_svm
def test_svm(svm, testing_dict, name):
num_correct = 0
num_wrong = 0
for correct, testing in testing_dict.items():
for test in testing:
r = svm.predict(test)[0]
if r == correct:
num_correct += 1
else:
num_wrong += 1
print("\n{1} - Correct:{0}".format(num_correct, name), end="")
print("\n{1} - Wrong:{0}".format(num_wrong, name), end="")
accuracy = float(num_correct)/(num_correct+num_wrong)*100
print("\n{1} - Accuracy:{0:.2f}%".format(round(accuracy,2), name), end="")
开发者ID:ictmaster,项目名称:svm,代码行数:14,代码来源:run.py
示例18: plotSVM
def plotSVM(svm, n, title):
X = np.array(training_0[plot_num:] +
training_1[plot_num:] +
training_2[plot_num:])
colors = np.array(["g" for i in training_2d_0][plot_num:] +
["r" for i in training_2d_1][plot_num:] +
["b" for i in training_2d_2][plot_num:])
plt.subplot(2, 2, n)
Z = svm.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap = plt.cm.Paired, alpha = 0.8)
plt.scatter(X[:, 0], X[:, 1], c = colors, cmap = plt.cm.Paired)
plt.title(title)
开发者ID:jtfidje,项目名称:ikt441_assignment3,代码行数:16,代码来源:AI.py
示例19: trainTest
def trainTest():
data2010, labels2010 = read_tac('2010')
data2011, labels2011 = read_tac("2011")
#classifiers
gnb = naive_bayes.GaussianNB()
svm = svm.SVC(kernel = "linear")
logReg = linear_model.LogisticRegression()
gnb.fit(data2010, labels2010)
svm.fit(data2010, labels2010)
logReg.fit(data2010, labels2010)
gnbPrediction = gnb.predict(data2011)
svmPrediction = svm.predict(data2011)
logRegPrediction = logReg.predict(data2011)
gnbAccuracy = accuracy(labels2011, gnbPrediction)
svmAccuracy = accuracy(labels2011, svmPrediction)
logRegAccuracy = accuracy(labels2011, logRegPrediction)
confusionMatrix = metrics.confusion_matrix(labels2011, logRegPrediction)
print "Results:"
print "Gaussian Naive Bayes: "
print gnbAccuracy
print "Support Vector Machine: "
print svmAccuracy
print "Logistic Regression: "
print logRegAccuracy
print confusionMatrix
fh.write("Results:" + "\n")
fh.write("Gaussian Naive Bayes: " + "\n")
fh.write(gnbAccuracy + "\n")
fh.write("Support Vector Machine: " + "\n")
fh.write(svmAccuracy + "\n")
fh.write("Logistic Regression: " + "\n")
fh.write(logRegAccuracy + "\n")
for i in confusionMatrix:
fh.write(str(i))
fh.write("\n")
fh.write("-------------------------------------------------\n")
fh.write("\n\n")
开发者ID:daveguy,项目名称:Comp599,代码行数:46,代码来源:a1.py
示例20: hw1q16
def hw1q16():
print "----------------------------------------"
print " Homework 1 Question 16 "
print "----------------------------------------"
# polynomial kernel: (coef0 + gamma * x1.T * x2) ** degree
for idx in (0, 2, 4, 6, 8):
svm = sklearn.svm.SVC(
C=0.01, kernel="poly", degree=2, gamma=1, coef0=1, tol=1e-4, shrinking=True, verbose=False
)
Y_train_i = (Y_train == idx).astype(int)
svm.fit(X_train, Y_train_i)
Y_predict_i = svm.predict(X_train)
support = svm.support_
coef = svm.dual_coef_[0]
b = svm.intercept_[0]
E_in = np.count_nonzero(Y_train_i != Y_predict_i)
print "For class %d:" % (idx)
print "sum(alpha) =", np.sum(np.abs(coef))
print "b =", b
print "E_in =", E_in
fig = plt.figure()
# plt.suptitle('%d vs rest' % (idx))
plt.subplot(311)
plt.title("Training data: green +, red -")
plot_01(X_train, Y_train_i)
plt.tick_params(axis="x", labelbottom="off")
plt.subplot(312)
plt.title("Prediction: green +, red -")
plot_01(X_train, Y_predict_i)
plt.tick_params(axis="x", labelbottom="off")
plt.subplot(313)
plt.title("Support vectors: blue")
plt.plot(X_train[:, 0], X_train[:, 1], "r.")
plt.plot(X_train[support, 0], X_train[support, 1], "b.")
plt.show()
开发者ID:huayue21,项目名称:Machine-Learning-Techniques-NTU,代码行数:45,代码来源:hw1q15.py
注:本文中的sklearn.svm.predict函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论