03-逻辑回归-电信客户流失预测

📅 2026/7/10 2:44:50
03-逻辑回归-电信客户流失预测
1. 需求分析已知个人、通话、上网等数据通过分析特征属性确定用户流失的原因以及哪些因素可能导致用户流失。建立预测模型判断用户是否流失。2. 数据说明3. 建模3.1 获取数据data pd.read_csv(./data/churn.csv)3.2 数据预处理# 1. 分类字段one-hot编码 data pd.get_dummies(data, columns[Churn, gender]) # 2. 删除编码后冗余的列 data.drop([Churn_No, gender_Male], axis1, inplaceTrue) # axis0表示行axis1表示列; inplaceTrue表示直接修改原数据 # 3. 修改列名 data.rename(columns{Churn_Yes: flag}, inplaceTrue) # 4. 查看数据分布 print(data.flag.value_counts()) # 不均衡数据 # 5. 可视化绘制计数柱状图 sns.countplot(xContract_Month, datadata, hueflag) # 参数解释x表示横坐标data表示数据源hue表示 plt.show()3.3 特征工程# 1. 提取特征 x data[[Contract_Month, internet_other, PaymentElectronic]] y data[flag] # 2. 特征预处理 x_train, x_test, y_train, y_test train_test_split(x, y, test_size0.2, random_state24)3.4 模型训练estimator LogisticRegression() estimator.fit(x_train, y_train)3.5 模型预测y_pre estimator.predict(x_test)3.6 模型评估# 分类报告 reoprt classification_report(y_test, y_pre) # 画图roc 曲线 fpr, tpr, thresholds roc_curve(y_test, y_pre) auc auc(fpr, tpr) plt.plot(fpr, tpr) plt.plot(fpr, tpr, labelROC curve (area %.2f) % auc) plt.legend() plt.show()