1.XGboost在python中安装
首先 需要从作者github上clone并且编译C++版本(Ubuntu)
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost; make -j4
然后进入目录中python-packge下
sudo python setup.py install
安装完后正常导入就好
import xgboost as xgb
2.使用XGBClassifier
from xgboost import XGBClassifier
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
xgbc = XGBClassifier()
xgbc.fit(train_inf, train_target)
predict = xgbc.predict(test_inf) #返回预测结果
xgbc.score(test_inf, test_target) #获得平均准确率
print f1_score(test_target, predict) #获得F1值
print recall_score(test_target, predict) #获得召回率
3.使用随机森林
#随机森林的使用
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
from sklearn.metrics import zero_one_loss
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(train_inf, train_target) #训练
clf_predict = clf.predict(test_inf) #返回预测结果
print clf.score(test_inf, test_target) #返回准确率
print f1_score(test_target, predict) #返回F1值
print recall_score(test_target, predict) #返回召回率
print zero_one_loss(test_target, predict) #返回0-1损失
4.使用KNN
#KNN的使用
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
from sklearn.metrics import zero_one_loss
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(train_inf, train_target) #训练
neigh_predict = neigh.predict(test_inf) #返回预测结果
print neigh.score(test_inf, test_target) #返回准确率
print f1_score(test_target, neigh_predict) #返回F1值
print recall_score(test_target, neigh_predict) #返回召回率
print zero_one_loss(test_target, neigh_predict) #返回0-1损失
5.使用朴素贝叶斯
#朴素贝叶斯
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
from sklearn.metrics import zero_one_loss
Gbayes = GaussianNB()
Gbayes = Gbayes.fit(train_inf, train_target) #训练
Gbayes_predict = Gbayes.predict(test_inf) #返回预测结果
print Gbayes.score(test_inf, test_target) #返回准确率
print f1_score(test_target, Gbayes_predict) #返回F1值
print recall_score(test_target, Gbayes_predict) #返回召回率
print zero_one_loss(test_target, Gbayes_predict) #返回0-1损失
- 地方