该项目演示了如何使用决策树分类器来预测客户流失(客户是否离开服务)。该数据集包括年龄、每月费用和客户服务电话等特征,目的是预测客户是否会流失。
模型使用 Scikit-learn 的决策树分类器进行训练,代码将决策树可视化,以便更好地理解模型如何做出决策。
import pandas as pd import matplotlib.pyplot as plt import warnings from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score from sklearn import tree
熊猫(pd):
Matplotlib(plt):
警告(警告):
Scikit-learn 库:
import pandas as pd import matplotlib.pyplot as plt import warnings from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score from sklearn import tree
warnings.filterwarnings("ignore")
在这里,我们为该项目创建一个合成数据集。该数据集模拟了一家电信公司的客户信息,具有年龄、月费、CustomerServiceCalls 和目标变量流失(客户是否流失)等特征。
Pandas DataFrame:数据被构造为 DataFrame (df),一种二维标记数据结构,允许轻松操作和分析数据。
import pandas as pd import matplotlib.pyplot as plt import warnings from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score from sklearn import tree
warnings.filterwarnings("ignore")
data = { 'CustomerID': range(1, 101), # Unique ID for each customer 'Age': [20, 25, 30, 35, 40, 45, 50, 55, 60, 65]*10, # Age of customers 'MonthlyCharge': [50, 60, 70, 80, 90, 100, 110, 120, 130, 140]*10, # Monthly bill amount 'CustomerServiceCalls': [1, 2, 3, 4, 0, 1, 2, 3, 4, 0]*10, # Number of customer service calls 'Churn': ['No', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes']*10 # Churn status } df = pd.DataFrame(data) print(df.head())
X = df[['Age', 'MonthlyCharge', 'CustomerServiceCalls']] # Features y = df['Churn'] # Target Variable
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier() clf.fit(X_train, y_train)
import pandas as pd import matplotlib.pyplot as plt import warnings from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score from sklearn import tree
以上是预测客户流失的决策树分类器示例的详细内容。更多信息请关注PHP中文网其他相关文章!