本文整理汇总了Python中sklearn.metrics.hinge_loss函数的典型用法代码示例。如果您正苦于以下问题:Python hinge_loss函数的具体用法?Python hinge_loss怎么用?Python hinge_loss使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了hinge_loss函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: check_lambda
def check_lambda(datanm, samples_per_class, Cs, num_classes, gamma, num_iter = 100, kernel = 'linear', strat = 'ovr'):
data, labels = load_full(datanm, samples_per_class)
slo = StratifiedShuffleSplit(labels, n_iter=num_iter, test_size=0.3, train_size=0.7, random_state=None)
ans = np.zeros((len(Cs), len(gamma), 4))
for train_index, test_index in slo:
train_data = [data[train_index, :], labels[train_index]]
valid_data = [data[test_index , :], labels[test_index ]]
for j, g in enumerate(gamma):
for i, C in enumerate(Cs):
clf = svm.SVC(C=C, kernel=kernel, degree=3, gamma=g, coef0=0.0, shrinking=True,
probability=False, tol=0.001, cache_size=10000, class_weight=None,
verbose=False, max_iter=-1, decision_function_shape=strat, random_state=None)
clf.fit(train_data[0], train_data[1])
out_train = clf.decision_function(train_data[0])
out_valid = clf.decision_function(valid_data[0])
ans[i, j, 2] += hinge_loss(train_data[1], out_train, range(num_classes))
ans[i, j, 3] += hinge_loss(valid_data[1], out_valid, range(num_classes))
#ans[i, j, 0] += log_loss(train_data[1], clf.predict_proba(train_data[0]))
#ans[i, j, 1] += log_loss(valid_data[1], clf.predict_proba(valid_data[0]))
ans[:, :, :] /= num_iter
np.savez("svm_lambda_" + kernel + '_' + strat, ans= ans, Cs = Cs, num_iter = num_iter, num_classes = num_classes, samples_per_class = samples_per_class)
return ans
开发者ID:dmitro-nazarenko,项目名称:chemfin-open,代码行数:28,代码来源:svm.py
示例2: learning_curve
def learning_curve(classifier, y, train, cv, n=15):
"""Plot train and cv loss for increasing train sample sizes."""
chunk = int(len(y)/n)
n_samples = []
train_losses = []
cv_losses = []
previous_cache_dir = classifier.cache_dir
classifier.cache_dir = "diagnostics"
for i in range(n):
train_subset = train[:(i + 1)*chunk]
preds_cv = classifier.fit_predict(y, train_subset, cv,
show_steps=False)
preds_train = classifier.fit_predict(y, train_subset, train_subset,
show_steps=False)
n_samples.append((i + 1)*chunk)
cv_losses.append(hinge_loss(y[cv], preds_cv, neg_label=0))
train_losses.append(hinge_loss(y[train_subset], preds_train,
neg_label=0))
classifier.cache_dir = previous_cache_dir
plt.clf()
plt.plot(n_samples, train_losses, 'r--', n_samples, cv_losses, 'b--')
plt.ylim([min(train_losses) - .01, max(cv_losses) + .01])
plt.savefig('plots/learning_curve.png')
plt.show()
开发者ID:BabelTower,项目名称:amazonaccess,代码行数:27,代码来源:diagnostics.py
示例3: test_hinge_loss_binary
def test_hinge_loss_binary():
y_true = np.array([-1, 1, 1, -1])
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
assert_equal(hinge_loss(y_true, pred_decision), 1.2 / 4)
y_true = np.array([0, 2, 2, 0])
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
assert_equal(hinge_loss(y_true, pred_decision), 1.2 / 4)
开发者ID:nateyoder,项目名称:scikit-learn,代码行数:8,代码来源:test_classification.py
示例4: test_hinge_loss_binary
def test_hinge_loss_binary():
y_true = np.array([-1, 1, 1, -1])
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
assert_equal(1.2 / 4, hinge_loss(y_true, pred_decision))
y_true = np.array([0, 2, 2, 0])
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
assert_equal(1.2 / 4, hinge_loss(y_true, pred_decision, pos_label=2, neg_label=0))
开发者ID:kmike,项目名称:scikit-learn,代码行数:8,代码来源:test_metrics.py
示例5: test_hinge_loss_binary
def test_hinge_loss_binary():
y_true = np.array([-1, 1, 1, -1])
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
assert_equal(1.2 / 4, hinge_loss(y_true, pred_decision))
with warnings.catch_warnings():
# Test deprecated pos_label
assert_equal(hinge_loss(-y_true, pred_decision), hinge_loss(y_true, pred_decision, pos_label=-1, neg_label=1))
y_true = np.array([0, 2, 2, 0])
pred_decision = np.array([-8.5, 0.5, 1.5, -0.3])
assert_equal(1.2 / 4, hinge_loss(y_true, pred_decision))
with warnings.catch_warnings():
# Test deprecated pos_label
assert_equal(1.2 / 4, hinge_loss(y_true, pred_decision, pos_label=2, neg_label=0))
开发者ID:Clstone,项目名称:scikit-learn,代码行数:15,代码来源:test_metrics.py
示例6: validation_metric_vw
def validation_metric_vw(self):
v = open('%s' % self.holdout_pred, 'r')
y_pred_holdout = []
for line in v:
y_pred_holdout.append(float(line.split()[0].strip()))
if self.outer_loss_function == 'logistic':
y_pred_holdout_proba = [1. / (1 + exp(-i)) for i in y_pred_holdout]
loss = log_loss(self.y_true_holdout, y_pred_holdout_proba)
elif self.outer_loss_function == 'squared':
loss = mean_squared_error(self.y_true_holdout, y_pred_holdout)
elif self.outer_loss_function == 'hinge':
loss = hinge_loss(self.y_true_holdout, y_pred_holdout)
elif self.outer_loss_function == 'pr-auc':
loss = -average_precision_score(self.y_true_holdout, y_pred_holdout)
elif self.outer_loss_function == 'roc-auc':
y_pred_holdout_proba = [1. / (1 + exp(-i)) for i in y_pred_holdout]
fpr, tpr, _ = roc_curve(self.y_true_holdout, y_pred_holdout_proba)
loss = -auc(fpr, tpr)
self.logger.info('parameter suffix: %s' % self.param_suffix)
self.logger.info('loss value: %.6f' % loss)
return loss
开发者ID:ArunkumarRamanan,项目名称:vowpal_wabbit,代码行数:28,代码来源:vw-hyperopt.py
示例7: test_hinge_loss_multiclass_invariance_lists
def test_hinge_loss_multiclass_invariance_lists():
# Currently, invariance of string and integer labels cannot be tested
# in common invariance tests because invariance tests for multiclass
# decision functions is not implemented yet.
y_true = ['blue', 'green', 'red',
'green', 'white', 'red']
pred_decision = [
[0.36, -0.17, -0.58, -0.99],
[-0.55, -0.38, -0.48, -0.58],
[-1.45, -0.58, -0.38, -0.17],
[-0.55, -0.38, -0.48, -0.58],
[-2.36, -0.79, -0.27, 0.24],
[-1.45, -0.58, -0.38, -0.17]]
dummy_losses = np.array([
1 - pred_decision[0][0] + pred_decision[0][1],
1 - pred_decision[1][1] + pred_decision[1][2],
1 - pred_decision[2][2] + pred_decision[2][3],
1 - pred_decision[3][1] + pred_decision[3][2],
1 - pred_decision[4][3] + pred_decision[4][2],
1 - pred_decision[5][2] + pred_decision[5][3]
])
dummy_losses[dummy_losses <= 0] = 0
dummy_hinge_loss = np.mean(dummy_losses)
assert_equal(hinge_loss(y_true, pred_decision),
dummy_hinge_loss)
开发者ID:nateyoder,项目名称:scikit-learn,代码行数:25,代码来源:test_classification.py
示例8: validation_metric_vw
def validation_metric_vw(self):
y_pred_holdout = self.get_y_pred_holdout()
if self.outer_loss_function == 'logistic':
if self.labels_clf_count > 2:
y_pred_holdout_proba = y_pred_holdout
else:
y_pred_holdout_proba = [1. / (1 + exp(-i)) for i in y_pred_holdout]
loss = log_loss(self.y_true_holdout, y_pred_holdout_proba)
elif self.outer_loss_function == 'squared':
loss = mean_squared_error(self.y_true_holdout, y_pred_holdout)
elif self.outer_loss_function == 'hinge':
loss = hinge_loss(self.y_true_holdout, y_pred_holdout)
elif self.outer_loss_function == 'pr-auc':
loss = -average_precision_score(self.y_true_holdout, y_pred_holdout)
elif self.outer_loss_function == 'roc-auc':
y_pred_holdout_proba = [1. / (1 + exp(-i)) for i in y_pred_holdout]
fpr, tpr, _ = roc_curve(self.y_true_holdout, y_pred_holdout_proba)
loss = -auc(fpr, tpr)
else:
raise KeyError('Invalide outer loss function')
self.logger.info('parameter suffix: %s' % self.param_suffix)
self.logger.info('loss value: %.6f' % loss)
return loss
开发者ID:JohnLangford,项目名称:vowpal_wabbit,代码行数:31,代码来源:vw-hyperopt.py
示例9: print_metrics
def print_metrics(y_true, y_preds):
'''
Description: print out accuracy, recall, precision, hinge loss, and f1-score of model
'''
print "Accuracy: %.4g" % metrics.accuracy_score(y_true, y_preds, normalize=True)
print "Recall: %.4g" % metrics.recall_score(y_true, y_preds)
print "Precision: %.4g" % metrics.precision_score(y_true, y_preds)
print "Hinge loss: %.4g" % metrics.hinge_loss(y_true, y_preds)
print "F1 score: %.4g" % metrics.f1_score(y_true, y_preds)
开发者ID:apyeh,项目名称:BaseballHOF,代码行数:9,代码来源:Model_Building.py
示例10: check_vb
def check_vb(datanm, samples_per_class, Cs, num_classes, gamma, num_iter = 100, kernel = 'linear', strat = 'ovr'):
data, labels = load_full(datanm, samples_per_class)
slo = StratifiedShuffleSplit(labels, n_iter=num_iter, test_size=0.5, train_size=0.5, random_state=None)
ans = np.zeros((len(Cs), len(gamma), samples_per_class/2, 4))
for train_index, test_index in slo:
train_data = [data[train_index, :], labels[train_index]]
valid_data = [data[test_index , :], labels[test_index ]]
for l in xrange(samples_per_class/2):
ind_train = []
ind_valid = []
for k in xrange(num_classes):
ind_train = ind_train + np.where(train_data[1] == k)[0].tolist()[:l+1]
ind_valid = ind_valid + np.where(valid_data[1] == k)[0].tolist()[:l+1]
ctrain_data = [ train_data[0][ind_train], train_data[1][ind_train] ]
cvalid_data = [ valid_data[0][ind_valid], valid_data[1][ind_valid] ]
for i, C in enumerate(Cs):
for j, g in enumerate(gamma):
clf = svm.SVC(C=C, kernel=kernel, degree=3, gamma=g, coef0=0.0, shrinking=True,
probability=False, tol=0.001, cache_size=10000, class_weight=None,
verbose=False, max_iter=-1, decision_function_shape=strat, random_state=None)
clf.fit(ctrain_data[0], ctrain_data[1])
#out_train = clf.predict_proba(ctrain_data[0])
#out_valid = clf.predict_proba(cvalid_data[0])
#ans[i, l, 0] += log_loss(ctrain_data[1], out_train)
#ans[i, l, 1] += log_loss(cvalid_data[1], out_valid)
out_train = clf.decision_function(train_data[0])
out_valid = clf.decision_function(valid_data[0])
ans[i, j, l, 2] += hinge_loss(train_data[1], out_train, range(num_classes))
ans[i, j, l, 3] += hinge_loss(valid_data[1], out_valid, range(num_classes))
ans /= num_iter
np.savez("svm_bv_" + kernel + '_' + strat, ans= ans, Cs = Cs, num_iter = num_iter, num_classes = num_classes, samples_per_class = samples_per_class)
return ans
开发者ID:dmitro-nazarenko,项目名称:chemfin-open,代码行数:41,代码来源:svm.py
示例11: cross_validation
def cross_validation(self, model):
# kfold = cross_validation.KFold(self.train_x.shape[0], n_folds=5, shuffle=True, random_state=self.random_state)
kfold = cross_validation.StratifiedKFold(self.train_y,
n_folds=self.k_fold_,
shuffle=True,
random_state=self.random_state)
scores = {'auc':list(),
'hinge_loss':list(),
'log_loss':list(),
'accuracy':list(),
'precision':list(),
'recall':list(),
'f1_value':list()}
#scores = list()
preds = np.zeros(len(self.train_y))
i = 0
for train_idx, test_idx in kfold:
print (' --------- fold {0} ---------- '.format(i))
train_x = self.train_x.toarray()[train_idx]
train_y = self.train_y[train_idx]
test_x = self.train_x.toarray()[test_idx]
test_y = self.train_y[test_idx]
model.fit(train_x, train_y)
pred = model.predict(test_x)
score = metrics.roc_auc_score(test_y, pred)
preds[test_idx] = pred
score = metrics.roc_auc_score(test_y, pred)# auc
scores['auc'].append(score)
score = metrics.hinge_loss(test_y, pred)# hinge_loss
scores['hinge_loss'].append(score)
score = metrics.log_loss(test_y, pred)# log_loss
scores['log_loss'].append(score)
#score = metrics.accuracy_score(test_y, pred)# accuracy
#scores['accuracy'].append(score)
#score = metrics.precision_score(test_y, pred)# precision
#scores['precision'].append(score)
#score = metrics.recall_score(test_y, pred)# recall
#scores['recall'].append(score)
#score = metrics.f1_score(test_y, pred)# f_value
#scores['f1_value'].append(score)
i += 1
for key in scores.keys():
scores[key] = np.asarray(scores[key], dtype=np.float32)
#print key, scores[key].mean(), scores[key].std()
return scores, preds
开发者ID:Quasi-quant2010,项目名称:Stacking,代码行数:47,代码来源:experiment_l1.py
示例12: getResult
def getResult(self, predict, data_set):
y_true, y_predict = control.calculate_entire_ds(predict, data_set)
result = metrics.classification_report(y_true, y_predict)
result += "\nAccuracy classification: %f\n" % metrics.accuracy_score(y_true, y_predict)
result += "F1 score: %f\n" % metrics.f1_score(y_true, y_predict)
result += "Fbeta score: %f\n" % metrics.fbeta_score(y_true, y_predict, beta=0.5)
result += "Hamming loss: %f\n" % metrics.hamming_loss(y_true, y_predict)
result += "Hinge loss: %f\n" % metrics.hinge_loss(y_true, y_predict)
result += "Jaccard similarity: %f\n" % metrics.jaccard_similarity_score(y_true, y_predict)
result += "Precision: %f\n" % metrics.precision_score(y_true, y_predict)
result += "Recall: %f\n" % metrics.recall_score(y_true, y_predict)
if self.is_binary():
result += "Average precision: %f\n" % metrics.average_precision_score(y_true, y_predict)
result += "Matthews correlation coefficient: %f\n" % metrics.matthews_corrcoef(y_true, y_predict)
result += "Area Under the Curve: %f" % metrics.roc_auc_score(y_true, y_predict)
return result
开发者ID:ArsEmchik,项目名称:course_project,代码行数:18,代码来源:_interface.py
示例13: test_hinge_loss_multiclass_with_missing_labels
def test_hinge_loss_multiclass_with_missing_labels():
pred_decision = np.array([
[0.36, -0.17, -0.58, -0.99],
[-0.55, -0.38, -0.48, -0.58],
[-1.45, -0.58, -0.38, -0.17],
[-0.55, -0.38, -0.48, -0.58],
[-1.45, -0.58, -0.38, -0.17]
])
y_true = np.array([0, 1, 2, 1, 2])
labels = np.array([0, 1, 2, 3])
dummy_losses = np.array([
1 - pred_decision[0][0] + pred_decision[0][1],
1 - pred_decision[1][1] + pred_decision[1][2],
1 - pred_decision[2][2] + pred_decision[2][3],
1 - pred_decision[3][1] + pred_decision[3][2],
1 - pred_decision[4][2] + pred_decision[4][3]
])
dummy_losses[dummy_losses <= 0] = 0
dummy_hinge_loss = np.mean(dummy_losses)
assert_equal(hinge_loss(y_true, pred_decision, labels=labels),
dummy_hinge_loss)
开发者ID:nateyoder,项目名称:scikit-learn,代码行数:21,代码来源:test_classification.py
示例14: test_hinge_loss_multiclass
def test_hinge_loss_multiclass():
pred_decision = np.array([
[0.36, -0.17, -0.58, -0.99],
[-0.54, -0.37, -0.48, -0.58],
[-1.45, -0.58, -0.38, -0.17],
[-0.54, -0.38, -0.48, -0.58],
[-2.36, -0.79, -0.27, 0.24],
[-1.45, -0.58, -0.38, -0.17]
])
y_true = np.array([0, 1, 2, 1, 3, 2])
dummy_losses = np.array([
1 - pred_decision[0][0] + pred_decision[0][1],
1 - pred_decision[1][1] + pred_decision[1][2],
1 - pred_decision[2][2] + pred_decision[2][3],
1 - pred_decision[3][1] + pred_decision[3][2],
1 - pred_decision[4][3] + pred_decision[4][2],
1 - pred_decision[5][2] + pred_decision[5][3]
])
dummy_losses[dummy_losses <= 0] = 0
dummy_hinge_loss = np.mean(dummy_losses)
assert_equal(hinge_loss(y_true, pred_decision),
dummy_hinge_loss)
开发者ID:nateyoder,项目名称:scikit-learn,代码行数:22,代码来源:test_classification.py
示例15: evaluate
def evaluate(estimator, dev_X, dev_y):
print('evaluating on development set', flush=True)
guess_dev = estimator.predict(dev_X)
score_roc_auc_dev = roc_auc_score(dev_y, guess_dev)
print('{:.4f} -- roc auc'.format(score_roc_auc_dev))
score_brier_loss_dev = brier_score_loss(dev_y, guess_dev)
print('{:.4f} -- brier loss'.format(score_brier_loss_dev))
score_log_loss_dev = log_loss(dev_y, estimator.predict_proba(dev_X))
print('{:.4f} -- log loss'.format(score_log_loss_dev))
guess_dev_negative_one = guess_dev.copy().astype('int8')
guess_dev_negative_one[guess_dev_negative_one == 0] = -1
'''
decision_fuction not implemented
# score_hinge_loss_dev = hinge_loss(dev_y, estimator.decision_function(dev_X))
'''
score_hinge_loss_dev = hinge_loss(dev_y, guess_dev_negative_one)
print('{:.4f} -- hinge loss'.format(score_hinge_loss_dev))
score_matthews_corrcoef_dev = matthews_corrcoef(dev_y, guess_dev_negative_one)
print('{:.4f} -- matthews_corrcoef'.format(score_matthews_corrcoef_dev))
print(flush=True)
return score_roc_auc_dev, score_brier_loss_dev,\
score_log_loss_dev, score_hinge_loss_dev, score_matthews_corrcoef_dev
开发者ID:mguo001,项目名称:Data-Science-Intensive,代码行数:23,代码来源:Capstone_Kaggle_Project.py
示例16: print
param_grid={"C": [0.001,0.01,0.1,1,10,100]},n_jobs=8)
Clf.fit(TrainFvs,TrainLabels)
PredictedLabels = Clf.predict(TestFvs)
print '*'*100
print 'classification report'
print '-'*20
Accuracy = np.mean(PredictedLabels == TestLabels)
print "Test Set Accuracy = ", Accuracy
print(metrics.classification_report(TestLabels,
PredictedLabels, target_names=['Neg', 'Pos']))
print "Accuracy classification score:", metrics.accuracy_score(TestLabels, PredictedLabels)
print "Hamming loss:", metrics.hamming_loss(TestLabels, PredictedLabels)
print "Average hinge loss:", metrics.hinge_loss(TestLabels, PredictedLabels)
print "Log loss:", metrics.log_loss(TestLabels, PredictedLabels)
print "F1 Score:", metrics.f1_score(TestLabels, PredictedLabels)
print "Zero-one classification loss:", metrics.zero_one_loss(TestLabels, PredictedLabels)
print '*'*100
print 'total vocab size: {} '.format(len(model.vocab.keys()))
# for k,v in model.vocab.iteritems():
# print k
# print v
# raw_input()
开发者ID:MLDroid,项目名称:SentimentAnalysisInt,代码行数:30,代码来源:Doc2Vec.py
示例17: main
def main():
# if sys.argv[2] == 'svm':
# Clf = LinearSVC(C = 0.1, class_weight = 'balanced',max_iter=100)
# elif sys.argv[2] == 'lr':
# Clf = LogisticRegression (C=0.1,max_iter=100,n_jobs=8)
# elif sys.argv[2] == 'pa':
# Clf = PassiveAggressiveClassifier(C=0.1,n_iter=1,n_jobs=8,class_weight='balanced')
# else:
# Clf = SGDClassifier(n_iter=1,n_jobs=8,class_weight='balanced')
Clf = LinearSVC(C = 0.1, class_weight = 'balanced',max_iter=100)
Clf = LogisticRegression (C=0.1,max_iter=1000,n_jobs=8,class_weight='balanced')
Clf = GridSearchCV(LogisticRegression(max_iter=1000,n_jobs=8,class_weight='balanced'), cv=5,
param_grid={"C": [0.001,0.01,0.1,1,10,100]},n_jobs=8)
# Clf = GridSearchCV(LinearSVC(C = 0.1, class_weight = 'balanced',max_iter=1000), cv=3,
# param_grid={"C": [0.001,0.01,0.1,1,10,100]},n_jobs=8)
File = '/home/annamalai/Senti/UCI/amazon_cells_labelled.txt'
Ngram = 2
print 'Clf: {}, File: {}, ngram: {}'.format(Clf, File, Ngram)
PosSamples = [l.split('\t')[0].strip() for l in open (File).xreadlines() if l.strip().endswith('1')]#[:100]
NegSamples = [l.split('\t')[0].strip() for l in open (File).xreadlines() if l.strip().endswith('0')]#[:100]
print 'loaded {} pos and {} neg samples'.format(len(PosSamples), len(NegSamples))
X = PosSamples + NegSamples
y = [1 for _ in xrange(len(PosSamples))] + [-1 for _ in xrange (len(NegSamples))]
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.1,
random_state=random.randint(0,100))
print '# TrainLabels', len(y_train)
print '# TestLabels', len(y_test)
print 'performing CVectorizer'
CVectorizer = CountVectorizer(lowercase = True,
stop_words='english',
# token_pattern='(?u)\b\w\w+\b',
# tokenizer = SGTokenizer,
tokenizer = Tokenizer,
ngram_range=(1,2),
dtype=np.float64,
decode_error = 'ignore',
max_df=0.8)
print 'performing TfidfTransformer and Normalizer'
# TFIDFTransformer = TfidfTransformer()
normalizer = Normalizer()
print 'creating Train and Test FVs'
T0 = time()
TrainFVs = CVectorizer.fit_transform(X_train)
TestFVs = CVectorizer.transform(X_test)
print 'feat ext time', time() - T0
# TrainFVs = TFIDFTransformer.fit_transform(TrainFVs)
# TestFVs = TFIDFTransformer.transform(TestFVs)
TrainFVs = normalizer.fit_transform(TrainFVs)
TestFVs = normalizer.transform(TestFVs)
print 'Trai/test split'
print TrainFVs.shape
print TestFVs.shape
# raw_input('hit any key...')
print 'training classifier with train samples shape:', TrainFVs.shape
T0 = time()
# memory_dump('before_train_mem.txt')
Model = Clf.fit (TrainFVs, y_train) # re-train on current training set (daily)
print 'batch fitted'
print 'training time', time() - T0
# memory_dump('after_train_mem.txt')
print 'testing classifier with test samples shape:', TestFVs.shape
T0 = time()
# memory_dump('before_test_mem.txt')
PredictedLabels = Clf.predict(TestFVs)
print 'testing time', time() - T0
# memory_dump('after_test_mem.txt')
print '*'*100
print 'classification report'
print '-'*20
Accuracy = np.mean(PredictedLabels == y_test)
print "Test Set Accuracy = ", Accuracy
print(metrics.classification_report(y_test,
PredictedLabels, target_names=['Neg', 'Pos']))
print "Accuracy classification score:", metrics.accuracy_score(y_test, PredictedLabels)
print "Hamming loss:", metrics.hamming_loss(y_test, PredictedLabels)
print "Average hinge loss:", metrics.hinge_loss(y_test, PredictedLabels)
print "Log loss:", metrics.log_loss(y_test, PredictedLabels)
print "F1 Score:", metrics.f1_score(y_test, PredictedLabels)
print "Zero-one classification loss:", metrics.zero_one_loss(y_test, PredictedLabels)
print '*'*100
Vocab = CVectorizer.get_feature_names()
# print Vocab[:100]
# raw_input()
#.........这里部分代码省略.........
开发者ID:MLDroid,项目名称:SentimentAnalysisInt,代码行数:101,代码来源:Classify.py
示例18: test_hinge_loss
def test_hinge_loss(self):
result = self.df.metrics.hinge_loss()
expected = metrics.hinge_loss(self.target, self.decision)
self.assertEqual(result, expected)
开发者ID:Sandy4321,项目名称:pandas-ml,代码行数:4,代码来源:test_metrics.py
示例19: train_svm
def train_svm(train_data, valid_data, test_data, model_dir, C=1.0, kernel='rbf',
num_classes=10, tol=0.001, max_iterations=-1, verbose=False,
random_state=12345678, **kwargs):
"""
Train a Support Vector Machine model on the given data
Args:
X_train: Training feature data
(Type: np.ndarray)
y_train: Training label data
(Type: np.ndarray)
X_test: Testing feature data
(Type: np.ndarray)
y_test: Testing label data
(Type: np.ndarray)
Keyword Args:
C: SVM regularization hyperparameter
(Type: float)
verbose: If True, print verbose messages
(Type: bool)
Returns:
clf: Classifier object
(Type: sklearn.svm.SVC)
y_train_pred: Predicted train output of classifier
(Type: np.ndarray)
y_test_pred: Predicted test output of classifier
(Type: np.ndarray)
"""
np.random.seed(random_state)
random.seed(random_state)
X_train = train_data['features']
y_train = train_data['labels']
model_output_path = os.path.join(model_dir, "model.pkl")
# Create classifier
clf = SVC(C=C, probability=True, kernel=kernel, max_iter=max_iterations,
tol=tol, random_state=random_state, verbose=verbose)
# Fit data and get output for train and valid batches
LOGGER.debug('Fitting model to data...')
clf.fit(X_train, y_train)
LOGGER.info('Saving model...')
joblib.dump(clf, model_output_path)
y_train_pred = clf.predict(X_train)
# Compute new metrics
classes = np.arange(num_classes)
train_loss = hinge_loss(y_train, clf.decision_function(X_train), labels=classes)
train_metrics = compute_metrics(y_train, y_train_pred, num_classes=num_classes)
train_metrics['loss'] = train_loss
train_msg = 'Train - hinge loss: {}, acc: {}'
LOGGER.info(train_msg.format(train_loss, train_metrics['accuracy']))
if valid_data:
X_valid = valid_data['features']
y_valid = valid_data['labels']
y_valid_pred = clf.predict(X_valid)
valid_loss = hinge_loss(y_valid, clf.decision_function(X_valid), labels=classes)
valid_metrics = compute_metrics(y_valid, y_valid_pred, num_classes=num_classes)
valid_metrics['loss'] = valid_loss
valid_msg = 'Valid - hinge loss: {}, acc: {}'
LOGGER.info(valid_msg.format(valid_loss, valid_metrics['accuracy']))
else:
valid_metrics = {}
# Evaluate model on test data
if test_data:
X_test = test_data['features']
y_test_pred_frame = clf.predict_proba(X_test)
y_test_pred = []
for start_idx, end_idx in test_data['file_idxs']:
class_pred = y_test_pred_frame[start_idx:end_idx].mean(axis=0).argmax()
y_test_pred.append(class_pred)
y_test_pred = np.array(y_test_pred)
test_metrics = compute_metrics(test_data['labels'], y_test_pred, num_classes=num_classes)
else:
test_metrics = {}
return clf, train_metrics, valid_metrics, test_metrics
开发者ID:Mohitsharma44,项目名称:l3embedding,代码行数:88,代码来源:train.py
示例20: main_func
def main_func(datanm, samples_per_class, C, num_classes, gamma, num_iter = 100, kernel = 'linear', strat = 'ovr'):
data, labels = load_full(datanm, samples_per_class)
slo = StratifiedShuffleSplit(labels, n_iter=num_iter, test_size=0.3, train_size=0.7, random_state=None)
recall = np.zeros((num_classes+1, 2))
precision = np.zeros((num_classes+1, 2))
f1 = np.zeros((num_classes+1, 2))
accuracy = np.zeros((2))
logloss = np.zeros((2))
hingeloss = np.zeros((2))
for train_index, test_index in slo:
train_data = [data[train_index, :], labels[train_index]]
valid_data = [data[test_index , :], labels[test_index ]]
clf = svm.SVC(C=C, kernel=kernel, degree=3, gamma=gamma, coef0=0.0, shrinking=True,
probability=False, tol=0.001, cache_size=10000, class_weight=None,
verbose=False, max_iter=-1, decision_function_shape=strat, random_state=None)
clf.fit(train_data[0], train_data[1])
#out_train = clf.predict_proba(train_data[0])
#out_valid = clf.predict_proba(valid_data[0])
#logloss[0] += log_loss(train_data[1], out_train)
#logloss[1] += log_loss(valid_data[1], out_valid)
out_train = clf.decision_function(train_data[0])
out_valid = clf.decision_function(valid_data[0])
hingeloss[0] += hinge_loss(train_data[1], out_train)
hingeloss[1] += hinge_loss(valid_data[1], out_valid)
out_train = clf.predict(train_data[0])
out_valid = clf.predict(valid_data[0])
accuracy[0] += accuracy_score(train_data[1], out_train)
accuracy[1] += accuracy_score(valid_data[1], out_valid)
precision[:-1, 0] += precision_score(train_data[1], out_train, average = None)
precision[-1, 0] += precision_score(train_data[1], out_train, average = 'macro')
precision[:-1, 1] += precision_score(valid_data[1], out_valid, average = None)
precision[-1, 1] += precision_score(valid_data[1], out_valid, average = 'macro')
recall[:-1, 0] += recall_score(train_data[1], out_train, average = None)
recall[-1, 0] += recall_score(train_data[1], out_train, average = 'macro')
recall[:-1, 1] += recall_score(valid_data[1], out_valid, average = None)
recall[-1, 1] += recall_score(valid_data[1], out_valid, average = 'macro')
f1[:-1, 0] += f1_score(train_data[1], out_train, average = None)
f1[-1, 0] += f1_score(train_data[1], out_train, average = 'macro')
f1[:-1, 1] += f1_score(valid_data[1], out_valid, average = None)
f1[-1, 1] += f1_score(valid_data[1], out_valid, average = 'macro')
f1 /= num_iter
recall /= num_iter
precision /= num_iter
logloss /= num_iter
accuracy /= num_iter
np.savez("svm_final_" + kernel + '_' + strat, accuracy = accuracy, recall = recall, f1 = f1,
precision = precision, logloss = logloss, C = C,
num_iter = num_iter, num_classes = num_classes,
samples_per_class = samples_per_class,
hingeloss = hingeloss)
return [accuracy, recall, f1, precision, logloss, hingeloss]
开发者ID:dmitro-nazarenko,项目名称:chemfin-open,代码行数:65,代码来源:svm.py
注:本文中的sklearn.metrics.hinge_loss函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论