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


Python GaussianNB.partial_fit方法代码示例

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


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

示例1: test_gnb_sample_weight

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
def test_gnb_sample_weight():
    """Test whether sample weights are properly used in GNB. """
    # Sample weights all being 1 should not change results
    sw = np.ones(6)
    clf = GaussianNB().fit(X, y)
    clf_sw = GaussianNB().fit(X, y, sw)

    assert_array_almost_equal(clf.theta_, clf_sw.theta_)
    assert_array_almost_equal(clf.sigma_, clf_sw.sigma_)

    # Fitting twice with half sample-weights should result
    # in same result as fitting once with full weights
    sw = rng.rand(y.shape[0])
    clf1 = GaussianNB().fit(X, y, sample_weight=sw)
    clf2 = GaussianNB().partial_fit(X, y, classes=[1, 2], sample_weight=sw / 2)
    clf2.partial_fit(X, y, sample_weight=sw / 2)

    assert_array_almost_equal(clf1.theta_, clf2.theta_)
    assert_array_almost_equal(clf1.sigma_, clf2.sigma_)

    # Check that duplicate entries and correspondingly increased sample
    # weights yield the same result
    ind = rng.randint(0, X.shape[0], 20)
    sample_weight = np.bincount(ind, minlength=X.shape[0])

    clf_dupl = GaussianNB().fit(X[ind], y[ind])
    clf_sw = GaussianNB().fit(X, y, sample_weight)

    assert_array_almost_equal(clf_dupl.theta_, clf_sw.theta_)
    assert_array_almost_equal(clf_dupl.sigma_, clf_sw.sigma_)
开发者ID:daidan,项目名称:MLearning,代码行数:32,代码来源:test_naive_bayes.py

示例2: bayes_test

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
def bayes_test():
    X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    Y = np.array([1, 1, 1, 2, 2, 2])
    clf = GaussianNB()
    clf.fit(X, Y)
    print(clf.predict([[-0.8, -1]]))
    clf_pf = GaussianNB()
    clf_pf.partial_fit(X, Y, np.unique(Y))
    print(clf_pf.predict([[-0.8, -1]]))
开发者ID:texpine,项目名称:ud120-projects,代码行数:11,代码来源:explore_enron_data.py

示例3: test_gnb_partial_fit

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
def test_gnb_partial_fit():
    clf = GaussianNB().fit(X, y)
    clf_pf = GaussianNB().partial_fit(X, y, np.unique(y))
    assert_array_almost_equal(clf.theta_, clf_pf.theta_)
    assert_array_almost_equal(clf.sigma_, clf_pf.sigma_)
    assert_array_almost_equal(clf.class_prior_, clf_pf.class_prior_)

    clf_pf2 = GaussianNB().partial_fit(X[0::2, :], y[0::2], np.unique(y))
    clf_pf2.partial_fit(X[1::2], y[1::2])
    assert_array_almost_equal(clf.theta_, clf_pf2.theta_)
    assert_array_almost_equal(clf.sigma_, clf_pf2.sigma_)
    assert_array_almost_equal(clf.class_prior_, clf_pf2.class_prior_)
开发者ID:daidan,项目名称:MLearning,代码行数:14,代码来源:test_naive_bayes.py

示例4: DrunkLearningNB

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
class DrunkLearningNB(DrunkLearning):
    """drunk_learning class"""
    def __init__(self):
        super(DrunkLearningNB, self).__init__()
        self.clf = GaussianNB()
        self.filename = 'modelNB.pkl'

    def partial_fit(self, X, y):
        X = np.array([X])
        y = np.array(y)
        self.clf.partial_fit(X, y, [0, 1])
        joblib.dump(self.clf, self.filename, compress=9)
开发者ID:jackbeckwith,项目名称:drunk_learning,代码行数:14,代码来源:drunk_learning.py

示例5: predict

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
    def predict(self):
            ClassifierN = 0
            classifier = GaussianNB()
            X_train,X_test,y_train,y_test = train_test_split(self.training,self.trainingLabels,test_size=0.75,random_state=33)
            classifier.fit(X_train, y_train)
            # predict UnLabledData
            #pred_labelsForTrainingUn = classifier.predict(X_test)
            print 'Enhanced classifier...'
            while 1:
                if len(X_test)<=5: # min
                    break         #min
                proba_labelsForTrainingUn = classifier.predict_proba(X_test)
                X_test_labels = np.hstack((X_test, proba_labelsForTrainingUn))
                X_test_labels0_sort = sorted(X_test_labels,key=lambda x:x[5],reverse=True)
                if X_test_labels0_sort[4][5]>X_test_labels0_sort[4][6]:
                    a = map(lambda x: x[:5], X_test_labels0_sort)
                    b = a[0:5]
                    classifier.partial_fit(b, ['0','0','0','0','0'], classes=['0', '1'],sample_weight=np.ones(len(b), dtype=np.float) * self.Lambda)
                    X_test_labels = X_test_labels0_sort[5:]
                    X_test = a[5:]
                if len(X_test)<6: # min
                    break         #min

                X_test_labels0_sort = sorted(X_test_labels, key=lambda x: x[5], reverse=True)
                if X_test_labels0_sort[4][5]<=X_test_labels0_sort[4][6]: #min
                    a = map(lambda x: x[:5], X_test_labels0_sort)
                    b = a[0:5]
                    classifier.partial_fit(b, ['1', '1', '1', '1', '1'], classes=['0', '1'],sample_weight=np.ones(len(b), dtype=np.float) * 1)
                    X_test_labels = X_test_labels0_sort[5:]  # min
                    X_test = a[5:]
                if len(X_test)<6:
                    break
            # while 1 :
            #     p1 = pred_labelsForTrainingUn
            #     # 将带λ参数的无标签数据拟合入分类器
            #     classifier.partial_fit(X_test, pred_labelsForTrainingUn,classes=['0','1'], sample_weight=np.ones(len(X_test),dtype=np.float)*self.Lambda)
            #     pred_labelsForTrainingUn = classifier.predict(X_test)
            #     p2 = pred_labelsForTrainingUn
            #     # 判断分类器是否稳定
            #     if list(p1)==list(p2) :
            #         ClassifierN += 1
            #     elif ClassifierN > 0:
            #         ClassifierN = 0
            #     if ClassifierN == 20:
            #         break
            pred_labels = classifier.predict(self.test)
            print 'naive_bayes with EM algorithm:'
            return pred_labels
开发者ID:CoderWZW,项目名称:SDLib,代码行数:50,代码来源:SemiSAD.py

示例6: trainClassifier

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
def trainClassifier(partition_data,cols):
      #### Naive Bayes 
	gnb = GaussianNB()
      #### TODO change and put in parallel
 
	data=np.array(list(partition_data[1]))
	#the first call to partial_fit must include a list of all the classes 
	gnb=gnb.partial_fit(data[:,0:cols.value],data[:,cols.value],[0,1]) 
	return gnb
开发者ID:xaviercallens,项目名称:MLSentimentAnalysisDataCamp,代码行数:11,代码来源:helpers2.py

示例7: genCharsChrom

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
def genCharsChrom():
    """ Return chromosome (list) of length l, each of which is made up of the characters from chars. 
		
		pre:
			isinstance(l, int)
			hasattr(chars, '__getitem__')
			hasattr(chars, '__len__')
			len(chars) > 0
		
		post[l, chars]:
			__old__.l == l
			__old__.chars == chars
			len(__return__) == l
			forall(__return__, lambda a: a in chars)
	"""

    l = np.random.random_integers(0, 99, 20)

    for i in range(0, 100):
        if Y[i] == 1:
            # print("!!!!!!")
            l = np.append(l, [i])
            break

    for i in range(0, 100):
        if Y[i] == -1:
            # print("######")
            l = np.append(l, [i])
            break

    x = X[l]
    y = Y[l]

    clf = GaussianNB()

    clf = clf.partial_fit(x, y, [1, -1])

    return clf
开发者ID:royaljain,项目名称:ChangingPopulation,代码行数:40,代码来源:population.py

示例8: GaussianNB

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X, Y)
GaussianNB()
print(clf.predict([[-0.8, -1]]))

clf_pf = GaussianNB()
clf_pf.partial_fit(X, Y, np.unique(Y))
GaussianNB()
print(clf_pf.predict([[-0.8, -1]]))
开发者ID:marlomajor,项目名称:intro_to_machine_learning,代码行数:16,代码来源:gaussian_nb_example.py

示例9: GaussianNB

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
import numpy as np
import pandas as pd
from sklearn import cross_validation

print '---------- Naive Bayes -----------'

df = pd.read_csv("~/Desktop/My DM/Baltimore/Baltimore.csv",low_memory=False)

features = ["Month of the Crime","Mean Temperature","Mean Dew Point","Mean Visibility","Max Humidity","Mean Wind Speed","Max Sea Level"]

x = df[features]
y = df["Crime Type"]

print 'Partial Fit - training classifier'
clf_pf = GaussianNB()
clf_pf.partial_fit(x, y, np.unique(y))

print '--Cross Validation--'
scores = cross_validation.cross_val_score(clf_pf, x, y, cv=5)
print scores.mean()

print '--Random Split--'
X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(x, y, test_size=0.2, random_state=0)
clf1 = GaussianNB().fit(X_train, Y_train)
print clf1.score(X_test, Y_test)

# Test file
df_test = pd.read_csv("~/Desktop/My DM/Baltimore/Test_Baltimore.csv",low_memory=False)
xt = df_test[features]
print 'Partial Fit Predicted - '+str(clf_pf.predict(xt))
print 'Predict Probability - '+str(clf_pf.predict_proba(xt))
开发者ID:Pallavi-Madasu,项目名称:DataMining,代码行数:33,代码来源:Naive_Bayes_Classifier.py

示例10: GaussianNB

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
trainLabel1 = []
clf1 = GaussianNB()
iter = 1

for sentence in sentences:
    text = sentence.string
    x = vec.vector(text)
    trainData.append(x)
    if sentence.get('opinionated') == 'N': emotion = 'none'
    else: emotion = sentence.get('emotion-1-type')
    trainLabel1.append(label[emotion])
    if len(trainData) >= 1000:
	trainData = array(trainData)
	trainLabel1 = array(trainLabel1)
	print 'Training emotion 1...%d' % iter
	clf1.partial_fit(trainData, trainLabel1, unique(trainLabel1))
	trainData = []
	trainLabel1 = []
	iter += 1

print 'Testing...'
output = []
soup = BeautifulSoup(''.join(open('../data/Testing data for Emotion Classification.xml').readlines()))
weibos = soup.find_all('weibo')
for weibo in weibos:
    weibo_id = weibo.get('id')
    if int(weibo_id) > 6000: break
    text = ''
    sentences = weibo.find_all('sentence')
    for sentence in sentences:
	text += sentence.string + ' '
开发者ID:huangshenno1,项目名称:practice,代码行数:33,代码来源:test.py

示例11: normalize

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
classes = [1, 2, 3, 4]
dump_path = "dump.pkl"

try:
    # Loads dump if exists
    clf = joblib.load(dump_path)
except IOError:
    # No dump, loads base samples
    base = np.loadtxt("base.txt")
    y_base = base[:,0] # col 0: delay in minutes
    X_base = base[:,[1,2,3]] # cols 1(wind spd), 2(dew diff), 3(sky cover)
    normalize(X_base)
    
    # translate continuous to discrete output for classifier
    y_base[y_base <= 5] = 1
    y_base[np.logical_and(y_base > 5, y_base <= 30)] = 2
    y_base[np.logical_and(y_base > 30, y_base <= 60)] = 3
    y_base[y_base > 60] = 4
    
    clf = GaussianNB()
    clf.partial_fit(X_base, y_base, classes)
    joblib.dump(clf, 'dump.pkl')

while True:
    # receive data from web app
    (clientsocket, address) = s.accept()
    t = threading.Thread(target=worker, args=(clientsocket,))
    t.start()
开发者ID:MarcosCM,项目名称:NASA-SpaceApps2016-TimeFlies,代码行数:33,代码来源:complete.py

示例12: Bayes

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
class Bayes(object):
    def __init__(self):
        self.clf = GaussianNB()

    def histo_plot(self, data):
        (mu, sigma) = norm.fit(data)
        # the histogram of the data
        n, bins, patches = plt.hist(data, 30, normed=1, facecolor='green', alpha=0.75)

        # add a 'best fit' line
        y = mlab.normpdf( bins, mu, sigma)
        l = plt.plot(bins, y, 'r--', linewidth=2)

    def training(self):
        # Training data: a and b
        # b is twice, three times, four times of a
        a = np.random.normal(1, 0.1, 3000)
        b = np.append(np.random.normal(2, 0.2, 1000), np.random.normal(3, 0.3, 1000))
        b = np.append(b, np.random.normal(4, 0.4, 1000))

        # X: b/a, Y: type, 2/3/4
        X = b/a
        Y = np.append(np.ones(1000) * 2, np.ones(1000) * 3)
        Y = np.append(Y, np.ones(1000) * 4)

        # Plot the histogram of b/a
        self.histo_plot(X[:1000])
        self.histo_plot(X[1000:2000])
        self.histo_plot(X[2000:3000])
        plt.xlabel('b/a')
        plt.ylabel('Probability')
        plt.title('Histogram of the training set b/a')
        plt.grid(True)
        plt.show()

        # Plot b/a
        plt.plot(X)
        plt.plot(Y, marker='o', markersize=5, label='Type')
        plt.legend()
        plt.title("Training Set b/a, and types")
        plt.ylabel("b/a")
        plt.xlabel("time (s)")
        plt.show()

        # Bayes Classifier
        X = X.reshape(-1, 1)
        self.clf.partial_fit(X, Y, np.unique(Y))
        print self.clf.class_prior_
        print self.clf.theta_
        print self.clf.sigma_

    def testing(self):
        # Testing data, c and d
        c = np.random.normal(2, 0.2, 300)
        d = np.append(np.random.normal(4, 0.4, 100), np.random.normal(6, 1, 100))
        d = np.append(d, np.random.normal(8, 1, 100))

        # X1: testing set, Y1: predicted result
        X1 = d/c
        Y1 = []
        for item in X1:
            Y1.append(self.clf.predict([[item]]))

        # Plot the histogram of d/c
        self.histo_plot(X1[:100])
        self.histo_plot(X1[100:200])
        self.histo_plot(X1[200:300])
        plt.xlabel('d/c')
        plt.ylabel('Probability')
        plt.title('Histogram of the testing set d/c')
        plt.grid(True)
        plt.show()

        # Plot d/c
        plt.plot(X1)
        plt.plot(Y1, marker='o', markersize=5, label='Type')
        plt.legend()
        plt.title("Testing Set d/c, and types")
        plt.ylabel("d/c")
        plt.xlabel("time (s)")
        plt.show()
开发者ID:mangodream,项目名称:Bayes,代码行数:83,代码来源:bayes_save.py

示例13: GaussianNB

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
#-*- encoding:utf-8 -*-
r'''
高斯贝叶斯分类器本质是一个线性分类器,
它是贝叶斯分类器在联合概率分布满足
高斯分布的特殊情况
'''
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
print clf.fit(X, Y)
#>>GaussianNB()
print(clf.predict([[-0.8, -1],[2,4]]))
#>>[1 2]


#批量训练,即将数据集分成一块块,对于大数据集十分有效
clf_pf = GaussianNB()
print clf_pf.partial_fit(X, Y,np.unique(Y))
#>>GaussianNB()
print(clf_pf.predict([[-0.8, -1],[2,4]]))
#>>[1 2]
开发者ID:sixiong,项目名称:machine-learning,代码行数:26,代码来源:Gaussian+Naive+Bayes.py

示例14: Evaluator

# 需要导入模块: from sklearn.naive_bayes import GaussianNB [as 别名]
# 或者: from sklearn.naive_bayes.GaussianNB import partial_fit [as 别名]
class Evaluator(EvaluatorBase):
    '''
    This Evaluator requires the following for each module
    [model]
    screenfile    naivebayes_screen_model
    sleepfile    naivebayes_sleep_model
    history        5
    minsleeptime    240
    minscreentime    240
    '''


    def __init__(self, config):
        '''
        Constructor
        Accepts a module configuration dictionary
        '''
        self.config = config
        self.historylen = config.get("model",{}).get("history",5)
        self.history = [0.0]*(len(self.modules)*self.historylen)
        #TODO: Discover these.
        self.modules = self.config.get("modules",[])
        self.modules = self.modules +['bias']
                                                  
        self.screenfile = config.get("model",{}).get("screenfile",os.environ.get("HOME")+"naivebayesscreenmodel")
        if os.path.isfile(self.screenfile):
            self.screenclassifier = pickle.load(self.screenfile)
        else:
            self.screenclassifier = GaussianNB()
            self.screenclassifier.fit(np.asarray(self.history, dtype = np.float32),np.zeros([1]))
        self.sleepfile = config.get("model",{}).get("sleepfile",os.environ.get("HOME")+"naivebayessleepmodel")
        if os.path.isfile(self.sleepfile):
            self.sleepclassifier = pickle.load(self.sleepfile)
        else:
            self.sleepclassifier = GaussianNB()
            self.sleepclassifier.fit(np.asarray(self.history, dtype = np.float32),np.zeros([1]))
        
    def eval(self, metrics):
        
        #preload expected inputs at 0.0
        inputs = {}
        for m in self.modules:
            inputs[m] = 0.0
        inputs['bias'] = 1.0
        #update given inputs
        for modulename,metric in metrics.iteritems():
            inputs[modulename] = metric
        #Transform into consistent order vector
        inputvector = []
        for inputname in sorted(self.modules):
            inputvector.append(inputs[inputname])
        self.history = self.history[0:-len(self.modules)] + inputvector
        npinvec = np.asarray(self.history, dtype=np.float32)
        sleepdecision = self.sleepclassifier.predict(npinvec)[0] == 1
        screendecision = self.sleepclassifier.predict(npinvec)[0] == 1
        return {'sleep':sleepdecision,
                'screenoff':screendecision}
        
    def update(self,timeslept, timescreenoff):
        #TODO: Use this knowledge about how long the sleep lasted to update the ML model.
        self.sleepclassifier.partial_fit(np.asarray(self.history, dtype=np.float32) ,
                                         np.asarray([timeslept > self.config.get("model",{}).get("minsleeptime",240)],dtype=np.int ))
        self.screenclassifier.partial_fit(np.asarray(self.history, dtype=np.float32) ,
                                         np.asarray([timescreenoff > self.config.get("model",{}).get("minscreentime",240)],dtype=np.int ))
        
开发者ID:lovi9573,项目名称:sleepybox,代码行数:66,代码来源:NaiveBayes.py


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