本文整理汇总了Python中sklearn.ensemble.AdaBoostClassifier.predict_log_proba方法的典型用法代码示例。如果您正苦于以下问题:Python AdaBoostClassifier.predict_log_proba方法的具体用法?Python AdaBoostClassifier.predict_log_proba怎么用?Python AdaBoostClassifier.predict_log_proba使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.AdaBoostClassifier
的用法示例。
在下文中一共展示了AdaBoostClassifier.predict_log_proba方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeSamples
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import predict_log_proba [as 别名]
testSamples = makeSamples(testFiles)
testDescriptors = []
addDescriptors(testDescriptors, testSamples)
testClusters = kmeans.predict(testDescriptors)
testCounts = lil_matrix((len(testSamples), CLUSTERS_NUMBER))
testCounts1 = lil_matrix((len(testSamples), 256))
calculteCounts(testSamples, testCounts, testCounts1, testClusters)
testCounts = csr_matrix(testCounts)
testCounts1 = csr_matrix(testCounts1)
_tfidf = tfidf.transform(testCounts)
_tfidf1 = tfidf1.transform(testCounts1)
weights = clf.predict_log_proba(_tfidf)
weights1 = clf1.predict_log_proba(_tfidf1)
predictions = []
for i in xrange(0, len(weights)):
w = weights[i][0] - weights[i][1]
w1 = weights1[i][0] - weights1[i][1]
pred = w < 0
pred1 = w1 < 0
if pred != pred1:
pred = w + w1 < 0
predictions.append(pred)
match = 0
dismatch = 0
if len(testFiles) == len(predictions):
log = open('log.txt', 'w')
示例2: test_sparse_classification
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import predict_log_proba [as 别名]
def test_sparse_classification():
# Check classification with sparse input.
class CustomSVC(SVC):
"""SVC variant that records the nature of the training set."""
def fit(self, X, y, sample_weight=None):
"""Modification on fit caries data type for later verification."""
super(CustomSVC, self).fit(X, y, sample_weight=sample_weight)
self.data_type_ = type(X)
return self
X, y = datasets.make_multilabel_classification(n_classes=1, n_samples=15,
n_features=5,
random_state=42)
# Flatten y to a 1d array
y = np.ravel(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
for sparse_format in [csc_matrix, csr_matrix, lil_matrix, coo_matrix,
dok_matrix]:
X_train_sparse = sparse_format(X_train)
X_test_sparse = sparse_format(X_test)
# Trained on sparse format
sparse_classifier = AdaBoostClassifier(
base_estimator=CustomSVC(probability=True),
random_state=1,
algorithm="SAMME"
).fit(X_train_sparse, y_train)
# Trained on dense format
dense_classifier = AdaBoostClassifier(
base_estimator=CustomSVC(probability=True),
random_state=1,
algorithm="SAMME"
).fit(X_train, y_train)
# predict
sparse_results = sparse_classifier.predict(X_test_sparse)
dense_results = dense_classifier.predict(X_test)
assert_array_equal(sparse_results, dense_results)
# decision_function
sparse_results = sparse_classifier.decision_function(X_test_sparse)
dense_results = dense_classifier.decision_function(X_test)
assert_array_equal(sparse_results, dense_results)
# predict_log_proba
sparse_results = sparse_classifier.predict_log_proba(X_test_sparse)
dense_results = dense_classifier.predict_log_proba(X_test)
assert_array_equal(sparse_results, dense_results)
# predict_proba
sparse_results = sparse_classifier.predict_proba(X_test_sparse)
dense_results = dense_classifier.predict_proba(X_test)
assert_array_equal(sparse_results, dense_results)
# score
sparse_results = sparse_classifier.score(X_test_sparse, y_test)
dense_results = dense_classifier.score(X_test, y_test)
assert_array_equal(sparse_results, dense_results)
# staged_decision_function
sparse_results = sparse_classifier.staged_decision_function(
X_test_sparse)
dense_results = dense_classifier.staged_decision_function(X_test)
for sprase_res, dense_res in zip(sparse_results, dense_results):
assert_array_equal(sprase_res, dense_res)
# staged_predict
sparse_results = sparse_classifier.staged_predict(X_test_sparse)
dense_results = dense_classifier.staged_predict(X_test)
for sprase_res, dense_res in zip(sparse_results, dense_results):
assert_array_equal(sprase_res, dense_res)
# staged_predict_proba
sparse_results = sparse_classifier.staged_predict_proba(X_test_sparse)
dense_results = dense_classifier.staged_predict_proba(X_test)
for sprase_res, dense_res in zip(sparse_results, dense_results):
assert_array_equal(sprase_res, dense_res)
# staged_score
sparse_results = sparse_classifier.staged_score(X_test_sparse,
y_test)
dense_results = dense_classifier.staged_score(X_test, y_test)
for sprase_res, dense_res in zip(sparse_results, dense_results):
assert_array_equal(sprase_res, dense_res)
# Verify sparsity of data is maintained during training
types = [i.data_type_ for i in sparse_classifier.estimators_]
assert all([(t == csc_matrix or t == csr_matrix)
for t in types])
示例3: __init__
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import predict_log_proba [as 别名]
class PCR:
def __init__(self):
self.__clustersNumber = CLUSTERS_NUMBER
self.__queue = Queue()
self.__verbose = VERBOSE
self.__useCache = USE_CACHE
for i in range(FILE_LOAD_THREADS):
t = Thread(target=self.__worker)
t.daemon = True
t.start()
self.__kmeans = MiniBatchKMeans(
n_clusters=self.__clustersNumber,
random_state=CLUSTER_SEED,
verbose=self.__verbose)
self.__tfidf = TfidfTransformer()
self.__tfidf1 = TfidfTransformer()
self.__clf = AdaBoostClassifier(MultinomialNB(alpha=BAYES_ALPHA), n_estimators=ADA_BOOST_ESTIMATORS)
self.__clf1 = AdaBoostClassifier(MultinomialNB(alpha=BAYES_ALPHA), n_estimators=ADA_BOOST_ESTIMATORS)
def __worker(self):
while True:
task = self.__queue.get()
func, args = task
try:
func(args)
except Exception as e:
print('EXCEPTION:', e)
self.__queue.task_done()
def train(self, positiveFiles, negativeFiles):
cachedData = self.__loadCache()
if cachedData is None:
self.__log('loading positives')
positiveSamples = self.__loadSamples(positiveFiles)
self.__log('loading negatives')
negativeSamples = self.__loadSamples(negativeFiles)
totalDescriptors = []
self.__addDescriptors(totalDescriptors, positiveSamples)
self.__addDescriptors(totalDescriptors, negativeSamples)
self.__kmeans.fit(totalDescriptors)
clusters = self.__kmeans.predict(totalDescriptors)
self.__printDistribution(clusters)
self.__saveCache((positiveSamples, negativeSamples, self.__kmeans, clusters))
else:
self.__log('using cache')
positiveSamples, negativeSamples, self.__kmeans, clusters = cachedData
totalSamplesNumber = len(negativeSamples) + len(positiveSamples)
counts = lil_matrix((totalSamplesNumber, self.__clustersNumber))
counts1 = lil_matrix((totalSamplesNumber, 256))
self.__currentSample = 0
self.__currentDescr = 0
self.__calculteCounts(positiveSamples, counts, counts1, clusters)
self.__calculteCounts(negativeSamples, counts, counts1, clusters)
counts = csr_matrix(counts)
counts1 = csr_matrix(counts1)
self.__log('training bayes classifier')
tfidf = self.__tfidf.fit_transform(counts)
tfidf1 = self.__tfidf1.fit_transform(counts1)
classes = [True] * len(positiveSamples) + [False] * len(negativeSamples)
self.__clf.fit(tfidf, classes)
self.__clf1.fit(tfidf1, classes)
self.__log('training complete')
def predict(self, files):
self.__log('loading files')
samples = self.__loadSamples(files)
totalDescriptors = []
self.__addDescriptors(totalDescriptors, samples)
self.__log('predicting classes')
clusters = self.__kmeans.predict(totalDescriptors)
counts = lil_matrix((len(samples), self.__clustersNumber))
counts1 = lil_matrix((len(samples), 256))
self.__currentSample = 0
self.__currentDescr = 0
self.__calculteCounts(samples, counts, counts1, clusters)
counts = csr_matrix(counts)
counts1 = csr_matrix(counts1)
tfidf = self.__tfidf.transform(counts)
tfidf1 = self.__tfidf1.transform(counts1)
self.__log('classifying')
weights = self.__clf.predict_log_proba(tfidf.toarray())
weights1 = self.__clf1.predict_log_proba(tfidf1.toarray())
predictions = []
for i in range(0, len(weights)):
w = weights[i][0] - weights[i][1]
w1 = weights1[i][0] - weights1[i][1]
#.........这里部分代码省略.........