本文整理汇总了Python中sklearn.ensemble.AdaBoostClassifier.score方法的典型用法代码示例。如果您正苦于以下问题:Python AdaBoostClassifier.score方法的具体用法?Python AdaBoostClassifier.score怎么用?Python AdaBoostClassifier.score使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.AdaBoostClassifier
的用法示例。
在下文中一共展示了AdaBoostClassifier.score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cook
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def cook():
x, y, weights = load_data()
n_components = 200
svd = TruncatedSVD(n_components, random_state=42)
x_unweighted = svd.fit_transform(x)
x_weighted = svd.fit_transform(weighted(x, weights))
for i in range(9):
frac = 1 - (i * 0.01 + 0.01)
print frac
x_train, x_test, y_train, y_test = train_test_split(x_unweighted, y, test_size=frac)
classifier = AdaBoostClassifier(n_estimators=100)
classifier.fit(x_train, y_train)
print "Unweighted: ", classifier.score(x_test, y_test)
x_train, x_test, y_train, y_test = train_test_split(x_weighted, y, test_size=frac)
classifier = AdaBoostClassifier(n_estimators=100)
classifier.fit(x_train, y_train)
print "Weighted: ", classifier.score(x_test, y_test)
print '--------------------------'
'''
示例2: cvalidate
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def cvalidate():
from sklearn import cross_validation
trainset = np.genfromtxt(open('train.csv','r'), delimiter=',')[1:]
X = np.array([x[1:8] for x in trainset])
y = np.array([x[8] for x in trainset])
#print X,y
import math
for i, x in enumerate(X):
for j, xx in enumerate(x):
if(math.isnan(xx)):
X[i][j] = 26.6
#print X[0:3]
#print y[0:3]
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size = 0.3, random_state = 0)
X_train, X_test = decomposition_pca(X_train, X_test)
bdt = AdaBoostClassifier(base_estimator = KNeighborsClassifier(n_neighbors=20, algorithm = 'auto'), algorithm="SAMME", n_estimators = 200)
bdt.fit(X_train, y_train)
print bdt.score(X_test, y_test)
示例3: __init__
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
class AdaBoost:
def __init__(self, data, n_estimators=50, learning_rate=1.0):
features, weights, labels = data
self.clf = AdaBoostClassifier(n_estimators=n_estimators, learning_rate=learning_rate)
self.predictions, self.trnaccuracy, self.tstaccuracy = None, None, None
self.dataset = split_dataset(features, weights, labels)
def train(self):
"""
Train Ada Boost on the higgs dataset
"""
self.clf = self.clf.fit(self.dataset['training']['features'], self.dataset['training']['labels'])
def predict(self):
"""
Predict label using Ada Boost
:return:
"""
self.predictions = self.clf.predict(self.dataset['test']['features'])
def evaluate(self):
self.trnaccuracy = self.clf.score(self.dataset['training']['features'],
self.dataset['training']['labels'],
sample_weight=self.dataset['training']['weights'])
self.tstaccuracy = self.clf.score(self.dataset['test']['features'],
self.dataset['test']['labels'],
sample_weight=self.dataset['test']['weights'])
示例4: test_staged_predict
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def test_staged_predict():
"""Check staged predictions."""
# AdaBoost classification
for alg in ['SAMME', 'SAMME.R']:
clf = AdaBoostClassifier(algorithm=alg, n_estimators=10)
clf.fit(iris.data, iris.target)
predictions = clf.predict(iris.data)
staged_predictions = [p for p in clf.staged_predict(iris.data)]
proba = clf.predict_proba(iris.data)
staged_probas = [p for p in clf.staged_predict_proba(iris.data)]
score = clf.score(iris.data, iris.target)
staged_scores = [s for s in clf.staged_score(iris.data, iris.target)]
assert_equal(len(staged_predictions), 10)
assert_array_almost_equal(predictions, staged_predictions[-1])
assert_equal(len(staged_probas), 10)
assert_array_almost_equal(proba, staged_probas[-1])
assert_equal(len(staged_scores), 10)
assert_array_almost_equal(score, staged_scores[-1])
# AdaBoost regression
clf = AdaBoostRegressor(n_estimators=10)
clf.fit(boston.data, boston.target)
predictions = clf.predict(boston.data)
staged_predictions = [p for p in clf.staged_predict(boston.data)]
score = clf.score(boston.data, boston.target)
staged_scores = [s for s in clf.staged_score(boston.data, boston.target)]
assert_equal(len(staged_predictions), 10)
assert_array_almost_equal(predictions, staged_predictions[-1])
assert_equal(len(staged_scores), 10)
assert_array_almost_equal(score, staged_scores[-1])
示例5: test_pickle
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def test_pickle():
# Check pickability.
import pickle
# Adaboost classifier
for alg in ['SAMME', 'SAMME.R']:
obj = AdaBoostClassifier(algorithm=alg)
obj.fit(iris.data, iris.target)
score = obj.score(iris.data, iris.target)
s = pickle.dumps(obj)
obj2 = pickle.loads(s)
assert_equal(type(obj2), obj.__class__)
score2 = obj2.score(iris.data, iris.target)
assert_equal(score, score2)
# Adaboost regressor
obj = AdaBoostRegressor(random_state=0)
obj.fit(boston.data, boston.target)
score = obj.score(boston.data, boston.target)
s = pickle.dumps(obj)
obj2 = pickle.loads(s)
assert_equal(type(obj2), obj.__class__)
score2 = obj2.score(boston.data, boston.target)
assert_equal(score, score2)
示例6: Model_Adaboost
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
class Model_Adaboost(object):
def __init__(self,model,parameter = {"n_estimators" : 50, "CV_size": 0}):
self.train = model.train
self.test = model.test
self.CVsize = float(parameter["CV_size"].get())
train = np.array(self.train)
self.X_train = train[:, :-1]
self.y_train = train[:, -1]
self.X_train,self.X_CV,self.y_train,self.y_CV = train_test_split(self.X_train, self.y_train, test_size=self.CVsize)
if self.CVsize == 0:
self.clf = AdaBoostClassifier(n_estimators = int(parameter["n_estimators"].get()))
self.model = model
def fit(self):
self.clf.fit(self.X_train,self.y_train)
def score(self):
pre = self.clf.predict(self.X_train)
truth = self.y_train
print ("score: " + str(self.clf.score(self.X_train,truth)))
print ("f1: " + str(f1_score(truth,pre, average=None)))
print ("AUC score: " + str(roc_auc_score(truth,pre)))
def save_results(self):
pre = self.model.clf.predict(self.model.test)
df = pd.DataFrame({"predict":pre})
fileName = tkFileDialog.asksaveasfilename()
df.to_csv(fileName)
def crossValidation(self):
estimatorList = [3,5,7,10,13,15,20,25,30,50]
bestScore = [0,0] #score,n_estimator
bestF1ScoreNeg = [0,0]
bestF1ScorePos = [0,0]
#bestAUCScore = [0,0]
for e in estimatorList:
self.clf = AdaBoostClassifier(n_estimators = e)
self.clf.fit(self.X_train,self.y_train)
pre = self.clf.predict(self.X_CV)
truth = self.y_CV
score = self.clf.score(self.X_CV,truth)
if score > bestScore[0]:
bestScore[0] = score
bestScore[1] = e
f1pos = f1_score(truth,pre, average=None)[1]
if f1pos > bestF1ScorePos[0]:
bestF1ScorePos[0] = f1pos
bestF1ScorePos[1] = e
f1neg = f1_score(truth,pre, average=None)[0]
if f1neg > bestF1ScoreNeg[0]:
bestF1ScoreNeg[0] = f1neg
bestF1ScoreNeg[1] = e
print ("Adaboost:")
print ("Best [score,n_estimators] on Cross Validation set: " + str(bestScore))
print ("Best [f1(pos),n_estimators] on Cross Validation set: " + str(bestF1ScorePos))
print ("Best [f1(neg),n_estimators] on Cross Validation set" + str(bestF1ScoreNeg))
示例7: boost_report
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def boost_report():
svm_train_features = list()
svm_train_classes = list()
svm_test_features = list()
svm_test_classes = list()
for record in mit_records:
svm_train_features.append(list(record.features.values()))
svm_train_classes.append(record.my_class)
for record in mim_records:
svm_test_features.append(list(record.features.values()))
svm_test_classes.append(record.my_class)
svm_classifier = svm.SVC(kernel="linear", C=0.1)
svm_classifier.fit(svm_train_features, svm_train_classes)
print("linear kernel svm accuracy: " +
str(svm_classifier.score(svm_test_features, svm_test_classes)))
classifier = AdaBoostClassifier(
base_estimator=svm_classifier,
n_estimators=100,
algorithm='SAMME')
classifier.fit(svm_train_features, svm_train_classes)
print("adaboost accuracy: " +
str(classifier.score(svm_test_features, svm_test_classes)))
示例8: test_iris
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def test_iris():
# Check consistency on dataset iris.
classes = np.unique(iris.target)
clf_samme = prob_samme = None
for alg in ['SAMME', 'SAMME.R']:
clf = AdaBoostClassifier(algorithm=alg)
clf.fit(iris.data, iris.target)
assert_array_equal(classes, clf.classes_)
proba = clf.predict_proba(iris.data)
if alg == "SAMME":
clf_samme = clf
prob_samme = proba
assert_equal(proba.shape[1], len(classes))
assert_equal(clf.decision_function(iris.data).shape[1], len(classes))
score = clf.score(iris.data, iris.target)
assert score > 0.9, "Failed with algorithm %s and score = %f" % \
(alg, score)
# Somewhat hacky regression test: prior to
# ae7adc880d624615a34bafdb1d75ef67051b8200,
# predict_proba returned SAMME.R values for SAMME.
clf_samme.algorithm = "SAMME.R"
assert_array_less(0,
np.abs(clf_samme.predict_proba(iris.data) - prob_samme))
示例9: prediction
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def prediction(feat,label):
x_train, x_test, y_train, y_test = cross_validation.train_test_split(feat, label, test_size = 0.25, random_state = 0)
num_leaves = []
accuracy_score = []
auc_score = []
# for depth in range(1,10):
# clf = tree.DecisionTreeClassifier(max_depth = depth)
# clf.fit(x_train,y_train)
# predictions = clf.predict(x_test)
# accuracy = clf.score(x_test,y_test)
# auc = metrics.roc_auc_score(y_test,predictions)
# num_leaves.append(depth)
# accuracy_score.append(accuracy)
# auc_score.append(auc)
for depth in range(1,10):
clf = AdaBoostClassifier(tree.DecisionTreeClassifier(max_depth = depth), n_estimators = 100)
clf.fit(x_train,y_train)
predictions = clf.predict(x_test)
accuracy = clf.score(x_test,y_test)
auc = metrics.roc_auc_score(y_test,predictions)
num_leaves.append(depth)
accuracy_score.append(accuracy)
auc_score.append(auc)
return num_leaves,accuracy_score,auc_score
示例10: ADA_Classifier
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def ADA_Classifier(X_train, X_cv, X_test, Y_train,Y_cv,Y_test, Actual_DS):
print("***************Starting AdaBoost Classifier***************")
t0 = time()
clf = AdaBoostClassifier(n_estimators=300)
clf.fit(X_train, Y_train)
preds = clf.predict(X_cv)
score = clf.score(X_cv,Y_cv)
print("AdaBoost Classifier - {0:.2f}%".format(100 * score))
Summary = pd.crosstab(label_enc.inverse_transform(Y_cv), label_enc.inverse_transform(preds),
rownames=['actual'], colnames=['preds'])
Summary['pct'] = (Summary.divide(Summary.sum(axis=1), axis=1)).max(axis=1)*100
print(Summary)
#Check with log loss function
epsilon = 1e-15
#ll_output = log_loss_func(Y_cv, preds, epsilon)
preds2 = clf.predict_proba(X_cv)
ll_output2= log_loss(Y_cv, preds2, eps=1e-15, normalize=True)
print(ll_output2)
print("done in %0.3fs" % (time() - t0))
preds3 = clf.predict_proba(X_test)
#preds4 = clf.predict_proba((Actual_DS.ix[:,'feat_1':]))
preds4 = clf.predict_proba(Actual_DS)
print("***************Ending AdaBoost Classifier***************")
return pd.DataFrame(preds2) , pd.DataFrame(preds3),pd.DataFrame(preds4)
示例11: adaboost_skin
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def adaboost_skin(X_train, y_train, X_test, y_test):
"""Learn the skin data sets with AdaBoost.
X_*: Samples.
y_*: labels.
"""
print 'AdaBoost'
min_iter = 1
max_iter = 200
steps = 30
diff = (max_iter - min_iter) / steps
iterations = [min_iter + diff * step for step in xrange(steps+1)]
scores = []
for T in iterations:
clf = AdaBoostClassifier(
base_estimator=DecisionTreeClassifier(max_depth=1),
algorithm="SAMME",
n_estimators=T)
clf.fit(X_train.toarray(), y_train)
scores.append(100 * clf.score(X_test.toarray(), y_test))
print '\t%d Iterations: %.2f%%' % (T, scores[-1])
return iterations, scores
示例12: adaboost
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def adaboost(df,label_name,feature_names,features_len,ifeat,n_estimators=100):
# TODO: just copied from RF, needs real code
from sklearn.ensemble import AdaBoostClassifier
print('---------------------------------------------------')
print(ifeat,features_len,'Adaboost, features:',feature_names)
df_train_Y = df[label_name]
train_Y = df_train_Y.values.ravel() # turn from 2D to 1D
df_train_X = df[feature_names]
train_X = df_train_X.values
clf =AdaBoostClassifier(n_estimators=n_estimators)
clf = clf.fit(train_X,train_Y)
# output = clf.predict(train_X)
E_in = round(1.-clf.score(train_X, train_Y),5) # 'in sample' error
#print('\tE_in :',E_in)
# -----
# Kfold as estimator for 'out of sample' error
kf=skl.cross_validation.KFold(n=len(train_X), n_folds=5)
cv_scores=skl.cross_validation.cross_val_score(clf, train_X, y=train_Y, cv=kf)
E_out = round(1.-np.mean(cv_scores),5)
#print("\tE_out:",E_out)
return E_in,E_out
示例13: AB_results
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def AB_results(): # AdaBoostClassifier
print "--------------AdaBoostClassifier-----------------"
rang = [60, 80]
# print "--------------With HOG-----------------"
# ans = []
# print "n_estimators Accuracy"
# for i in rang:
# clf = AdaBoostClassifier(n_estimators=i)
# clf.fit(X_train_hog, y_train)
# mean_accuracy = clf.score(X_test_hog, y_test)
# print i, " ", mean_accuracy
# ans.append('('+str(i)+", "+str(mean_accuracy)+')')
# print ans
# plt.plot(rang, ans, linewidth=2.0)
# plt.xlabel("n_estimators")
# plt.ylabel("mean_accuracy")
# plt.savefig("temp_hog.png")
print "\n--------------Without HOG-----------------"
ans = []
print "n_estimators Accuracy"
for i in rang:
clf = AdaBoostClassifier(n_estimators=i)
clf.fit(X_train, y_train)
mean_accuracy = clf.score(X_test, y_test)
print i, " ", mean_accuracy
ans.append('('+str(i)+", "+str(mean_accuracy)+')')
print ans
plt.plot(rang, ans, linewidth=2.0)
plt.xlabel("n_estimators")
plt.ylabel("mean_accuracy")
plt.savefig("temp_plain.png")
示例14: AdaBoostcls
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
class AdaBoostcls(object):
"""docstring for ClassName"""
def __init__(self):
self.adaboost_cls = AdaBoostClassifier()
self.prediction = None
self.train_x = None
self.train_y = None
def train_model(self, train_x, train_y):
try:
self.train_x = train_x
self.train_y = train_y
self.adaboost_cls.fit(train_x, train_y)
except:
print(traceback.format_exc())
def predict(self, test_x):
try:
self.test_x = test_x
self.prediction = self.adaboost_cls.predict(test_x)
return self.prediction
except:
print(traceback.format_exc())
def accuracy_score(self, test_y):
try:
# return r2_score(test_y, self.prediction)
return self.adaboost_cls.score(self.test_x, test_y)
except:
print(traceback.format_exc())
示例15: boost_report
# 需要导入模块: from sklearn.ensemble import AdaBoostClassifier [as 别名]
# 或者: from sklearn.ensemble.AdaBoostClassifier import score [as 别名]
def boost_report(test_split_size):
scd_count = 0
for record in records:
if (record.my_class == "SCD"):
scd_count += 1
print(scd_count)
shuffle(records)
split = int(len(records) * (1 / test_split_size))
print(len(records))
train_set = records[:(len(records) - split)]
test_set = records[split:]
print("split:", test_split_size, "train:", len(train_set), "test:", split)
svm_train_features = list()
svm_train_classes = list()
svm_test_features = list()
svm_test_classes = list()
for record in train_set:
svm_train_features.append(list(record.features.values()))
svm_train_classes.append(record.my_class)
for record in test_set:
svm_test_features.append(list(record.features.values()))
svm_test_classes.append(record.my_class)
svm_classifier = svm.SVC(kernel="linear", C=0.1)
svm_classifier.fit(svm_train_features, svm_train_classes)
print("linear kernel svm accuracy: " +
str(svm_classifier.score(svm_test_features, svm_test_classes)))
classifier = AdaBoostClassifier(
base_estimator=svm_classifier,
n_estimators=50,
algorithm='SAMME'
)
classifier.fit(svm_train_features, svm_train_classes)
print("adaboost accuracy: " +
str(classifier.score(svm_test_features, svm_test_classes)))
classifier2 = AdaBoostClassifier(
n_estimators=50,
algorithm='SAMME'
)
classifier2.fit(svm_train_features, svm_train_classes)
print("adaboost2 accuracy: " +
str(classifier2.score(svm_test_features, svm_test_classes)))