AI 获客系统中线索识别与筛选功能的示例代码。该示例会模拟客户数据,并利用逻辑回归模型对潜在线索进行识别和筛选。AI矩阵获客系统,AI矩阵获客系统开发
代码说明
数据生成:generate_mock_data 函数创建了一个模拟的客户数据集,包含年龄、收入、购买频率、平均购买金额以及是否为潜在线索的标签。
数据预处理:preprocess_data 函数提取特征矩阵 X 和目标变量 y,并使用 StandardScaler 对特征矩阵进行标准化处理,以确保所有特征具有相同的尺度。
模型训练:
train_model 函数将数据集划分为训练集和测试集,使用逻辑回归模型进行训练。
计算模型在测试集上的准确率并输出。
线索识别与筛选:
identify_and_filter_leads 函数使用训练好的模型对所有客户数据进行预测,得到每个客户成为潜在线索的概率。
假设概率大于 0.5 的客户为潜在线索,返回一个布尔数组表示每个客户是否为潜在线索。
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
# 生成模拟客户数据
def generate_mock_data():
data = {
'age': [25, 30, 35, 40, 45, 22, 28, 32, 38, 42],
'income': [50000, 60000, 70000, 80000, 90000, 45000, 55000, 65000, 75000, 85000],
'purchase_frequency': [5, 3, 7, 2, 4, 6, 8, 1, 3, 5],
'average_purchase_amount': [100, 200, 150, 300, 250, 80, 120, 220, 180, 280],
'is_lead': [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] # 1 表示潜在线索,0 表示非线索
}
return pd.DataFrame(data)
# 数据预处理
def preprocess_data(df):
# 提取特征和目标变量
X = df.drop('is_lead', axis=1)
y = df['is_lead']
# 数据标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
return X_scaled, y
# 训练逻辑回归模型
def train_model(X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
# 模型评估
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"模型准确率: {accuracy}")
return model
# 线索识别与筛选
def identify_and_filter_leads(model, X):
lead_probabilities = model.predict_proba(X)[:, 1]
# 假设概率大于 0.5 为潜在线索
potential_leads = lead_probabilities > 0.5
return potential_leads
if __name__ == "__main__":
# 生成模拟数据
df = generate_mock_data()
# 数据预处理
X_scaled, y = preprocess_data(df)
# 训练模型
model = train_model(X_scaled, y)
# 线索识别与筛选
potential_leads = identify_and_filter_leads(model, X_scaled)
print("潜在线索识别结果:")
print(potential_leads)