算法原理集成学习(ensemble leaning)通过构建并连系多个学习器来完成学习义务,通过将多个学习器连系,经常可以获得比单一学习器显著优越的效果和泛化能力。集成学习中的基学习器可以是同质的,也可以是异质的。凭证个体学习器的天生方式,现在的集成学习方式大致可分为三大类:一类是Bagging,个体学习器之间不存在强依赖关系,可以同时并行化训练和天生,最终效果通常通过投票机制产出,随机森林是这一类型的代表;另一类是Boosting,个体学习器之间存在强依赖关系,后一学习器依赖前一学习器的效果,,因此必须以序列化形式串行天生,我们下节会讲到的Adaboost和GBDT是这一类型的代表;实在另有第三类,叫Stacking,即将低级学习器的输出次级学习器的输入特征,深层神经网络甚至可以明白为Stacking集成学习的变种。
随机森林(Random Forest)是以决议树为基学习器构建的Bagging集成学习算法,实在现简朴、盘算开销小、而且在许多现实义务中显示出抢眼的效果。其主要通过样本扰动和属性扰动使得集成学习的泛化性显著提高。样本扰动是指通过对初始训练集采样构建每一棵决议树;属性扰动是指对基决议树的每个节点,盘据时从该节点的属性聚集中随机选择k个属性(k一样平常去log(d,2),d为属性数目)。
模子训练代码地址:https://github.com/qianshuang/ml-exp
def train():
print(“start training…”)
# 处置训练数据
train_feature, train_target = process_file(train_dir, word_to_id, cat_to_id)
# 模子训练
model.fit(train_feature, train_target)
def test():
print(“start testing…”)
# 处置测试数据
test_feature, test_target = process_file(test_dir, word_to_id, cat_to_id)
# test_predict = model.predict(test_feature) # 返回展望种别
test_predict_proba = model.predict_proba(test_feature) # 返回属于各个种其余概率
test_predict = np.argmax(test_predict_proba, 1) # 返回概率最大的种别标签
# accuracy
true_false = (test_predict == test_target)
accuracy = np.count_nonzero(true_false) / float(len(test_target))
print()
print(“accuracy is %f” % accuracy)
# precision recall f1-score
print()
print(metrics.classification_report(test_target, test_predict, target_names=categories))
# 混淆矩阵
print(“Confusion Matrix…”)
print(metrics.confusion_matrix(test_target, test_predict))
if not os.path.exists(vocab_dir):
# 构建词典表
全网霸屏营销推广和SEO有什么区别
build_vocab(train_dir, vocab_dir)
categories, cat_to_id = read_category()
words, word_to_id = read_vocab(vocab_dir)
# kNN
# model = neighbors.KNeighborsClassifier()
# decision tree
# model = tree.DecisionTreeClassifier()
# random forest
model = ensemble.RandomForestClassifier(n_estimators=10) # n_estimators为基决议树的数目,一样平常越大效果越好直至趋于收敛
train()
test()运行效果:
ead_category…
read_vocab…
start training…
start testing…
accuracy is 0.875000
precision recall f1-score support
娱乐 0.83 0.91 0.87 89
房产 0.78 0.83 0.80 104
教育 0.81 0.81 0.81 104
家居 0.75 0.71 0.73 89
游戏 0.93 0.95 0.94 104
时政 0.78 0.79 0.78 94
时尚 0.94 0.89 0.92 91
体育 0.98 0.97 0.97 116
财经 0.95 0.91 0.93 115
科技 0.99 0.96 0.97 94
avg / total 0.88 0.88 0.88 1000
Confusion Matrix…
——————-领会更多干货文章,可以关注小程序八斗问答
百度竞价投放效果差转化少,要如何操作才气提