基於僱員流失率數據進行多分類模型訓練及閾值調整實踐-大數據ML樣本集案例實戰

版權聲明:本套技術專欄是做者(秦凱新)平時工做的總結和昇華,經過從真實商業環境抽取案例進行總結和分享,並給出商業應用的調優建議和集羣環境容量規劃等內容,請持續關注本套博客。QQ郵箱地址:1120746959@qq.com,若有任何學術交流,可隨時聯繫。dom

1 數據的預處理分析

from __future__ import division
import pandas as pd
import numpy as np

churn_df = pd.read_csv('churn.csv')
col_names = churn_df.columns.tolist()

print "Column names:"
print col_names

#前六個後六個
to_show = col_names[:6] + col_names[-6:]

print "\nSample data:"
churn_df[to_show].head(6)
複製代碼

2 數據標準化處理

churn_result = churn_df['Churn?']
y = np.where(churn_result == 'True.',1,0)

# We don't need these columns
to_drop = ['State','Area Code','Phone','Churn?']
churn_feat_space = churn_df.drop(to_drop,axis=1)

# 'yes'/'no' has to be converted to boolean values
# NumPy converts these from boolean to 1. and 0. later
yes_no_cols = ["Int'l Plan","VMail Plan"]
churn_feat_space[yes_no_cols] = churn_feat_space[yes_no_cols] == 'yes'

# Pull out features for future use
features = churn_feat_space.columns

X = churn_feat_space.as_matrix().astype(np.float)

# This is important
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)

print "Feature space holds %d observations and %d features" % X.shape
print "Unique target labels:", np.unique(y)
print X[0]
print len(y[y == 0])

Feature space holds 3333 observations and 17 features
Unique target labels: [0 1]
[ 0.67648946 -0.32758048  1.6170861   1.23488274  1.56676695  0.47664315
  1.56703625 -0.07060962 -0.05594035 -0.07042665  0.86674322 -0.46549436
  0.86602851 -0.08500823 -0.60119509 -0.0856905  -0.42793202]
2850
複製代碼

3 sklearn多模型封裝(已廢棄,學思想)

from sklearn.cross_validation import KFold

def run_cv(X,y,clf_class,**kwargs):
    # Construct a kfolds object
    kf = KFold(len(y),n_folds=5,shuffle=True)
    y_pred = y.copy()

    # Iterate through folds
    for train_index, test_index in kf:
        X_train, X_test = X[train_index], X[test_index]
        y_train = y[train_index]
        # Initialize a classifier with key word arguments
        clf = clf_class(**kwargs)
        clf.fit(X_train,y_train)
        y_pred[test_index] = clf.predict(X_test)
    return y_pred
    
    from sklearn.svm import SVC
    from sklearn.ensemble import RandomForestClassifier as RF
    from sklearn.neighbors import KNeighborsClassifier as KNN
    
    def accuracy(y_true,y_pred):
        # NumPy interprets True and False as 1. and 0.
        return np.mean(y_true == y_pred)
    
    print "Support vector machines:"
    print "%.3f" % accuracy(y, run_cv(X,y,SVC))
    print "Random forest:"
    print "%.3f" % accuracy(y, run_cv(X,y,RF))
    print "K-nearest-neighbors:"
    print "%.3f" % accuracy(y, run_cv(X,y,KNN))
    
    Support vector machines:
    0.916
    Random forest:
    0.944
    K-nearest-neighbors:
    0.893
複製代碼

4 閾值機率調整

def run_prob_cv(X, y, clf_class, **kwargs):
        kf = KFold(len(y), n_folds=5, shuffle=True)
        y_prob = np.zeros((len(y),2))
        for train_index, test_index in kf:
            X_train, X_test = X[train_index], X[test_index]
            y_train = y[train_index]
            clf = clf_class(**kwargs)
            clf.fit(X_train,y_train)
            # Predict probabilities, not classes
            y_prob[test_index] = clf.predict_proba(X_test)
        return y_prob
        
    import warnings
    warnings.filterwarnings('ignore')
    
    # Use 10 estimators so predictions are all multiples of 0.1
    pred_prob = run_prob_cv(X, y, RF, n_estimators=10)
    #print pred_prob[0]
    pred_churn = pred_prob[:,1]
    is_churn = y == 1
    
    # Number of times a predicted probability is assigned to an observation
    counts = pd.value_counts(pred_churn)
    #print counts
    
    # calculate true probabilities
    true_prob = {}
    for prob in counts.index:
        true_prob[prob] = np.mean(is_churn[pred_churn == prob])
        true_prob = pd.Series(true_prob)
    
    # pandas-fu
    counts = pd.concat([counts,true_prob], axis=1).reset_index()
    counts.columns = ['pred_prob', 'count', 'true_prob']
    counts
    
    # 0.7以上流式率達到94%,說明閾值爲0.7是合適的,低於0.7無論,高於0.7的都認爲是流失的
複製代碼

5 總結

方便複習,整成筆記,內容粗略,勿怪spa

版權聲明:本套技術專欄是做者(秦凱新)平時工做的總結和昇華,經過從真實商業環境抽取案例進行總結和分享,並給出商業應用的調優建議和集羣環境容量規劃等內容,請持續關注本套博客。QQ郵箱地址:1120746959@qq.com,若有任何學術交流,可隨時聯繫。3d

秦凱新 於深圳 20181211056rest

相關文章
相關標籤/搜索