1、数据准备
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score
dpath = './data/'
data = pd.read_csv(dpath+"mushrooms.csv")
data.head()
data.info()
data['class'].unique()
data.shape
2、特征编码
很多模型需要数值型的输入,比如Logisstic回归、xgboost.......
LableEncoder编码后会进行排序,因此是有序的,适用于logistic回归。
而颜色等特征是没有序关系,决策树等模型不care排序,也可以试试OneHotEncoder
from sklearn.preprocessing import LabelEncoder
labelencoder=LabelEncoder()
for col in data.columns:
data[col] = labelencoder.fit_transform(data[col])
data.head()
y = data['class'] #用列名访问更直观
X = data.drop('class', axis = 1)
3、数据标准化
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=4)
columns = X_train.columns
# 数据标准化
from sklearn.preprocessing import StandardScaler
# 分别初始化对特征和目标值的标准化器
ss_X = StandardScaler()
ss_y = StandardScaler()
# 分别对训练和测试数据的特征以及目标值进行标准化处理
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
4、决策树
from sklearn.tree import DecisionTreeClassifier
model_tree = DecisionTreeClassifier()
model_tree.fit(X_train, y_train)
y_prob = model_tree.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities
y_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.
model_tree.score(X_test, y_pred)
print('The AUC of default Desicion Tree is',roc_auc_score(y_test,y_pred))
#查看变量的重要性排序
df = pd.DataFrame({"columns":list(columns), "importance":list(model_tree.feature_importances_.T)})
df.sort_values(by=['importance'],ascending=False)
plt.bar(range(len(model_tree.feature_importances_)), model_tree.feature_importances_)
plt.show()
5、根据特征重要性做特征选择
from numpy import sort
from sklearn.feature_selection import SelectFromModel
# Fit model using each importance as a threshold
thresholds = sort(model_tree.feature_importances_)
for thresh in thresholds:
# select features using threshold
selection = SelectFromModel(model_tree, threshold=thresh, prefit=True)
select_X_train = selection.transform(X_train)
# train model
selection_model = DecisionTreeClassifier()
selection_model.fit(select_X_train, y_train)
# eval model
select_X_test = selection.transform(X_test)
y_pred = selection_model.predict(select_X_test)
predictions = [round(value) for value in y_pred]
accuracy = accuracy_score(y_test, predictions)
print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" % (thresh, select_X_train.shape[1],
accuracy*100.0))
# Fit model using the best threshhold
thresh = 0.020
selection = SelectFromModel(model_tree, threshold=thresh, prefit=True)
select_X_train = selection.transform(X_train)
# train model
selection_model = DecisionTreeClassifier()
selection_model.fit(select_X_train, y_train)
6、导出决策树图
import graphviz
from sklearn import tree
tree.export_graphviz(model_tree, out_file='best_tree.dot')
#liunx系统可在 best_tree.dot所在目录下执行 dot -Tpng best_tree.dot -o best_tree.png 命令
#将决策树dot文件转化成png图
#查看决策树
from PIL import Image
img=Image.open('best_tree.png')
plt.imshow(img)
plt.show()