本文整理汇总了Python中sklearn.naive_bayes.GaussianNB.predict_proba方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianNB.predict_proba方法的具体用法?Python GaussianNB.predict_proba怎么用?Python GaussianNB.predict_proba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.naive_bayes.GaussianNB
的用法示例。
在下文中一共展示了GaussianNB.predict_proba方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_gnb_priors
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def test_gnb_priors():
"""Test whether the class prior override is properly used"""
clf = GaussianNB(priors=np.array([0.3, 0.7])).fit(X, y)
assert_array_almost_equal(clf.predict_proba([[-0.1, -0.1]]),
np.array([[0.825303662161683,
0.174696337838317]]), 8)
assert_array_equal(clf.class_prior_, np.array([0.3, 0.7]))
示例2: GNB
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
class GNB(object):
def __init__(self):
self.gnb = GaussianNB()
def predict(self, X):
return self.gnb.predict_proba(X)[:,1][:,np.newaxis]
def fit(self, X, y):
self.gnb.fit(X,y)
示例3: performNB
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def performNB(trainingScores, trainingResults, testScores):
print "->Gaussian NB"
X = []
for currMark in trainingScores:
pass
for idx in range(0, len(trainingScores[currMark])):
X.append([])
for currMark in trainingScores:
if "Asym" in currMark:
continue
print currMark,
for idx in range(0, len(trainingScores[currMark])):
X[idx].append(trainingScores[currMark][idx])
X_test = []
for idx in range(0, len(testScores[currMark])):
X_test.append([])
for currMark in trainingScores:
if "Asym" in currMark:
continue
for idx in range(0, len(testScores[currMark])):
X_test[idx].append(testScores[currMark][idx])
gnb = GaussianNB()
gnb.fit(X, np.array(trainingResults))
y_pred = gnb.predict_proba(X_test)[:, 1]
print "->Gaussian NB"
return y_pred
示例4: gnbmodel
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def gnbmodel(d,X_2,y_2,X_3,y_3,X_test,y_test):
X_3_copy = X_3.copy(deep=True)
X_3_copy['chance']=0
index = 0
########## k折交叉验证 ###########################
scores = cross_val_score(GaussianNB(), X_2, y_2, cv=5, scoring='accuracy')
score_mean =scores.mean()
print(d+'5折交互检验:'+str(score_mean))
#################################################
gnb = GaussianNB().fit(X_2,y_2)
################ 预测测试集 ################
answer_gnb = gnb.predict(X_test)
accuracy = metrics.accuracy_score(y_test,answer_gnb)
print(d+'预测:'+str(accuracy))
###############################################
chance = gnb.predict_proba(X_3)[:,1]
for c in chance:
X_3_copy.iloc[index,len(X_3_copy.columns)-1]=c
index += 1
chance_que = X_3_copy.iloc[:,len(X_3_copy.columns)-1]
return chance_que
示例5: GaussianColorClassifier
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
class GaussianColorClassifier(ContourClassifier):
'''
A contour classifier which classifies a contour
based on it's mean color in BGR, HSV, and LAB colorspaces,
using a Gaussian classifier for these features.
For more usage info, see class ContourClassifier
'''
FEATURES = ['B', 'G', 'R', 'H', 'S', 'V', 'L', 'A', 'B']
def __init__(self, classes, **kwargs):
super(GaussianColorClassifier, self).__init__(classes, **kwargs)
self.classifier = GaussianNB()
def get_features(self, img, mask):
mean = cv2.mean(img, mask)
mean = np.array([[mean[:3]]], dtype=np.uint8)
mean_hsv = cv2.cvtColor(mean, cv2.COLOR_BGR2HSV)
mean_lab = cv2.cvtColor(mean, cv2.COLOR_BGR2LAB)
features = np.hstack((mean.flatten(), mean_hsv.flatten(), mean_lab.flatten()))
return features
def classify_features(self, features):
return self.classifier.predict(features)
def feature_probabilities(self, features):
return self.classifier.predict_proba(features)
def train(self, features, classes):
self.classifier.fit(features, classes)
示例6: trainData
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def trainData(username):
"""
Trains the data based on the users performance so far
Returns a trained Gaussian Naive Bayes model and updates result collection
"""
X = getFeatures(username)
Y = getClassList(username)
trainX = np.array(X)
trainY = np.array(Y)
gnb = GaussianNB()
gnb.fit(trainX, trainY)
print "Score with Naive Bayes: ", gnb.score(trainX, trainY)
testData = words.posts.find({}, {'id' : 1,
'points' : 1,
'diff' : 1,
'_id' : 0})
testData = map(lambda x : (x['id'], x['points'], x['diff']), testData)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
for data in testData:
testWord = words.posts.find_one({'id' : data[0]}, {'word' : 1, '_id' : 0})['word']
wordClass = setWordClass(list(gnb.predict_proba(data))[0])
classWord = result.posts.update({'username' : username}, {'$set' : {testWord : wordClass}}, upsert = True)
示例7: naiveBayesClassifierTraining
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def naiveBayesClassifierTraining(compounds_all):
print "Building naive Bayes classifier (" + str(NB_FOLDS) + "-fold cross-validation)..."
# get the data
keys = compounds_all.keys()
fingerprint_data = [compounds_all[cmpnd_id]['fingerprint'] for cmpnd_id in keys]
fingerprint_data = numpy.asarray(fingerprint_data)
activity_data = [compounds_all[cmpnd_id]['active'] for cmpnd_id in keys]
activity_data = numpy.asarray(activity_data)
# perform K-fold cross-validation
classifier = GaussianNB()
kfold_xv_strat = cross_validation.StratifiedKFold(activity_data, NB_FOLDS, indices=False)
confusion_matrices = []
probabilities = []
scores = []
models = []
true_activities = []
aucs = []
for train, test in kfold_xv_strat:
fingerprint_data_train = fingerprint_data[train]
fingerprint_data_test = fingerprint_data[test]
activity_data_train = activity_data[train]
activity_data_test = activity_data[test]
# model building
classifier.fit(fingerprint_data_train, activity_data_train)
# testing
activity_data_predictions = classifier.predict(fingerprint_data_test)
models.append(classifier)
probability_estimates = classifier.predict_proba(fingerprint_data_test)
probabilities.append(probability_estimates)
scores.append(classifier.score(fingerprint_data_test, activity_data_test))
activity_confusion_matrix = confusion_matrix(activity_data_test, activity_data_predictions)
confusion_matrices.append(activity_confusion_matrix)
true_activities.append(activity_data_test)
# ROC curves
fpr, tpr, thresholds = roc_curve(activity_data_test, probability_estimates[:, 1])
aucs.append(auc(fpr, tpr))
classifier.fit(fingerprint_data, activity_data)
print "Done."
return {
'confusion_matrices' : confusion_matrices
, 'probabilities' : probabilities
, 'scores' : scores
, 'models' : models
, 'true_activity_data' : true_activities
, 'AUCs' : aucs
, 'fingerprint_data' : fingerprint_data
, 'activity_data' : activity_data
, 'final_model' : classifier
}
示例8: bayseFilter
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def bayseFilter(X,y):
clf = GaussianNB()
clf.fit(X,y)
bayseX = clf.predict_proba(X)
t = np.ones(bayseX.shape[0])
for i in range(0,bayseX.shape[1]):
t = t*bayseX[:,i]
bayseXfilter = t
return bayseXfilter
示例9: __init__
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
class NaiveBayes:
__theta = 0
__sigma = 0
def __init__(self):
pass
#self.__new_data = 0
def learning(self,x_data,y_data):
self.rssi = np.loadtxt(x_data, delimiter=',')
print(self.rssi)
self.position = np.loadtxt(y_data, delimiter=',')
print(self.position)
self.gaussian_nb = GaussianNB()
from sklearn.cross_validation import train_test_split
rssi_train, rssi_test, position_train, position_test = train_test_split(self.rssi, self.position, random_state=0)
self.gaussian_nb.fit(rssi_train,position_train)
print("theta",self.gaussian_nb.theta_)
print("sigma",self.gaussian_nb.sigma_)
predicted = self.gaussian_nb.predict(rssi_test)
print(metrics.accuracy_score(position_test, predicted))
'''
def set_params(self,theta,sigma):
__theta = theta
__sigma = sigma
print __theta
print __sigma
'''
def inference(self,r_data):
self.predicted_class = self.gaussian_nb.predict(r_data)
post_prob = self.gaussian_nb.predict_proba(r_data)
log_prob = self.gaussian_nb.predict_log_proba(r_data)
self.post_prob_float16 = post_prob.astype(np.float16)
#E = 1*self.post_prob_float16[0][0]+2*self.post_prob_float16[0][1]+3*self.post_prob_float16[0][2]
#var = (1*self.post_prob_float16[0][0]+4*self.post_prob_float16[0][1]+9*self.post_prob_float16[0][2])-E**2
#print(self.post_prob_float16)
#print(self.post_prob_float16[0])
#print(var)
print(self.predicted_class)
#print(self.gaussian_nb.class_prior_)
#print(log_prob)
return self.predicted_class
def output(self):
output = graph.Graph()
output.bar_graph(self.post_prob_float16[0])
示例10: train_NB_model
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def train_NB_model(trackset, training_set):
useful_features = ['acousticness','danceability','instrumentalness','energy','speechiness','tempo','valence']
X = training_set[useful_features]
Y = training_set.status
w = training_set.weight
clf = GaussianNB()
clf.fit(X, Y, sample_weight=w)
predicts = pd.DataFrame(clf.predict_proba(trackset[useful_features]))
predicts.columns = ['P_reject','P_accept']
trackset.P_accept = predicts['P_accept'].values
return trackset.sort_values(by=['P_accept'], ascending=False)
示例11: main
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def main():
train = p.read_table('../train.tsv').replace('?',0)
# target = np.array(train)[:,-1]
train['alchemy_category'] = train.groupby('alchemy_category').grouper.group_info[0]
train['alchemy_category_score'] = train['alchemy_category_score'].astype(float)
# train = np.array(train)[:,:-1]
train = np.array(train)[:,3:]
test = p.read_table('../test.tsv').replace('?',0)
test['alchemy_category'] = test.groupby('alchemy_category').grouper.group_info[0]
test['alchemy_category_score'] = test['alchemy_category_score'].astype(float)
valid_index = list(np.array(test)[:,1])
orig_test = np.array(test)[:,3:]
test = train
test = outlier(test,20)
target = test[:,-1]
test = test[:,:-1]
print len(test)
r = []
r.append([0,0.000])
for j in range(1,10):
n = int((8.5*len(train))/10)
X_train = test[:n]
X_test = test[n:]
y_train = target[:n]
y_test = target[n:]
# run the model
#classifier = RandomForestClassifier(n_estimators=1000,verbose=0,n_jobs=20,min_samples_split=5,random_state=1034324)
classifier = GaussianNB()
classifier.fit(X_train, y_train)
pred = classifier.predict_proba(X_test)
fpr, tpr, thresholds = roc_curve(y_test,pred[:,1])
roc_auc = auc(fpr, tpr)
print("%d Area under the ROC curve : %f" %(i,roc_auc))
r.append([j,roc_auc])
plt.grid(True)
#print r
x = [i[0]*10 for i in r]
y = [i[1]*100 for i in r]
plt.plot(x,y,linewidth=3)
plt.axis([0,100,0,100])
plt.xlabel("training % data")
plt.ylabel('Accuracy (CV score k=20)')
plt.show()
# gnb.fit(X_train, y_train)
# pred = gnb.predict(X_test)
# fpr, tpr, thresholds = roc_curve(y_test,pred)
# roc_auc = auc(fpr, tpr)
# print("Area under the ROC curve : %f" % roc_auc)
# write
writer = csv.writer(open("predictions", "w"), lineterminator="\n")
rows = [x for x in zip(valid_index, classifier.predict(orig_test))]
writer.writerow(("urlid","label"))
writer.writerows(rows)
示例12: nbayes
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def nbayes(source, target):
""" Naive Bayes Classifier
"""
source = SMOTE(source)
clf = GaussianNB()
features = source.columns[:-1]
klass = source[source.columns[-1]]
clf.fit(source[features], klass)
preds = clf.predict(target[target.columns[:-1]])
distr = clf.predict_proba(target[target.columns[:-1]])
return preds, distr[:,1]
示例13: gNB
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def gNB(train_data, train_labels, test, save_result=False):
log_state('Use Gaussian Naive Bayes classifier')
clf = GaussianNB()
clf.fit(train_data, train_labels)
predict_labels = clf.predict(test)
predict_proba = clf.predict_proba(test)
if save_result == True:
dump_picle(predict_labels, './data/predict_labels/predict_labels.p')
dump_picle(predict_proba, './data/predict_labels/predict_proba.p')
logger.info('Classifier training complete, saved predict labels to pickle')
return predict_labels
示例14: naive_bayes_crossval_network
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def naive_bayes_crossval_network(title):
csv = pandas.read_csv("data/cables2009WithRefAttributes.csv", sep=";")
X, Y = get_xy_from_csv2(csv)
fold_size = len(Y)/10
for fold in xrange(0, 10):
if fold == 9:
last = len(Y) - (fold + 1) * fold_size
else:
last = 0
test = range(fold * fold_size, (fold + 1) * fold_size + last)
train = list(set(range(len(Y))) - set(test))
clf = GaussianNB()
clf.fit(X[train, :], Y[train])
if fold == 0:
print "Naive bayes 10-fold crossval: 0",
probs = clf.predict_proba(X[test, :])
else:
print fold,
probs = np.concatenate([probs, clf.predict_proba(X[test, :])])
print " "
plot_ROC_of_graph(0, 0, True, Y, probs, title)
示例15: naiveBayesModel
# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import predict_proba [as 别名]
def naiveBayesModel(train_data, test_data, train_Y, test_Y):
# Build Naive Bayes Model
model = GaussianNB()
model.fit(train_data, train_Y)
# print(model)
# Make predictions
predicted = model.predict_proba(test_data)
# print predicted[0:,1]
print "Naive Bayes :"
print 'Log Loss :', metrics.log_loss(test_Y, predicted[0:,1])