コードコードコードコード自体のコメント
7173 ワード
コードとコメントは以下の通りです.
#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# :
# http://scikit-learn.org/stable/auto_examples/ensemble/plot_feature_transformation.html#sphx-glr-auto-examples-ensemble-plot-feature-transformation-py
import numpy as np
np.random.seed(10)
np.set_printoptions(threshold = 1e6)#
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import (RandomTreesEmbedding, RandomForestClassifier,GradientBoostingClassifier)
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.pipeline import make_pipeline
n_estimator = 10
X, y = make_classification(n_samples=100)#
# ,0 1
# y_train X_train
# y_test X_test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
#
# It is important to train the ensemble of trees on a different subset
# of the training data than the linear regression model to avoid
# overfitting, in particular if the total number of leaves is
# similar to the number of training samples
#y_train X_train
#y_train_lr X_train_lr
X_train, X_train_lr, y_train, y_train_lr = train_test_split(X_train, y_train,test_size=0.5)
#
print"---------------------------------------------------------------------------------------"
print"y_train",y_train
print"---------------------------------------------------------------------------------------"
print"y_train_lr",y_train_lr
print"---------------------------------------------------------------------------------------"
# :
#X_train
#y_train
#
#X_test
#y_test
#
#X_train_lr
#y_train_lr
#
# Unsupervised transformation based on totally random trees
#----------------------------- + ---------------------------------------------------------
rt_lm = LogisticRegression()#
rt = RandomTreesEmbedding(max_depth=3, n_estimators=n_estimator,random_state=0)# , ,
pipeline = make_pipeline(rt, rt_lm)#
print"X_train=",X_train
print"y_train=",y_train
pipeline.fit(X_train, y_train)
print"X_test",X_test
y_pred_rt = pipeline.predict_proba(X_test)[:, 1]# 1 , pipeline.predict_proba(X_test) , 0 0 , 1 1
fpr_rt_lm, tpr_rt_lm, _ = roc_curve(y_test, y_pred_rt)# rf ,lr roc
#
# pr_rt_lm =False-Positive-Ratio _ RandomForestTree _ LogisticRegression-Model
# tpr_rt_lm=True -Positive-Ratio _ RandomForestTree _ LogisticRegression-Model
# rf=RandomForestClassifier
#-------------------------------------- ----------------------------------------------------------------------------
# Supervised transformation based on random forests
rf = RandomForestClassifier(max_depth=3, n_estimators=n_estimator)
rf.fit(X_train, y_train)
y_pred_rf = rf.predict_proba(X_test)[:, 1]#
fpr_rf, tpr_rf, _ = roc_curve(y_test, y_pred_rf)
# y_pred_rf, y_test roc (y_test y_pred_rf ,roc_curve )
#----------------------------- apply ( )+ + ---------------------------------------------------------
rf_enc = OneHotEncoder()
rf_lm = LogisticRegression()
rf_enc.fit(rf.apply(X_train))
rf_lm.fit(rf_enc.transform(rf.apply(X_train_lr)), y_train_lr)# transform , predict,y_train_lr
y_pred_rf_lm = rf_lm.predict_proba(rf_enc.transform(rf.apply(X_test)))[:, 1]# transofrm 。[:, 1] 1
fpr_rf_lm, tpr_rf_lm, _ = roc_curve(y_test, y_pred_rf_lm)
#----------------------------- ----------------------------------------------------------
grd = GradientBoostingClassifier(n_estimators=n_estimator)
grd.fit(X_train, y_train)
y_pred_grd = grd.predict_proba(X_test)[:, 1]#
fpr_grd, tpr_grd, _ = roc_curve(y_test, y_pred_grd)#_
#----------------------------- apply + + ----------------------------------------------------------
grd_enc = OneHotEncoder()#
grd_enc.fit(grd.apply(X_train)[:, :, 0])
grd_lm = LogisticRegression()
print" ",type(grd.apply(X_train_lr)[:, :, 0])#X_train_lr ,
print"grd.apply(X_train_lr)[:, :, 0]=",grd.apply(X_train_lr)
grd_lm.fit(grd_enc.transform(grd.apply(X_train_lr)[:, :, 0]), y_train_lr)#X_train_lr ( ),y_train_lr X_train_lr
y_pred_grd_lm = grd_lm.predict_proba(grd_enc.transform(grd.apply(X_test)[:, :, 0]))[:, 1]# , transform ,[:, 1] 1
# [:, :, 0] nd.arrary , , :
# [[[3],
# [2],
# [3]],
# [[3],
# [3],
# [4]]]
fpr_grd_lm, tpr_grd_lm, _ = roc_curve(y_test, y_pred_grd_lm)
# :
#X_test ( )
#y_test X_test
#☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
# , , , X
# TP vs FP(True Positive vs False Positive)
plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')# ,
# :
# https://blog.csdn.net/ztf312/article/details/49933497
# :plot(x,y)
plt.plot(fpr_rt_lm, tpr_rt_lm, label='RT + LR')# embedding +
plt.plot(fpr_rf, tpr_rf, label='RF')#
plt.plot(fpr_rf_lm, tpr_rf_lm, label='RF + LR')# apply + +
plt.plot(fpr_grd, tpr_grd, label='GBT')#
plt.plot(fpr_grd_lm,tpr_grd_lm,label='GBT + LR')# + +
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.show()
print"----------------------------- 1 --------------------------------------------"
plt.figure(2)
plt.xlim(0, 0.2)
plt.ylim(0.8, 1)# xlim ylim
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_rt_lm, tpr_rt_lm, label='RT + LR')# embedding +
plt.plot(fpr_rf, tpr_rf, label='RF')#
plt.plot(fpr_rf_lm, tpr_rf_lm, label='RF + LR')# apply + +
plt.plot(fpr_grd, tpr_grd, label='GBT')#
plt.plot(fpr_grd_lm,tpr_grd_lm,label='GBT + LR')# + +
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve (zoomed in at top left)')
plt.legend(loc='best')
plt.show()
print"----------------------------- 2 --------------------------------------------"
運転結果は以下の通りです.