本文整理汇总了Python中sklearn.naive_bayes.GaussianNB.decision_function方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianNB.decision_function方法的具体用法?Python GaussianNB.decision_function怎么用?Python GaussianNB.decision_function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.naive_bayes.GaussianNB
的用法示例。
在下文中一共展示了GaussianNB.decision_function方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Classifier
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import decision_function [as 别名]
#.........这里部分代码省略.........
X = preprocessing.scale(X)
return self.clf.score(X, Y)
def predict(self, X):
"""Predict values for X"""
if not self.trained:
raise ValueError("Classifier is not trained")
if self.scale:
X = preprocessing.scale(X)
return self.clf.predict(X)
def predict_proba(self, X):
"""Return probability for each class prediction"""
return self.clf.predict_proba(X)
def feature_importance(self, outfile="importance.png", features_list=[]):
"""Show each feature importance"""
if (self.method in ['rf', 'etc']):
importances = self.clf.feature_importances_
if len(features_list) > 0 and len(features_list) != len(importances):
raise ValueError("Number of features does not fit!")
indices = np.argsort(importances)[::-1]
n_feats = len(features_list)
np.savetxt(outfile + ".txt", np.array([tree.feature_importances_
for tree in self.clf.estimators_]), delimiter=',', fmt='%1.3e')
std = np.std(
[tree.feature_importances_ for tree in self.clf.estimators_], axis=0)
plt.figure()
plt.title("Feature importances")
plt.bar(range(n_feats), importances[
indices], width=0.5, color="b", yerr=std[indices], align="center")
if len(features_list) > 0:
features_list = np.asarray(features_list)[indices]
plt.xticks(range(n_feats), features_list, rotation='vertical')
plt.xlim([-1, n_feats])
plt.margins(0.2)
plt.subplots_adjust(bottom=0.15)
plt.savefig(outfile, bbox_inches='tight')
else:
raise NotImplementedError(
"Not supported for classifier other than Ensembl Tree")
def cross_validation(self, X, Y, X_test=None, Y_test=None, tsize=0.3):
"""Cross validation on X and Y, using a sub sample"""
if X_test is None or Y_test is None:
X_train, X_test, Y_train, Y_test = train_test_split(
X, Y, test_size=tsize)
else:
X_train = X
Y_train = Y
self.train(X_train, Y_train)
Y_predicted = self.predict(X_test)
self.get_stat(X_test, Y_test)
return Y_predicted, self.get_score(X_test, Y_test)
def plot_precision_recall(self, X_test, y_test, infos="", outfile="precision_recall.png"):
"""plot precicion-recall curve"""
if self.trained:
try:
y_score = self.clf.decision_function(X_test)
except:
y_score = self.clf.predict_proba(X_test)[:, 1]
precision, recall, _ = precision_recall_curve(y_test, y_score)
average_precision = average_precision_score(
y_test, y_score, average="micro")
# Plot Precision-Recall curve for each class
plt.clf()
plt.plot(recall, precision,
label='Average Precision-recall curve (area = {0:0.2f})'
''.format(average_precision))
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall curve for %s (%s)' %
(self.method, infos))
plt.legend(loc="lower right")
plt.savefig(outfile)
else:
raise ValueError("Classifier is not trained")
def get_stat(self, X_test, y_test):
"""Print list of score for the current classifier"""
y_pred = self.predict(X_test)
if hasattr(self.clf, "predict_proba"):
prob_pos = self.clf.predict_proba(X_test)[:, 1]
else: # use decision function
prob_pos = self.clf.decision_function(X_test)
prob_pos = (prob_pos - prob_pos.min()) / \
(prob_pos.max() - prob_pos.min())
clf_score = brier_score_loss(y_test, prob_pos)
print("%s:" % self.method)
print("\tBrier: %1.3f" % (clf_score))
print("\tPrecision: %1.3f" % precision_score(y_test, y_pred))
print("\tRecall: %1.3f" % recall_score(y_test, y_pred))
print("\tF1: %1.3f" % f1_score(y_test, y_pred))
print("\tROC AUC score: %1.3f\n" % roc_auc_score(y_test, prob_pos))
示例2: Learning
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import decision_function [as 别名]
#.........这里部分代码省略.........
self.logger.error("failed to load %s" % (file_name))
def load_scaler(self, file_name):
try:
self.scaler = pickle.load( open(file_name, "r"))
if self.scaler:
self.scaling = True
except ValueError:
self.logger.error("failed to load %s" % (file_name))
def predict(self, X_test, y_test, **kwargs):
'''
return dictionary of results
'''
if self.scaling:
X_test = self.scaler.transform(X_test)
self.logger.info('y_test = %s', str(y_test.shape))
y_predict = self.clf.predict(X_test)
X_predict_prob = self.clf.predict_proba(X_test) if self.clf.probability else 0
results = {}
if 'score' in kwargs and kwargs['score'] == True:
results.update({'score': self.clf.score(X_test, y_test.tolist())})
self.logger.info('score = %f', results['score'])
if 'weighted_score' in kwargs and kwargs['weighted_score'] == True:
results.update({'weighted_score': self._weighted_score(y_test.tolist(), y_predict)})
self.logger.info('weighted_score = %f', results['weighted_score'])
if 'y_predict' in kwargs and kwargs['y_predict'] == True:
results.update({'y_predict': y_predict})
self.logger.info('y_predict = %f', results['y_predict'])
if 'X_predict_prob' in kwargs and kwargs['X_predict_prob'] == True:
results.update({'X_predict_prob': X_predict_prob[:, 1]})
self.logger.info('X_predict_prob = %s', str(results['X_predict_prob']))
if 'auc' in kwargs and kwargs['auc'] == True:
fpr, tpr, thresholds = roc_curve(y_test, X_predict_prob[:, 1])
results.update({'auc': auc(fpr, tpr)})
self.logger.info('auc = %f', results['auc'])
if 'decision_value' in kwargs and kwargs['decision_value'] == True:
results.update({'decision_value': self.clf.decision_function(X_test)})
self.logger.debug('decision_value = %s', str(results['decision_value']))
return results
def _weighted_score(self, y_test, y_predict):
# calc weighted score
n_pos = len([val for val in y_test if val == 1])
n_neg = len([val for val in y_test if val == -1])
temp_min = min(n_pos, n_neg)
weight_pos = 1.0/(n_pos/temp_min)
weight_neg = 1.0/(n_neg/temp_min)
correct_predict = [i for i, j in zip(y_test, y_predict) if i == j]
weighted_sum = 0.0
for answer in correct_predict:
weighted_sum += weight_pos if answer == 1 else weight_neg
wscore = weighted_sum / (n_pos * weight_pos + n_neg * weight_neg)
return wscore
def kfold(self, kfolder, **kwargs):
"""
return:
mean score for kfold training
required:
kfolder: generated by sklearn.cross_validatio.KFold
options:
same as _train
"""
#amend = False if "amend" not in kwargs else kwargs["amend"]
#if amend:
## amend dense matrix: replace NaN and None with float values
# self.check_and_amend()
#else:
# self.logger.debug("skip the amending process")
sum_score = 0.0
for (i, (train_index, test_index)) in enumerate(kfolder):
self.logger.info("cross-validation fold %d: train=%d, test=%d" % (i, len(train_index), len(test_index)))
X_train, X_test, y_train, y_test = self.X[train_index], self.X[test_index], self.y[train_index], self.y[test_index]
self._train(X_train, y_train, **kwargs)
score = self.predict(X_test, y_test, score=True)['score']
self.logger.info('score = %.5f' % (score))
sum_score += score
mean_score = sum_score/len(kfolder)
self.logger.info('*** C = %f, mean_score = %f' % (kwargs['C'], mean_score))
return mean_score