应用场景:用户流失
本文将介绍模型调用预测的步骤,这里深度学习模型使用的是自定义的deepfm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from collections import defaultdict
from scipy import stats
from scipy import signal
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, f1_score
from scipy.spatial.distance import cosine
import lightgbm as lgb
from sklearn.preprocessing import LabelEncoder, MinMaxScaler, StandardScaler
from tensorflow.keras.layers import *
import tensorflow.keras.backend as K
import tensorflow as tf
from tensorflow.keras.models import Model
import os,gc,re,warnings,sys,math
warnings.filterwarnings("ignore")
pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
data = pd.read_csv('df_04m.csv')
sparse_cols = ['shop_id','sex']
dense_cols = [c for c in data.columns if c not in sparse_cols + ['customer_id', 'flag', 'duartion_is_lm']]
def process_dense_feats(data, cols):
d = data.copy()
for f in cols:
d[f] = d[f].fillna(0)
ss=StandardScaler()
d[f] = ss.fit_transform(d[[f]])
return d
data = process_dense_feats(data, dense_cols)
def process_sparse_feats(data, cols):
d = data.copy()
for f in cols:
d[f] = d[f].fillna('-1').astype(str)
label_encoder = LabelEncoder()
d[f] = label_encoder.fit_transform(d[f])
return d
data = process_sparse_feats(data, sparse_cols)
y = data['flag']
X = data.drop(['customer_id', 'flag', 'duartion_is_lm'], axis = 1)
X_sparse_x = [X[f].values for f in sparse_cols]
X_dense_x = [X[f].values for f in dense_cols]
y_label = [y.values]
X_sparse_x
loaded_model = tf.keras.models.load_model('deepfm_model.h5')
deepfm_prob = loaded_model.predict(X_sparse_x+X_dense_x, batch_size=4096*4, verbose=1)
deepfm_prob.shape
deepfm_prob
df_submit = pd.DataFrame()
df_submit = data
df_submit['prob'] = deepfm_prob
df_submit.head(3)
df_submit.shape
df_submit['y_pre'] = ''
df_submit['y_pre'].loc[(df_submit['prob']>=0.5)] = 1
df_submit['y_pre'].loc[(df_submit['prob']<0.5)] = 0
df_submit.head(3)
df_submit = df_submit.reset_index()
df_submit.head(1)
df_submit = df_submit.drop('index', axis = 1)
df_submit.head(1)
df_submit.groupby(['flag', 'y_pre'])['customer_id'].count()
precision =
recall =
参考资料:自己琢磨将资料整合