机器学习之路: python 实践 提升树 XGBoost 分类器

git: https://github.com/linyi0604/MachineLearning

数据集被我下载到本地,可以去我的git上拿数据集

XGBoost
提升分类器
属于集成学习模型
把成百上千个分类准确率较低的树模型组合起来
不断迭代,每次迭代生成一颗新的树


下面 对泰坦尼克遇难预测
使用XGBoost模型 和 其他分类器性能进行比较

 1 import pandas as pd
 2 from sklearn.cross_validation import train_test_split
 3 from sklearn.feature_extraction import DictVectorizer
 4 from sklearn.ensemble import RandomForestClassifier
 5 from xgboost import XGBClassifier
 6 
 7 '''
 8 XGBoost
 9 提升分类器
10     属于集成学习模型
11     把成百上千个分类准确率较低的树模型组合起来
12     不断迭代,每次迭代生成一颗新的树
13     
14     
15 下面 对泰坦尼克遇难预测
16 使用XGBoost模型 和 其他分类器性能进行比较
17 
18 '''
19 
20 titanic = pd.read_csv("../data/titanic/titanic.txt")
21 # 抽取pclass age 和 sex 作为训练样本
22 x = titanic[["pclass", "age", "sex"]]
23 y = titanic["survived"]
24 # 采集的age空的用平均数补全
25 x["age"].fillna(x["age"].mean(), inplace=True)
26 
27 # 分割训练数据和测试数据
28 x_train, x_test, y_train, y_test = train_test_split(x,
29                                                     y,
30                                                     test_size=0.25,
31                                                     random_state=33)
32 # 提取字典特征 进行 向量化
33 vec = DictVectorizer()
34 x_train = vec.fit_transform(x_train.to_dict(orient="record"))
35 x_test = vec.transform(x_test.to_dict(orient="record"))
36 
37 # 采用默认配置的随机森林进行预测
38 rfc = RandomForestClassifier()
39 rfc.fit(x_train, y_train)
40 print("随机森林预测准确率:", rfc.score(x_test, y_test))  # 0.7811550151975684
41 
42 # 采用XGBoost模型进行预测
43 xgbc = XGBClassifier()
44 xgbc.fit(x_train, y_train)
45 print("XGBoost预测准确率:", xgbc.score(x_test, y_test))  # 0.7872340425531915

猜你喜欢

转载自www.cnblogs.com/Lin-Yi/p/9009271.html