【机器学习 sklearn】XGBoost and RandomForest

数据集下载网站:
http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt

对比随机决策森林以及XGBboost模型对泰坦尼克号上的乘客是否生还进行预测。

#coding:utf-8
from __future__ import division
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import time
start_time = time.time()


import pandas as pd
titanic = pd.read_csv('titanic.txt')

X = titanic[['pclass', 'age', 'sex']]
y = titanic['survived']

# print X
X['age'].fillna(X['age'].mean(), inplace=True)


from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)


from sklearn.feature_extraction import DictVectorizer


vec = DictVectorizer(sparse=False)
X_train = vec.fit_transform(X_train.to_dict(orient='record'))
X_test = vec.transform(X_test.to_dict(orient='record'))

from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier()
rfc.fit(X_train, y_train)
print 'The accuracy of Random Forest Classifier on testing set:', rfc.score(X_test, y_test)



# 利用训练好的模型对测试样本进行预测,预测结果储存在变量y_predict中。
rfc_y_predict = rfc.predict(X_test)



# 依然使用sklearn.metrics里面的classification_report模块对预测结果做更加详细的分析。
from sklearn.metrics import classification_report
print classification_report(y_test, rfc_y_predict)



from xgboost import XGBClassifier

xgbc = XGBClassifier()

xgbc.fit(X_train, y_train)

xgbc_y_predict = xgbc.predict(X_test)
print 'The accuracy of eXtreme Gradient Boosting Classifier on testing set:', xgbc.score(X_test, y_test)

## 依然使用sklearn.metrics里面的classification_report模块对预测结果做更加详细的分析。
from sklearn.metrics import classification_report

print classification_report(y_test, xgbc_y_predict)


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页