本文整理汇总了Python中sklearn.multiclass.OneVsOneClassifier.predict方法的典型用法代码示例。如果您正苦于以下问题:Python OneVsOneClassifier.predict方法的具体用法?Python OneVsOneClassifier.predict怎么用?Python OneVsOneClassifier.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.multiclass.OneVsOneClassifier
的用法示例。
在下文中一共展示了OneVsOneClassifier.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ovo_partial_fit_predict
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def test_ovo_partial_fit_predict():
X, y = shuffle(iris.data, iris.target)
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(X[:100], y[:100], np.unique(y))
ovo1.partial_fit(X[100:], y[100:])
pred1 = ovo1.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
ovo2.fit(X, y)
pred2 = ovo2.predict(X)
assert_equal(len(ovo1.estimators_), n_classes * (n_classes - 1) / 2)
assert_greater(np.mean(y == pred1), 0.65)
assert_almost_equal(pred1, pred2)
# Test when mini-batches don't have all target classes
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(iris.data[:60], iris.target[:60], np.unique(iris.target))
ovo1.partial_fit(iris.data[60:], iris.target[60:])
pred1 = ovo1.predict(iris.data)
ovo2 = OneVsOneClassifier(MultinomialNB())
pred2 = ovo2.fit(iris.data, iris.target).predict(iris.data)
assert_almost_equal(pred1, pred2)
assert_equal(len(ovo1.estimators_), len(np.unique(iris.target)))
assert_greater(np.mean(iris.target == pred1), 0.65)
示例2: test_ovo_partial_fit_predict
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def test_ovo_partial_fit_predict():
temp = datasets.load_iris()
X, y = temp.data, temp.target
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(X[:100], y[:100], np.unique(y))
ovo1.partial_fit(X[100:], y[100:])
pred1 = ovo1.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
ovo2.fit(X, y)
pred2 = ovo2.predict(X)
assert_equal(len(ovo1.estimators_), n_classes * (n_classes - 1) / 2)
assert_greater(np.mean(y == pred1), 0.65)
assert_almost_equal(pred1, pred2)
# Test when mini-batches have binary target classes
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(X[:60], y[:60], np.unique(y))
ovo1.partial_fit(X[60:], y[60:])
pred1 = ovo1.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
pred2 = ovo2.fit(X, y).predict(X)
assert_almost_equal(pred1, pred2)
assert_equal(len(ovo1.estimators_), len(np.unique(y)))
assert_greater(np.mean(y == pred1), 0.65)
ovo = OneVsOneClassifier(MultinomialNB())
X = np.random.rand(14, 2)
y = [1, 1, 2, 3, 3, 0, 0, 4, 4, 4, 4, 4, 2, 2]
ovo.partial_fit(X[:7], y[:7], [0, 1, 2, 3, 4])
ovo.partial_fit(X[7:], y[7:])
pred = ovo.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
pred2 = ovo2.fit(X, y).predict(X)
assert_almost_equal(pred, pred2)
# raises error when mini-batch does not have classes from all_classes
ovo = OneVsOneClassifier(MultinomialNB())
error_y = [0, 1, 2, 3, 4, 5, 2]
message_re = escape("Mini-batch contains {0} while "
"it must be subset of {1}".format(np.unique(error_y),
np.unique(y)))
assert_raises_regexp(ValueError, message_re, ovo.partial_fit, X[:7],
error_y, np.unique(y))
# test partial_fit only exists if estimator has it:
ovr = OneVsOneClassifier(SVC())
assert_false(hasattr(ovr, "partial_fit"))
示例3: test_ovo_string_y
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def test_ovo_string_y():
# Test that the OvO doesn't mess up the encoding of string labels
X = np.eye(4)
y = np.array(['a', 'b', 'c', 'd'])
ovo = OneVsOneClassifier(LinearSVC())
ovo.fit(X, y)
assert_array_equal(y, ovo.predict(X))
示例4: test_ovo_string_y
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def test_ovo_string_y():
"Test that the OvO doesn't screw the encoding of string labels"
X = np.eye(4)
y = np.array(['a', 'b', 'c', 'd'])
svc = LinearSVC()
ovo = OneVsOneClassifier(svc)
ovo.fit(X, y)
assert_array_equal(y, ovo.predict(X))
示例5: multiclassSVC
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def multiclassSVC(classifier, sz=2000):
mnsize = sz
df = hw6u.load_mnist_features(mnsize)
data = utils.pandas_to_data(df)
k = 10
all_folds = hw3u.partition_folds(data, k)
kf_train, kf_test = dl.get_train_and_test(all_folds, 0)
y, X = hw4u.split_truth_from_data(kf_train, replace_zeros=False)
y, X = np.asarray(y), np.asarray(X)
y_test, X_test = hw4u.split_truth_from_data(kf_test, replace_zeros=False)
y_test, X_test = np.asarray(y_test), np.asarray(X_test)
print 'Beginning analysis: {}'.format(X.shape)
#clf = OneVsRestClassifier(classifier, n_jobs=4).fit(X, y)
clf = OneVsOneClassifier(classifier).fit(X, y)
#clf = OutputCodeClassifier(LinearSVC(random_state=0), code_size=10, random_state=0).fit(np.asarray(X), y)
y_pred = clf.predict(X)
print 'train acc: {} test acc: {}'.format(accuracy_score(fix_y(y_pred), fix_y(y)), accuracy_score(fix_y(y_test), fix_y(clf.predict(X_test))))
print 'train acc: {} test acc: {}'.format(accuracy_score(fix_y(clf.predict(X)), fix_y(y)), accuracy_score(fix_y(y_test), fix_y(clf.predict(X_test))))
示例6: OneVsOne
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def OneVsOne(inputs_train, inputs_valid, target_train, target_valid):
name = "Multiclass One Vs One"
clf = OneVsOneClassifier(LinearSVC(random_state=0))
clf.fit(inputs_train, np.ravel(target_train))
prediction = clf.predict(inputs_valid)
correct = np.count_nonzero(np.ravel(target_valid) == prediction)
total = target_valid.shape[0]
correctRate = (float(correct)/total)*100
return name, correctRate
示例7: test_ovo_decision_function
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
def test_ovo_decision_function():
n_samples = iris.data.shape[0]
ovo_clf = OneVsOneClassifier(LinearSVC(random_state=0))
# first binary
ovo_clf.fit(iris.data, iris.target == 0)
decisions = ovo_clf.decision_function(iris.data)
assert_equal(decisions.shape, (n_samples,))
# then multi-class
ovo_clf.fit(iris.data, iris.target)
decisions = ovo_clf.decision_function(iris.data)
assert_equal(decisions.shape, (n_samples, n_classes))
assert_array_equal(decisions.argmax(axis=1), ovo_clf.predict(iris.data))
# Compute the votes
votes = np.zeros((n_samples, n_classes))
k = 0
for i in range(n_classes):
for j in range(i + 1, n_classes):
pred = ovo_clf.estimators_[k].predict(iris.data)
votes[pred == 0, i] += 1
votes[pred == 1, j] += 1
k += 1
# Extract votes and verify
assert_array_equal(votes, np.round(decisions))
for class_idx in range(n_classes):
# For each sample and each class, there only 3 possible vote levels
# because they are only 3 distinct class pairs thus 3 distinct
# binary classifiers.
# Therefore, sorting predictions based on votes would yield
# mostly tied predictions:
assert_true(set(votes[:, class_idx]).issubset(set([0., 1., 2.])))
# The OVO decision function on the other hand is able to resolve
# most of the ties on this data as it combines both the vote counts
# and the aggregated confidence levels of the binary classifiers
# to compute the aggregate decision function. The iris dataset
# has 150 samples with a couple of duplicates. The OvO decisions
# can resolve most of the ties:
assert_greater(len(np.unique(decisions[:, class_idx])), 146)
示例8: Imputer
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
#Fill missing values
imp = Imputer(missing_values='NaN',strategy='most_frequent',axis=0)
vec_f_cat_train = imp.fit_transform(vec_f_cat_train)
vec_f_cat_test = imp.transform(vec_f_cat_test)
# complete x
f_train = np.hstack((f_num_train, vec_f_cat_train ))
f_test = np.hstack((f_num_test, vec_f_cat_test ))
clf = OneVsOneClassifier(LinearSVC(random_state=0))
clf.fit(f_train,t_train)
y_test = clf.predict(f_test)
y_train = clf.predict(f_train)
y_test = le.inverse_transform(y_test)
ofile = open(data_dir+'output.csv','w')
writer = csv.writer(ofile)
writer.writerow(['ISIN','Risk_Stripe'])
for i in range(len(x_test)):
writer.writerow([x_test[i],y_test[i]])
ofile.close()
示例9: allSamples
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
score.append(multiclassifier.score(X_test, Y_test))
# svc = svm.SVC(kernel='linear', C=C).fit(X_train, Y_train)
# rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X_train, Y_train)
# poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X_train, Y_train)
# print score
# plt.figure("Score-order")
# plt.plot(slips,score)
# plt.show()
#Use validate data to test the model.
path = "./"
label_dict = {"test":0}
X_test,Y_test = allSamples(path,"test",label_dict,order,window,slip)
#print X_test,Y_test
print multiclassifier.predict(X_test)
print multiclassifier.score(X_test,Y_test)
#X_test = dim_reduction_PCA(X_test,0.99)
#plot_data(X_test,Y_test,"PLOT",mirror=1)
# y = pandas.Series(x)
# plt.figure()
# lag_plot(y,marker='+',color='gray')
# plt.show()
# autor = estimated_autocorrelation(x)
# autor = autor[autor.size/2:]
#
# timestamp = 1order5order397880668
# date = datetime.datetime.fromtimestamp(timestamp/1e3)
# datetime.datetime(2016, 2, 2, 8, 2order, order0, 668000)
示例10: Build_Data_Set
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
citation_data = citation_data.loc[citation_data['paper_year'] >= 1996]
citation_data.iloc[np.random.permutation(len(citation_data))]
X = np.array(citation_data[features].values)
#X = preprocessing.scale(X)
y = (citation_data["paper_cat"].values.tolist())
return X,y
test_X, test_y = Build_Data_Set()
"""
classifier_f = open("./workspace/rbfsvr.pickle", "rb")
clf= pickle.load(classifier_f)
classifier_f.close()
"""
print("Prediction started")
prediction = clf.predict(test_X)
from sklearn.metrics import confusion_matrix
confusion_matrix(test_y, prediction)
pearson = np.corrcoef(prediction, test_y)
spearman = spearmanr(prediction, test_y)
print("Results : rbf Kernel ")
print(pearson)
print(spearman)
"""
示例11: OneVsOneClassifier
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
from sklearn.svm import LinearSVC
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
from numpy import *
import datasets
if not datasets.Quizbowl.loaded:
datasets.loadQuizbowl()
print '\n\nRUNNING ON EASY DATA\n'
print 'training oaa'
X = datasets.QuizbowlSmall.X
Y = datasets.QuizbowlSmall.Y
oaa = OneVsOneClassifier(LinearSVC(random_state=0)).fit(X, Y)
print 'predicting oaa'
oaaDevPred = oaa.predict(datasets.QuizbowlSmall.Xde)
print 'error = %g' % mean(oaaDevPred != datasets.QuizbowlSmall.Yde)
print 'training ava'
ava = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X, Y)
print 'predicting ava'
avaDevPred = ava.predict(datasets.QuizbowlSmall.Xde)
print 'error = %g' % mean(avaDevPred != datasets.QuizbowlSmall.Yde)
print '\n\nRUNNING ON HARD DATA\n'
print 'training oaa'
X = datasets.QuizbowlHardSmall.X
Y = datasets.QuizbowlHardSmall.Y
oaa = OneVsOneClassifier(LinearSVC(random_state=0)).fit(X, Y)
print 'predicting oaa'
示例12: EveryWordOneFeature
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
class EveryWordOneFeature(object):
def __init__(self, slack=1, gamma=1, kernelType='linear', gram=1):
self.gram = gram
self.slack = slack
self.gamma = gamma
self.kernelType = kernelType
self.data = np.ones((1000, 1000))
self.cityClassifier = {}
#TODO: Wieso nimmst du OneVsOne und nicht OneVsRest? Ginge OneVsRest nicht schneller?
self.countryClassifier = OneVsOneClassifier(
svm.SVC(kernel=self.kernelType, C=self.slack, gamma=self.gamma, probability=False,
cache_size=1000))
self.bag = None
self.numberOfFeatures = 0
#Features and labels
self.fitting_data = None
self.predict_data = None
self.cityPrediction = {}
self.countryPrediction = None
self.numberOfCityFeatures = {}
def fit_cities(self, trainingData, labels, countryCode):
print "Start fitting cities for country " + str(countryCode)
#TODO: Wieso nimmst du OneVsOne und nicht OneVsRest? Ginge OneVsRest nicht schneller?
self.cityClassifier[countryCode] = OneVsOneClassifier(
svm.SVC(kernel=self.kernelType, C=self.slack, gamma=self.gamma, probability=False))
start = time.time()
self.cityClassifier[countryCode].fit(trainingData[:, :self.get_number_of_city_features(countryCode)],
labels)
end = time.time()
print "Finished fitting cities in " + str((end - start)) + "s"
def fit_countries(self):
print "Start Fitting countries"
start = time.time()
self.countryClassifier.fit(self.fitting_data[:, :self.numberOfFeatures],
self.fitting_data[:, (self.numberOfFeatures + 1)])
end = time.time()
print "Finished fitting countries in " + str((end - start)) + "s"
def preprocess_training_data(self, data):
startOfPreprocessing = time.time()
print "Start Preprocessing"
lengthOfTrainingData = self.data.shape[0]
print "length of trainingData = " + str(lengthOfTrainingData)
self.bag = BagOfWords(data)
self.fitting_data = self.bag.get_features_and_labels()
self.numberOfFeatures = self.fitting_data.shape[1] - 2
startOfFittingCities = time.time()
print "Finished Preprocessing in " + str((startOfFittingCities - startOfPreprocessing)) + "s"
def fit(self, data):
self.data = data
self.preprocess_training_data(data)
self.numberOfFeatures = self.fitting_data.shape[1] - 2
self.fit_countries()
def predict_cities(self, data, countryCode):
print "Start predict cities"
start = time.time()
print data[:, :self.numberOfFeatures]
print self.cityPrediction
self.cityPrediction[countryCode] = self.cityClassifier[countryCode].predict(data[:, :self.get_number_of_city_features(countryCode)])
end = time.time()
print "Finished predicting cities in " + str((end - start)) + "s"
def predict_countries(self):
start = time.time()
print "start predicting countries"
print self.predict_data
self.countryPrediction = self.countryClassifier.predict(self.predict_data[:, :self.numberOfFeatures])
end = time.time()
print "finished predicting countries in " + str((end - start)) + "s"
def preprocess_predict_data(self, predict):
self.predict_data = self.bag.get_get_validation_features(predict)
def get_city_featuers(self, data, countryCode):
return np.zeros((data.shape[0], 3))
def predict(self, predict):
self.preprocess_predict_data(predict)
self.numberOfFeatures = self.predict_data.shape[1]
# t1 = threading.Thread(target=self.predict_cities)
self.predict_countries()
joinedCityPredictions = np.zeros(predict.shape[0])
countryCodes = np.unique(self.data[:, 2].astype(int))
for countryCode in countryCodes:
countryIndices = np.where(self.data[:, 2].astype(int) == countryCode)[0]
self.fit_cities(self.get_city_featuers(self.data[countryIndices][:, 0],countryCode), self.data[countryIndices][:,1], countryCode)
countryCodes = np.unique(self.countryPrediction)
for countryCode in countryCodes:
#.........这里部分代码省略.........
示例13: train_test_split
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
x_train,x_valid,y_train,y_valid = train_test_split(X,y,test_size=0.3,random_state=None)
# Train classifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
clf = OneVsOneClassifier(GradientBoostingClassifier(n_estimators=50,verbose=100))
clf.fit(x_train,y_train)
# Run Predictions
from sklearn.metrics import confusion_matrix, accuracy_score
y_preds = clf.predict(x_valid)
print( confusion_matrix(y_valid,y_preds) );
print( "Accuracy: %f" % (accuracy_score(y_valid,y_preds)) );
f = open('OneVsOne_gradientBoost_take2.txt', 'w')
f.write( str(confusion_matrix(y_valid,y_preds)) );
f.write( "\nAccuracy: %f" % (accuracy_score(y_valid,y_preds)) );
f.write( "\nOneVsOneClassifier(...)" );
f.write( "\nclf = GradientBoostingClassifier(n_estimators=50,verbose=100)" );
# Now on to final submission
X_test = testing.iloc[:,1:]
y_final = pd.DataFrame(clf.predict(X_test).reshape([62096,]));
numbahs = testing['id']
df = pd.concat([numbahs,y_final],axis=1)
df.columns = ['id','country']
示例14: print
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
print(">>>> Loading finished")
feature_vec = np.zeros((len(data), kmeans.n_clusters))
for i in range(len(data)):
mydata = data[i]
# mydata = pca.transform(mydata)
feature_seq = kmeans.predict(mydata)
for j in feature_seq:
feature_vec[i][feature_seq[j]] += 1
feature_vec = normalize(feature_vec)
train_x, test_x, train_y, test_y = \
train_test_split(feature_vec, all_y, test_size = 1-train_ratio)
print(feature_vec.shape)
print(">>>> Data prepared")
# for alpha_ in [0.1, 0.01, 0.02, 0.03, 0.05, 0.008, 0.009, 0.006, 0.005]:
for alpha_ in [0.0001]:
clf = OneVsOneClassifier(linear_model.SGDClassifier(alpha = alpha_, n_iter=150000, shuffle=True), n_jobs=4)
clf.fit(train_x, train_y)
print(" alpha", alpha_)
print(" train score", clf.score(train_x, train_y))
print(" test score", clf.score(test_x, test_y))
print(clf)
pred_y = clf.predict(test_x)
print(test_x[:2,:5])
print(pred_y)
示例15: OneVsOneClassifier
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import predict [as 别名]
ts = np.genfromtxt(ts_path, delimiter=' ')
tr_feat = tr[:,1:]
ts_feat = ts[:,1:]
tr_label = tr[:,0]
ts_label = ts[:,0]
# use sklearn C-Support Vector Classification
## == one-vs-one == ##
# The multiclass support is handled in a one-vs-one scheme
# train
ovo_clf = OneVsOneClassifier(LinearSVC())
ovo_clf.fit(tr_feat, tr_label)
# predict
ovo_pred = ovo_clf.predict(ts_feat)
ovo_err = 1- ovo_clf.score(ts_feat, ts_label)
# confusion matrix
#
#array([[159, 7],
# [ 5, 161]])
ovo_cmat = metrics.confusion_matrix(ts_label, ovo_pred)
pred_total = np.sum(ovo_cmat,axis = 1)
ovo_mis = 1- np.diag(ovo_cmat).astype(float) / pred_total
print("one vs. one svm - classification err: %s \n"%(ovo_err))
print("confusion matrix: \n %s"%(ovo_cmat))
print("class misclassification rate : \n %s"%(ovo_mis))
## == one-vs-rest == ##
# The multiclass support is handled in a one-vs-rest scheme
# train