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


Python MultinomialNB.set_params方法代码示例

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


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

示例1: sk_learn_NB

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import set_params [as 别名]
def sk_learn_NB(a, jane_data, sherlock_data, k, e, l) :
    NB = MultinomialNB()
    NB.set_params(alpha=a)

    #Combine the data for each snippet into one data list and create a target list with corresponding class labels
    data = []
    target = []
    for d in jane_data :
        data.append(d)
        target.append(0)
    for d in sherlock_data :
        data.append(d)
        target.append(1)   
    data_array = numpy.array(data)
    target_array = numpy.array(target)
    
    #K-fold cross-validation
    kf = cross_validation.KFold(len(data), n_folds = k, shuffle = True)
    fold_stats = []
    for train_index, test_index in kf:
        data_train, data_test = data_array[train_index], data_array[test_index]
        target_train, target_test = target_array[train_index], target_array[test_index]            
        NB.fit(data_train, target_train)
        find_error(NB.predict(data_test), target_test, fold_stats)
            
    #Determining average stats for k-fold cross-validation
    recall_overall_avg = 0.0
    precision_overall_avg = 0.0
    fstat_overall_avg = 0.0
    recall_list = []
    precision_list = []
    fstat_list = []
    for i in range(0,len(fold_stats)) :
        recall_overall_avg = recall_overall_avg + fold_stats[i][0]
        precision_overall_avg = precision_overall_avg + fold_stats[i][1]
        fstat_overall_avg = fstat_overall_avg + fold_stats[i][2]
        recall_list.append(fold_stats[i][0])
        precision_list.append(fold_stats[i][1])
        fstat_list.append(fold_stats[i][2])
    recall_overall_avg = recall_overall_avg/len(fold_stats)
    precision_overall_avg = precision_overall_avg/len(fold_stats)
    fstat_overall_avg = fstat_overall_avg/len(fold_stats)
    var_recall = numpy.var(recall_list)
    var_precision = numpy.var(precision_list)
    var_fstat = numpy.var(fstat_list)
    print("Average stats for", k, "-fold cross-validation with l=", l, "and e=", e)
    print("Recall: ", recall_overall_avg, "with variance:", var_recall)
    print("Precision: ", precision_overall_avg, "with variance:", var_precision)
    print("F-statistic: ", fstat_overall_avg, "with variance:", var_fstat)
开发者ID:jluke13,项目名称:CS5525_HW1,代码行数:51,代码来源:HW1_Q7.py

示例2: alpha_search

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import set_params [as 别名]
def alpha_search(vectorizer):
  dg = DataGatherer()
  y_test = dg.validate_target
  y_train = dg.labeled_target
  X_train = vectorizer.fit_transform(dg.labeled_data)    
  X_test = vectorizer.transform(dg.validate_data)
  nb = MultinomialNB()
  alpha = 0.0
  best_score = -1
  best_alpha = None
  while(alpha < 5):
    alpha += 0.5
    nb.set_params(alpha = alpha)
    nb.fit(X_train, y_train)
    score = nb.score(X_test, y_test)
    if score > best_score:
      best_score = score
      best_alpha = alpha
    print(str(alpha) + "," + str(score))
  print("")
  print("Best alph: " + str(best_alpha))
  print("Best score: " + str(best_score))
开发者ID:Web5design,项目名称:big-data,代码行数:24,代码来源:naive_bayes_optimizer.py

示例3: __init__

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import set_params [as 别名]
class SparseModel:
    model = None
    model_type = None
    default = None
    column_names = None
    class_index = None
    params = None
    trained = None
    w = None
    b = None
    metric = None

    def __init__(self, model_type=None, column_names=None, metric='f1', **kwargs):
        self.model_type = model_type
        self.column_names = column_names
        self.params = kwargs
        self.trained = None
        self.metric = metric
        if model_type == 'LR':
            if self.params.get('regularization', None) is None:
                self.params['regularization'] = 'l1'
            if self.params.get('alpha', None) is None:
                self.params['alpha'] = 1.0
            self.model = lr(penalty=self.params['regularization'], C=self.params['alpha'])
        elif model_type == 'SVM' or model_type == 'SVMNB':
            if self.params.get('kernel', None) is None:
                self.params['kernel'] = 'rbf'

            if model_type == 'SVM':
                if self.params.get('alpha', None) is None:
                    self.params['alpha'] = 0.1
            else:  # elif model_type == SVMNB:
                self.params['kernel'] = 'linear'
                if self.params.get('alpha', None) is None:
                    self.params['alpha'] = 1
                if self.params.get('beta', None) is None:
                    self.params['beta'] = 0.25

            if self.params['kernel'] == 'linear':
                # override regularization parameter to avoid a conflict
                self.params['regularization'] = 'l2'
                self.model = svm.LinearSVC(C=self.params['alpha'])
            else:  # elif self.params['kernel'] != 'linear':
                if self.params.get('degree', None) is None:
                    self.params['degree'] = 3
                if self.params.get('gamma', None) is None:
                    self.params['gamma'] = 0.0
                if self.params.get('coef0', None) is None:
                    self.params['coef0'] = 0.0
                self.model = svm.SVC(C=self.params['alpha'], kernel=self.params['kernel'], degree=self.params['degree'],
                                     gamma=self.params['gamma'], coef0=self.params['coef0'])
        elif model_type == 'MNB':
            if 'alpha' not in self.params:
                self.params['alpha'] = 1.0
            self.model = MultinomialNB(alpha=self.params['alpha'], fit_prior=True)
        elif model_type == 'myMNB':
            if 'alpha' not in self.params:
                self.params['alpha'] = 1.0
            self.model = None
        else:
            self.model_type = 'default'
            self.model = None


    def set_alpha(self, alpha):
        self.params['alpha'] = alpha
        if self.model_type == 'LR':
            self.model.set_params(C=alpha)
        elif self.model_type == 'SVM' or self.model_type == 'SVMNB':
            self.model.set_params(C=alpha)
        elif self.model_type == 'MNB':
            self.model.set_params(alpha=float(alpha))

    def fit(self, X, y, sample_weights=None):
        n_items, n_features = X.shape
        if y.sum() == 0 or y.sum() == n_items or self.model_type == 'default':
            counts = np.bincount(y)
            y_mode = np.argmax(counts)
            self.default = y_mode
            self.model = None
            self.model_type = 'default'
        elif self.model_type == 'myMNB':
            # assumes y = {0,1}
            assert len(np.bincount(y)) == 2
            index_0 = np.array(y) == 0
            index_1 = np.array(y) == 1
            p = np.sum(X.toarray()[index_1, :], axis=0) + float(self.params['alpha'])
            q = np.sum(X.toarray()[index_0, :], axis=0) + float(self.params['alpha'])
            self.w = np.log((p / float(np.sum(np.abs(p)))) / (q / float(np.sum(np.abs(q)))))
            self.b = np.log(np.sum(index_1) / float(np.sum(index_0)))
            if np.isnan(self.w).any():
                sys.exit("nans encountered in " + str(self.__class__) + "." + self.fit.__name__)
        elif self.model_type == 'SVMNB':
            # assumes y = {0,1}
            assert len(np.bincount(y)) == 2
            # first calculate the inputs we would use for MNB
            X_dense = X.toarray()
            _, n_features = X_dense.shape
            index_0 = np.array(y) == 0
            index_1 = np.array(y) == 1
#.........这里部分代码省略.........
开发者ID:dallascard,项目名称:guac,代码行数:103,代码来源:sparse_model.py

示例4: __init__

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import set_params [as 别名]
class SparseModel:
    model = None
    model_type = None
    default = None
    column_names = None
    params = None
    trained = None
    w = None
    b = None

    def __init__(self, model_type=None, column_names=None, **kwargs):
        self.model_type = model_type
        self.column_names = column_names
        self.params = kwargs
        self.trained = None
        if model_type == 'LR':
            if self.params.get('regularization', None) is None:
                self.params['regularization'] = 'l1'
            if self.params.get('alpha', None) is None:
                self.params['alpha'] = 1.0
            self.model = lr(penalty=self.params['regularization'], C=self.params['alpha'])
        elif model_type == 'SVM' or model_type == 'SVMNB':
            if self.params.get('kernel', None) is None:
                self.params['kernel'] = 'rbf'

            if model_type == 'SVM':
                if self.params.get('alpha', None) is None:
                    self.params['alpha'] = 0.1
            else:  # elif model_type == SVMNB:
                self.params['kernel'] = 'linear'
                if self.params.get('alpha', None) is None:
                    self.params['alpha'] = 1
                if self.params.get('beta', None) is None:
                    self.params['beta'] = 0.25

            if self.params['kernel'] == 'linear':
                # override regularization parameter to avoid a conflict
                self.params['regularization'] = 'l2'
                self.model = svm.LinearSVC(C=self.params['alpha'])
            else:  # elif self.params['kernel'] != 'linear':
                if self.params.get('degree', None) is None:
                    self.params['degree'] = 3
                if self.params.get('gamma', None) is None:
                    self.params['gamma'] = 0.0
                if self.params.get('coef0', None) is None:
                    self.params['coef0'] = 0.0
                self.model = svm.SVC(C=self.params['alpha'], kernel=self.params['kernel'], degree=self.params['degree'],
                                     gamma=self.params['gamma'], coef0=self.params['coef0'])
        elif model_type == 'MNB':
            if 'alpha' not in self.params:
                self.params['alpha'] = 1.0
            self.model = MultinomialNB(alpha=self.params['alpha'], fit_prior=True)
        elif model_type == 'myMNB':
            if 'alpha' not in self.params:
                self.params['alpha'] = 1.0
            self.model = None
        else:
            self.model_type = 'default'
            self.model = None

    def set_alpha(self, alpha):
        self.params['alpha'] = alpha
        if self.model_type == 'LR':
            self.model.set_params(C=alpha)
        elif self.model_type == 'SVM' or self.model_type == 'SVMNB':
            self.model.set_params(C=alpha)
        elif self.model_type == 'MNB':
            self.model.set_params(alpha=float(alpha))

    def fit(self, X, y):
        n_items, n_features = X.shape
        if y.sum() == 0 or y.sum() == n_items or self.model_type == 'default':
            counts = np.bincount(y)
            y_mode = np.argmax(counts)
            self.default = y_mode
            self.model = None
            self.model_type = 'default'
        elif self.model_type == 'myMNB':
            # assumes y = {0,1}
            assert len(np.bincount(y)) == 2
            index_0 = np.array(y) == 0
            index_1 = np.array(y) == 1
            p = np.sum(X.toarray()[index_1, :], axis=0) + float(self.params['alpha'])
            q = np.sum(X.toarray()[index_0, :], axis=0) + float(self.params['alpha'])
            self.w = np.log((p / float(np.sum(np.abs(p)))) / (q / float(np.sum(np.abs(q)))))
            self.b = np.log(np.sum(index_1) / float(np.sum(index_0)))
            if np.isnan(self.w).any():
                sys.exit("nans encountered in " + str(self.__class__) + "." + self.fit.__name__)
        elif self.model_type == 'SVMNB':
            # assumes y = {0,1}
            assert len(np.bincount(y)) == 2
            # first calculate the inputs we would use for MNB
            X_dense = X.toarray()
            _, n_features = X_dense.shape
            index_0 = np.array(y) == 0
            index_1 = np.array(y) == 1
            # assume a hyperparameter for MNB of 1.0
            p = np.sum(X_dense[index_1, :], axis=0) + 1
            q = np.sum(X_dense[index_0, :], axis=0) + 1
            r = np.log((p / float(np.sum(np.abs(p)))) / (q / float(np.sum(np.abs(q)))))
#.........这里部分代码省略.........
开发者ID:brian-lau,项目名称:guac,代码行数:103,代码来源:sparse_model.py

示例5: __init__

# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import set_params [as 别名]
class NaiveBayes:
    def __init__(self, alpha):
        self.classifier = MultinomialNB(alpha=alpha, fit_prior=False)
        self.creation_ts = datetime.now()

    def train(self, training_set):

        features = []
        classes = []
        for index, entry in training_set.iterrows():
            features.append(entry[['Osfamily', 'Publisher', 'ArticleDistance']])
            # This is for when article popularity is known
            # features.append(entry[['UserClicksAd', 'Osfamily', 'Publisher', 'ArticleDistance', 'ArticlePopularity']])
            classes.append(entry['Output'])

        print('... Training the model')
        self.classifier.fit(features, classes)

    def classification(self, train_data):

        features = []

        for index, entry in train_data.iterrows():
            features.append(entry[['Osfamily', 'Publisher', 'ArticleDistance']])
            # This is for when article popularity is known
            # features.append(entry[['UserClicksAd', 'Osfamily', 'Publisher', 'ArticleDistance', 'ArticlePopularity']])
        print('... Started classification')
        results_prob = self.classifier.predict(features)
        return results_prob

    def predict_prob(self, train_data):

        features = []

        for index, entry in train_data.iterrows():
            features.append(entry[['Osfamily', 'Publisher', 'ArticleDistance']])
            # This is for when article popularity is known
            # features.append(entry[['UserClicksAd', 'Osfamily', 'Publisher', 'ArticleDistance', 'ArticlePopularity']])

        print('... Started classification')
        results_prob = self.classifier.predict_proba(features)
        return results_prob

    def series_predict_prob(self, data):

        features = []
        features.append(data[['Osfamily', 'Publisher', 'ArticleDistance']])
        # This is for when article popularity is known
        # features.append(entry[['UserClicksAd', 'Osfamily', 'Publisher', 'ArticleDistance', 'ArticlePopularity']])

        results_prob = self.classifier.predict_proba(features)
        return results_prob

    def classification_CV(self, train_data, number_of_folds):

        features = []
        for index, entry in train_data.iterrows():
            features.append(entry[['Osfamily', 'Publisher', 'ArticleDistance']])
            # This is for when article popularity is known
            # features.append(entry[['UserClicksAd', 'Osfamily', 'Publisher', 'ArticleDistance', 'ArticlePopularity']])

        print('... Started crossfold validation')
        score = cross_validation.cross_val_score(self.classifier, features, train_data.Output, cv=number_of_folds,
                                                 scoring='f1')
        return score

    def set_priors(self, priors):
        # sets priors of naive bayes component to given values
        if priors[0] + priors[1] == 1:
            self.classifier.set_params(class_prior=priors)
        else:
            print 'Priors should some up to 1.'
            exit()
开发者ID:ffahimi,项目名称:naive_recommender,代码行数:75,代码来源:NaiveBayes.py


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