当前位置: 首页>>代码示例>>Python>>正文


Python MultinomialNB.score方法代码示例

本文整理汇总了Python中sklearn.naive_bayes.MultinomialNB.score方法的典型用法代码示例。如果您正苦于以下问题:Python MultinomialNB.score方法的具体用法?Python MultinomialNB.score怎么用?Python MultinomialNB.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.naive_bayes.MultinomialNB的用法示例。


在下文中一共展示了MultinomialNB.score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: NBTest

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
class NBTest(unittest.TestCase):

    def setUp(self):
        self.mnb = NaiveBayes(multinomial=True)
        self.skmnb = MultinomialNB()
        self.bnb = NaiveBayes(bernoulli=True)
        self.skbnb = BernoulliNB()
        self.cnb = NaiveBayes(multinomial=True, cnb=True)
        self.wcnb = NaiveBayes(multinomial=True, wcnb=True)

    def test_count_vectorized(self):
        self.mnb.fit(X_count, train_targets)
        self.skmnb.fit(X_count, train_targets)
        self.assertEqual(self.mnb.score(X_count_test,test_targets),self.skmnb.score(X_count_test,test_targets))

    def test_tfidf_vectorized(self):
        self.mnb.fit(X_tfidf, train_targets)
        self.skmnb.fit(X_tfidf, train_targets)
        self.assertEqual(self.mnb.score(X_tfidf_test, test_targets), self.skmnb.score(X_tfidf_test, test_targets))

    def test_cnb(self):
        self.cnb.fit(X_count, train_targets)
        self.mnb.fit(X_count, train_targets)
        cnb_score = self.cnb.score(X_count_test, test_targets)
        mnb_score = self.mnb.score(X_count_test, test_targets)
        print "CNB: {},   MNB: {}".format(cnb_score, mnb_score)
        assert (cnb_score - mnb_score) > -0.1  

    def test_wcnb(self):
        self.wcnb.fit(X_count, train_targets)
        self.mnb.fit(X_count, train_targets)
        wcnb_score = self.wcnb.score(X_count_test, test_targets)
        mnb_score = self.mnb.score(X_count_test, test_targets)
        print "WCNB: {},   MNB: {}".format(wcnb_score, mnb_score)
        assert (wcnb_score - mnb_score) > -0.5  
开发者ID:davidrapoport,项目名称:ml_project2,代码行数:37,代码来源:test_nb.py

示例2: multinomialNB

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def multinomialNB(devMatrix, trainMatrix, devtarget, traintarget):
	f = open('MNNB2.log', 'a')
	f.write("Making model!!!!!")
	print 'Making model!'
	clf = MultinomialNB(alpha=1, fit_prior=False)
	clf.fit(trainMatrix, traintarget)
	f.write("\n")
	value = ('Model: multinomial bayes with parameters ',clf.get_params(False))
	print (str(value))
	f.write(str(value))
	f.write("\n")
	f.write("MSE for train: %.2f" % np.mean((clf.predict(trainMatrix) - traintarget) ** 2))
	score = clf.score(trainMatrix, traintarget)
	f.write("\n")
	value = ('Score for train %.2f', score)
	f.write("\n")
	f.write("MSE for dev: %.2f" % np.mean((clf.predict(devMatrix) - devtarget) ** 2))
	score = clf.score(devMatrix, devtarget)
	value = ('Score for dev %.2f', score)
	print(str(value))
	f.write("\n")
	s = str(value)
	f.write(s)
	f.write("\n")
	f.write('model done')
	f.write("\n")
	f.write("\n")
	f.close()
	return score
开发者ID:katymccl3,项目名称:MachineLearning,代码行数:31,代码来源:dataParser.py

示例3: create_model

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def create_model(feat_train, label_train_col, feat_test, label_test_col):
	'''
	create model
	'''
	clf_nb = MultinomialNB()
	clf_nb.fit(feat_train, label_train_col)
	#clf.fit(lsa_features, label_train)
	cpt = clf_nb.predict_proba(feat_test)
	clf_nb.score(feat_test, label_test_col)
	nb_cpt=clf_nb.feature_log_prob_
开发者ID:LisaDawn,项目名称:HitMaker,代码行数:12,代码来源:create_model.py

示例4: naive_bayes

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def naive_bayes(x_train, y_train, x_cv, y_cv):
    """ Using Naive Bayes to classify the data. """

    print 'Training with NB...'
    clf = MultinomialNB()
    clf.fit(x_train, y_train)

    print 'Accuracy in training set: %f' % clf.score(x_train, y_train)
    print 'Accuracy in cv set: %f' % clf.score(x_cv, y_cv)
    return clf
开发者ID:FindBoat,项目名称:Kaggle,代码行数:12,代码来源:classification.py

示例5: bayes

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def bayes(r_state,mod_type='Multinomial',**kwargs):
    """for playing with various naive bayes classifiers
    NB: they're mostly not appropriate to this data representation"""
    import collections
    
    #grab the flat clustering for top200 chords
    #numChords by numWindows array of sample data
    samples = []
    #numChords-length vector of cluster tags
    tags = []
    flat_clus_path = 'C:/Users/Andrew/Documents/DissNOTCORRUPT/Categories chapter/truncDend_memb.csv'
    all_labels = csv.reader(open(flat_clus_path,'r',newline='\n'))
    for i,row in enumerate(all_labels):
        if i==0: continue #skip header row
        tags.append(row[0])
        sf = np.genfromtxt('dcMats_PCAord/'+row[1]+'.csv',delimiter=',')
        samples.append(sf.flatten())
    #print(samples[0])
    
    #auto-separate training/testing sets from full tagged sample
    x_tr,x_ts,y_tr,y_ts = train_test_split(samples,tags,test_size=0.3,random_state=r_state)
    #print(y_tr)
    
    #estimate priors from the assignments of the training set
    #this needs thinking: consider a list of all priors that gets partly non-zero populated
    #would allow various smoothing factors later
    cat_labs = set(tags)
    est_priors = [0 for t in cat_labs]
    for tag in y_tr:
        est_priors[int(tag)-1] += 1
    est_priors = [y/sum(est_priors) for y in est_priors]
    #print(est_priors)
    
    #fit model, return score
    if mod_type=='Multinomial':
        from sklearn.naive_bayes import MultinomialNB
        clf = MultinomialNB(class_prior=est_priors,**kwargs)
    elif mod_type=='Gaussian':
        from sklearn.naive_bayes import GaussianNB
        clf = GaussianNB(**kwargs)
    else:
        raise TypeError('mod_type should be Multinomial or Gaussian')
    clf.fit(x_tr,[int(y) for y in y_tr])
    if verbose:
        #print(x_ts[0],clf.predict_proba(x_ts[0]))
        print(clf.score(x_ts,y_ts))
    #score the trained model on the held-out testing data
    return(clf.score(x_ts,y_ts))
开发者ID:andrewdjones,项目名称:YJaMP_Analysis,代码行数:50,代码来源:yjampClass.py

示例6: find_best_vectorizor

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def find_best_vectorizor(vectorizer, grid):
  dg = DataGatherer()
  y_test = dg.validate_target
  y_train = dg.labeled_target

  nb = MultinomialNB()
  header_printed = False
  best_params = None
  best_score = -1
  for param in IterGrid(grid):
    if not header_printed:
      print(str(",".join(param.keys())) + ",Score")
    header_printed = True
    vectorizer.set_params(**param)
    X_train = vectorizer.fit_transform(dg.labeled_data)    
    X_test = vectorizer.transform(dg.validate_data)
    nb.fit(X_train, y_train)
    score = nb.score(X_test, y_test)
    if score > best_score:
      best_score = score
      best_params = param
    print(str(",".join(map(str, param.values()))) + "," + str(score))
  print("")
  print("Best params: " + str(best_params))
  print("Best score: " + str(best_score))
开发者ID:Web5design,项目名称:big-data,代码行数:27,代码来源:naive_bayes_optimizer.py

示例7: learn

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def learn(train_datasets,test_dataset):
    test_x,test_y=test_dataset
    clfs=[]
    accurates=[]
    for train_data in train_datasets:
        clf = MultinomialNB()
        #clf = RandomForestClassifier()
        #clf = GaussianNB()
        #clf = LogisticRegression()
        clf.fit(train_data[0],train_data[1])
        accurate=clf.score(test_x,test_y)
        clfs.append(clf)
        accurates.append(accurate)
        print clf.score(train_data[0],train_data[1])
        print accurate
    return clfs,accurates
开发者ID:Adoni,项目名称:JD_Profiling,代码行数:18,代码来源:learn.py

示例8: multinomialNBClassifier

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def multinomialNBClassifier():
    maxRandomPerformance = []
    alphaValues = [0,10**(-7),10**(-6),10**(-5),10**(-4),10**(-3),10**(-2),10**(-1),1]
    for value in alphaValues:
        clf = MultinomialNB(alpha=value, fit_prior=True)
        clf = clf.fit(trainData,trainLabel)
        score = clf.score(validationData, validationLabel)
        maxRandomPerformance.append(score)

    indexForMax = maxRandomPerformance.index(max(maxRandomPerformance))
    alphaTest = alphaValues[indexForMax]

    clfTest = MultinomialNB(alpha=alphaTest, fit_prior=True)
    clfTest.fit(trainData, trainLabel)
    scoreTest = clfTest.score(testData, testLabel)
    guideToGraph['Multinomial Naive Bayes'] = scoreTest
开发者ID:RonakSumbaly,项目名称:Malware-Classification,代码行数:18,代码来源:classifications.py

示例9: tryMultinomialNaiveBayes

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def tryMultinomialNaiveBayes(goFast):

  best_score = 0

  from sklearn.datasets import dump_svmlight_file, load_svmlight_file
  if goFast:
    training_data, training_labels = load_svmlight_file("dt1_1500.trn.svm", n_features=253659, zero_based=True)
    validation_data, validation_labels = load_svmlight_file("dt1_1500.vld.svm", n_features=253659, zero_based=True)
    testing_data, testing_labels = load_svmlight_file("dt1_1500.tst.svm", n_features=253659, zero_based=True)
  else:
    training_data, training_labels = load_svmlight_file("dt1.trn.svm")
    validation_data, validation_labels = load_svmlight_file("dt1.vld.svm")
    testing_data, testing_labels = load_svmlight_file("dt1.tst.svm")

  from sklearn.naive_bayes import MultinomialNB

  for alpha_value in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]:
    for fit_prior_value in [True, False]:
      multinomial_operator = MultinomialNB(alpha=alpha_value,fit_prior=fit_prior_value)
      multinomial_operator.fit(training_data,training_labels)
      current_score = multinomial_operator.score(validation_data,validation_labels)

      print "Current test: " + str(alpha_value), fit_prior_value
      print "Current score: " + str(current_score)

      if current_score > best_score:
        best_score = current_score
        print "***NEW MAXIMUM SCORE: " + str(best_score)
        print "***NEW MAXIMUM PARAMETERS: " + str(alpha_value), fit_prior_value

  print "Best score was " + str(best_score)
开发者ID:Ikram,项目名称:DUMLS14,代码行数:33,代码来源:dataset_one_learner.py

示例10: __init__

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
class Sentiment:
    def __init__(self):
        self.stop_words = stopwords.words() + list(string.punctuation)
        self.tfid = TfidfVectorizer()
        self.clf = MultinomialNB()

        # score: 0.7225
        # self.clf = SVC()

    # create pipelines
    # clean the input
    def fit(self, X, Y):
        self.X = X
        self.Y = Y
        # give the subset of dataset to be trained
        l = 0
        h = 4000
        words = [word_tokenize(x.decode("utf-8").lower()) for x in X[l:h]]
        processed_words = [" ".join(w for w in s if w not in self.stop_words) for s in words]
        X_train = self.tfid.fit_transform(processed_words)
        Y_train = Y[l:h]
        self.clf.fit(X_train, Y_train)
        print "Classes: ", self.clf.classes_
        print "Score: ", self.clf.score(X_train, Y_train)

    def predict(self, X_inp):
        word_list = " ".join(w for w in word_tokenize(X_inp.decode("utf-8").lower()) if w not in self.stop_words)
        X_test = self.tfid.transform([word_list])
        return self.clf.predict(X_test)
开发者ID:abijith-kp,项目名称:DataMining_NLP_AI,代码行数:31,代码来源:sentiment.py

示例11: naive_bayes

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def naive_bayes():
    nb = MultinomialNB()
    nb.fit(X_train, train_data.danger)
    nb_pred = nb.predict(X_test)
    nb_score = nb.score(X_test, y_test)
    precision, recall, _, _ = precision_recall_fscore_support(y_test, nb_pred)
    return precision, recall, str(nb_score)
开发者ID:ilyaaltshteyn,项目名称:danger_tweets,代码行数:9,代码来源:classify4.py

示例12: run_analyzer

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def run_analyzer(data_file):
    start_time = time.time()
    with open(data_file, 'r') as f:
                data = pickle.load(f)
                labels = data['labels']
                features = data['features']
    
    #split into training and test data
    training_features, test_features, training_labels, test_labels = cross_validation.train_test_split(features, labels, test_size=0.3, random_state=0)
    
    
    clf = svm.SVC()
    clf.fit(training_features, training_labels)
    clf = MultinomialNB().fit(training_features, training_labels)
    print "number of training samples %d" %len(training_labels)
    print "number of test samples: %d" %len(test_labels)
    print "number of features: %d" %training_features.shape[1]
    print "score on the training data: %.2f: " %clf.score(training_features, training_labels)
    predictions = clf.predict(test_features)
    predictions = map(float, predictions)
    test_labels = map(float, test_labels)
    test_labels = np.array(test_labels)
    succes_rate = np.mean(predictions == test_labels)
    
    print "results fitting on test data:"
    print "succes rate: %s" %succes_rate
    print "Runtime : %.2f seconds" % (time.time() - start_time)

##SCRIPT
#run_analyzer(DATA_FILE_2)
#cross_val(DATA_FILE)
#cross_val(DATA_FILE_2)
#search_parameters(DATA_FILE_2)
开发者ID:erikvdp,项目名称:mood-detection,代码行数:35,代码来源:lyricsanalyzer.py

示例13: self_training

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def self_training(attribute,iterate_count,initial_data_count,new_data_count):
    from data_constructor import construct
    print ''

    construct(attribute,initial_data_count)
    unlabel_train_x,unlabel_train_y,unlabel_train_uids=get_data(attribute,'train_unlabel')
    train_x,train_y,train_uids=get_data(attribute,'train')
    test_x,test_y,_=get_data(attribute,'test')

    scores=[]
    for i in xrange(iterate_count):
        print '----------------'
        print 'Iterate: %d'%i
        print 'Labeled training data size: %d'%(len(train_x))
        print 'Unlabeled training data size: %d'%(len(unlabel_train_x))
        print 'Testing data size: %d'%(len(test_x))
        clf=MultinomialNB()
        clf.fit(train_x,train_y)
        score=clf.score(test_x,test_y)
        print 'Accurate: %0.4f'%score
        scores.append(score)
        result=clf.predict_proba(unlabel_train_x)
        good_x,good_y,bad_x,bad_y=extract_new_data(zip(unlabel_train_x,result),new_data_count)
        if len(good_x)==0:
            print 'No more new train data!'
            break
        print 'New training data size: %d'%(len(good_x))
        train_x=numpy.concatenate((train_x, good_x), axis=0)
        train_y=numpy.concatenate((train_y, good_y), axis=0)
        unlabel_train_x,unlabel_train_y=bad_x,bad_y
    print '--------'
    for s in scores:
        print s
    print '--------'
开发者ID:Adoni,项目名称:JD_Profiling,代码行数:36,代码来源:learn.py

示例14: compute_recommendation_model

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def compute_recommendation_model():

    base_file = "../data/feedly/pocket_like_data.json"
    user_ids = get_users(base_file)
    tuple_texts_categories=[]

    for user_id in user_ids :
        tuple_texts_categories.extend(TextLearning('english').get_stemmed_texts(user_id,base_file,True))

    texts= [x[0] for x in tuple_texts_categories]
    categories= [x[1] for x in tuple_texts_categories]
    for_df = {"texts" : texts, "categories":categories}
    data = pandas.DataFrame(for_df,columns=["texts","categories"])

    # Split into 2 data sets, one for training the other for test
    train,test = train_test_split(data, train_size=0.5)
    text_vectorizer = CountVectorizer(analyzer='word')
    train_matrix= text_vectorizer.fit_transform(train['texts'])
    test_matrix= text_vectorizer.transform(test['texts'])

    classifier = MultinomialNB().fit(train_matrix,train['categories'])
    print(classifier.score(test_matrix,test["categories"]))

    TextLearning.text_vectorizer = text_vectorizer
    TextLearning.recommendation_model = classifier
开发者ID:imachraoui,项目名称:Offline_entertainer,代码行数:27,代码来源:articles_service.py

示例15: NaiveBayes

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import score [as 别名]
def NaiveBayes(X_train, X_test, y_train, y_test):
    mod = MultinomialNB()
    mod.fit(X_train, y_train)
    print "Done training"
    nb_labels = mod.predict(X_test)
    print "Done testing"
    nb_score = mod.score(X_test, y_test)
    return nb_score, nb_labels
开发者ID:maniarathi,项目名称:takethislifedata,代码行数:10,代码来源:linclassifer.py


注:本文中的sklearn.naive_bayes.MultinomialNB.score方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。