- 2023/08/24/用户新增预测挑战赛-数据分析与可视化/

任务2.1 数据分析与可视化

# 导入库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 读取训练集和测试集文件
train_data = pd.read_csv('用户新增预测挑战赛公开数据/train.csv')
test_data = pd.read_csv('用户新增预测挑战赛公开数据/test.csv')

# 相关性热力图
sns.heatmap(train_data.corr().abs(), cmap='YlOrRd')
png

通过分析相关性并画热力图,可以看出common_ts与x6, x7与x8具有显著相关性。

# x7分组下标签均值
sns.barplot(x='x7', y='target', data=train_data)
png

x7取值是离散的,说明是类别属性。

- 字段x1至x8为用户相关的属性,为匿名处理字段。添加代码对这些数据字段的取值分析,那些字段为数值类型?那些字段为类别类型?

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 6))
coordinates = [[0, 0], [0, 1], [1, 0], [1, 1]]
types = [1,2,6,8]

# x7分组下标签均值
for i in types:
    ax = axes[coordinates[types.index(i)][0],coordinates[types.index(i)][1]]  # Get the current Axes object
    
    sns.barplot(x=f'x{i}', y='target', data=train_data, ax=ax)  # Plot on the current Axes
    
    ax.set_title(f'x{i} vs Target')  # Set subplot title
    ax.set_xlabel(f'x{i}')  # Set x-axis label
    ax.set_ylabel('Target')  # Set y-axis label

# Adjust layout
plt.tight_layout()

# Show the plots
plt.show()
png

这说明x1,x2,x6,x8均为类别属性。

fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(12, 6))
types = [3,4,5]

# x7分组下标签均值
for i in types:
    ax = axes[types.index(i)]  # Get the current Axes object
    
    sns.barplot(x=f'x{i}', y='target', data=train_data, ax=ax)  # Plot on the current Axes
    
    ax.set_title(f'x{i} vs Target')  # Set subplot title
    ax.set_xlabel(f'x{i}')  # Set x-axis label
    ax.set_ylabel('Target')  # Set y-axis label

# Adjust layout
plt.tight_layout()

# Show the plots
plt.show()
png

这说明x3,x4,x5都是数值属性。

- 对于数值类型的字段,考虑绘制在标签分组下的箱线图。

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12, 6))
types = [3,4,5]

# x7分组下标签均值
for i in types:
    ax = axes[types.index(i)]  # Get the current Axes object

    df = train_data[['target', f'x{i}']].groupby('target')
    df.boxplot(column=f'x{i}', subplots=False, ax=ax) # Plot on the current Axes

# Adjust layout
plt.tight_layout()

# Show the plots
plt.show()
png

注意到x4, x5的分布较为均匀正常,而x3有很多异常值,不适用箱线图。

df = train_data[['target', 'x3']].groupby('target')
for target_value, group_df in df:
    # target_value is the value of the 'target' column for the group
    # group_df is the DataFrame containing the data for that group
    print(f"Target: {target_value}")
    print(group_df)
Target: 0
        target  x3
0            0  41
1            0  41
2            0  41
3            0  41
4            0  41
...        ...  ..
620350       0  41
620351       0  41
620352       0  41
620354       0  41
620355       0  41

[533155 rows x 2 columns]
Target: 1
        target  x3
5            1  41
9            1  41
10           1  41
36           1  41
43           1  41
...        ...  ..
620326       1  41
620343       1  41
620346       1  41
620349       1  41
620353       1  41

[87201 rows x 2 columns]
# Assuming df is the grouped DataFrame as you mentioned: df = train_data[['target', 'x3']].groupby('target')
# Calculate the value counts for the 'x3' column in each group
value_counts_0 = df.get_group(0)['x3'].value_counts()
value_counts_1 = df.get_group(1)['x3'].value_counts()

# Plot the frequency distribution for each target group
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)  # Subplot for target 0
plt.bar(value_counts_0.index, value_counts_0.values)
plt.title("Frequency Distribution of x3 (Target 0)")
plt.xlabel("x3")
plt.ylabel("Frequency")

plt.subplot(1, 2, 2)  # Subplot for target 1
plt.bar(value_counts_1.index, value_counts_1.values)
plt.title("Frequency Distribution of x3 (Target 1)")
plt.xlabel("x3")
plt.ylabel("Frequency")

plt.tight_layout()
plt.show()
png
print(value_counts_0)
print(value_counts_1)
41    531017
47       478
15       265
7        238
71       133
       ...  
69         1
58         1
6          1
63         1
65         1
Name: x3, Length: 66, dtype: int64
41    86836
47       67
5        63
14       57
7        22
60       17
20       16
15       14
71       14
3        13
30        8
40        7
55        6
11        5
35        5
2         5
38        5
50        4
34        4
42        3
59        3
24        3
64        3
58        2
70        2
74        2
25        2
17        1
0         1
36        1
16        1
62        1
72        1
66        1
31        1
26        1
32        1
61        1
18        1
13        1
Name: x3, dtype: int64

这说明x3主要集中在取值41.

- 从common_ts中提取小时,绘制每小时下标签分布的变化。

train_data['common_ts'] = pd.to_datetime(train_data['common_ts'], unit='ms')
train_data['common_ts_hour'] = train_data['common_ts'].dt.hour
sns.barplot(x='common_ts_hour', y='target', data=train_data)
png

- 对udmap进行onehot,统计每个key对应的标签均值,绘制直方图。

def udmap_onethot(d):
    v = np.zeros(9)  # 创建一个长度为 9 的零数组
    if d == 'unknown':  # 如果 'udmap' 的值是 'unknown'
        return v  # 返回零数组
    d = eval(d)  # 将 'udmap' 的值解析为一个字典
    for i in range(1, 10):  # 遍历 'key1' 到 'key9', 注意, 这里不包括10本身
        if 'key' + str(i) in d:  # 如果当前键存在于字典中
            v[i-1] = d['key' + str(i)]  # 将字典中的值存储在对应的索引位置上
            
    return v  # 返回 One-Hot 编码后的数组

# 使用 apply() 方法将 udmap_onethot 函数应用于每个样本的 'udmap' 列
# np.vstack() 用于将结果堆叠成一个数组
train_udmap_df = pd.DataFrame(np.vstack(train_data['udmap'].apply(udmap_onethot)))

# 为新的特征 DataFrame 命名列名
train_udmap_df.columns = ['key' + str(i) for i in range(1, 10)]

# 将编码后的 udmap 特征与原始数据进行拼接,沿着列方向拼接
train_data = pd.concat([train_data, train_udmap_df], axis=1)

# 4. 编码 udmap 是否为空
# 使用比较运算符将每个样本的 'udmap' 列与字符串 'unknown' 进行比较,返回一个布尔值的 Series
# 使用 astype(int) 将布尔值转换为整数(0 或 1),以便进行后续的数值计算和分析
train_data['udmap_isunknown'] = (train_data['udmap'] == 'unknown').astype(int)
test_data['udmap_isunknown'] = (test_data['udmap'] == 'unknown').astype(int)
udmap_key = []
for i in range(1, 10):
    udmap_key.append(train_data[f'key{i}'].mean())
udmap_key
[64.21648698489254,
 260.31666333524623,
 29757.776829755818,
 1.4501092920839003,
 1.301670331229165,
 6.83070527245646,
 0.0005883718381058618,
 0.0001402420545622191,
 0.005659653489286797]
keylabel = ['key' + str(i) for  i in range(1, 10) ]
plt.bar(keylabel, udmap_key)
plt.title("udmap key mean")
plt.xlabel("udmap")
plt.ylabel("mean")
Text(0, 0.5, 'mean')
png
Author: Siyuan
URL: https://siyuan-zou.github.io/2023/08/24/%E7%94%A8%E6%88%B7%E6%96%B0%E5%A2%9E%E9%A2%84%E6%B5%8B%E6%8C%91%E6%88%98%E8%B5%9B-%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E4%B8%8E%E5%8F%AF%E8%A7%86%E5%8C%96/
This work is licensed under a CC BY-SA 4.0 .
- 2023/08/24/用户新增预测挑战赛/

1. 任务背景

为讯飞开放平台挑战赛的用户新增预测挑战赛提供的应用数据作为训练样本,基于提供的样本构建模型,预测用户的新增情况。

讯飞同时提供夏令营供学习,在此记录学习笔记。

1.1 数据说明

赛题数据由约62万条训练集、20万条测试集数据组成,共包含13个字段。其中uuid为样本唯一标识,eid为访问行为ID,udmap为行为属性,其中的key1到key9表示不同的行为属性,如项目名、项目id等相关字段,common_ts为应用访问记录发生时间(毫秒时间戳),其余字段x1至x8为用户相关的属性,为匿名处理字段。target字段为预测目标,即是否为新增用户。

1.2 评估指标

本次竞赛的评价标准采用f1_score,分数越高,效果越好。
f1_score 为二分类问题的一种常用评价标准,同时兼顾了分类模型的精确率(precision)和召回率(recall)。F1分数是模型精确率和召回率的一种调和平均,它的最大值是1,最小值是0。

  • 精确率(precision): 预测为True的样本中,多少个真的为True
  • 召回率(recall): 为True的样本中,多少个被预测为True

如下图:(待加入)

我们希望两个圆圈尽量重合,即精确率和召回率都尽量高,因此采用f1_score评价二分类问题。

2.1 任务一:一键跑通baseline

代码如下:

  1. 导入需要用到的相关库:
  • 导入 pandas 库,用于数据处理和分析
  • 导入 numpy 库,用于科学计算和多维数组操作
  • 从sklearn.tree模块中导入DecisionTreeClassifier,用于构建决策树分类模型
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
  1. 用pd读取训练集和测试集,转换成DataFrame格式
# 使用 read_csv() 函数从文件中读取训练集数据,文件名为 'train.csv'
train_data = pd.read_csv('用户新增预测挑战赛公开数据/train.csv')
# 使用 read_csv() 函数从文件中读取测试集数据,文件名为 'test.csv'
test_data = pd.read_csv('用户新增预测挑战赛公开数据/test.csv')
  1. 将 ‘udmap’ 列进行 One-Hot 编码

这种编码主要是对没有序关系的不同特征进行数学编码,由于不同特征之间不能比较、属于不同维度,因而有n个特征就采用n维向量编码,每个特征独享1个纬度。采用这样的编码的好处是避免机器按照数字大小比较不同特征:例如,将女性编译为1而男性编译为0,机器在处理时可能会默认女性特征比男性特征更重要。

# 数据样例:
#                    udmap  key1  key2  key3  key4  key5  key6  key7  key8  key9
# 0           {'key1': 2}     2     0     0     0     0     0     0     0     0
# 1           {'key2': 1}     0     1     0     0     0     0     0     0     0
# 2  {'key1': 3, 'key2': 2}   3     2     0     0     0     0     0     0     0

# 在 python 中, 形如 {'key1': 3, 'key2': 2} 格式的为字典类型对象, 通过key-value键值对的方式存储
# 而在本数据集中, udmap实际是以字符的形式存储, 所以处理时需要先用eval 函数将'udmap' 解析为字典

# 具体实现代码:
# 定义函数 udmap_onethot,用于将 'udmap' 列进行 One-Hot 编码
def udmap_onethot(d):
    v = np.zeros(9)  # 创建一个长度为 9 的零数组
    if d == 'unknown':  # 如果 'udmap' 的值是 'unknown'
        return v  # 返回零数组
    d = eval(d)  # 将 'udmap' 的值解析为一个字典
    for i in range(1, 10):  # 遍历 'key1' 到 'key9', 注意, 这里不包括10本身
        if 'key' + str(i) in d:  # 如果当前键存在于字典中
            v[i-1] = d['key' + str(i)]  # 将字典中的值存储在对应的索引位置上
            
    return v  # 返回 One-Hot 编码后的数组
# 使用 apply() 方法将 udmap_onethot 函数应用于每个样本的 'udmap' 列
# np.vstack() 用于将结果堆叠成一个数组
train_udmap_df = pd.DataFrame(np.vstack(train_data['udmap'].apply(udmap_onethot)))
test_udmap_df = pd.DataFrame(np.vstack(test_data['udmap'].apply(udmap_onethot)))
# 为新的特征 DataFrame 命名列名
train_udmap_df.columns = ['key' + str(i) for i in range(1, 10)]
test_udmap_df.columns = ['key' + str(i) for i in range(1, 10)]
# 将编码后的 udmap 特征与原始数据进行拼接,沿着列方向拼接
train_data = pd.concat([train_data, train_udmap_df], axis=1)
test_data = pd.concat([test_data, test_udmap_df], axis=1)
  1. 编码 udmap 是否为空

增加 ‘udmap_isunknown’ 特征列,取值0/1,以处理 ‘udmap’ 为 unknown 的情况。

# 使用比较运算符将每个样本的 'udmap' 列与字符串 'unknown' 进行比较,返回一个布尔值的 Series
# 使用 astype(int) 将布尔值转换为整数(0 或 1),以便进行后续的数值计算和分析
train_data['udmap_isunknown'] = (train_data['udmap'] == 'unknown').astype(int)
test_data['udmap_isunknown'] = (test_data['udmap'] == 'unknown').astype(int)
  1. 提取 eid 的频次特征
# 使用 map() 方法将每个样本的 eid 映射到训练数据中 eid 的频次计数
# train_data['eid'].value_counts() 返回每个 eid 出现的频次计数
train_data['eid_freq'] = train_data['eid'].map(train_data['eid'].value_counts())
test_data['eid_freq'] = test_data['eid'].map(train_data['eid'].value_counts())
  1. 提取 eid 的标签特征
# 使用 groupby() 方法按照 eid 进行分组,然后计算每个 eid 分组的目标值均值
# train_data.groupby('eid')['target'].mean() 返回每个 eid 分组的目标值均值
train_data['eid_mean'] = train_data['eid'].map(train_data.groupby('eid')['target'].mean())
test_data['eid_mean'] = test_data['eid'].map(train_data.groupby('eid')['target'].mean())

注意,在此我们使用了特征工程方法,即人为提取特征数据的频次、均值等特征,以改进模型拟合的效果。

  1. 提取时间戳

注意,需要注意时间戳的长度,如果是13位则 unit 为毫秒, 如果是10位则为秒。

# 使用 pd.to_datetime() 函数将时间戳列转换为 datetime 类型
# 样例:1678932546000->2023-03-15 15:14:16
train_data['common_ts'] = pd.to_datetime(train_data['common_ts'], unit='ms')
test_data['common_ts'] = pd.to_datetime(test_data['common_ts'], unit='ms')
# 使用 dt.hour 属性从 datetime 列中提取小时信息,并将提取的小时信息存储在新的列 'common_ts_hour'
train_data['common_ts_hour'] = train_data['common_ts'].dt.hour
test_data['common_ts_hour'] = test_data['common_ts'].dt.hour
  1. 加载决策树模型进行训练

直接使用sklearn中导入的包进行模型建立

clf = DecisionTreeClassifier()
# 使用 fit 方法训练模型
# train_data.drop(['udmap', 'common_ts', 'uuid', 'target'], axis=1) 从训练数据集中移除列 'udmap', 'common_ts', 'uuid', 'target'
# 这些列可能是特征或标签,取决于数据集的设置
# train_data['target'] 是训练数据集中的标签列,它包含了每个样本的目标值
clf.fit(
    train_data.drop(['udmap', 'common_ts', 'uuid', 'target'], axis=1),  # 特征数据:移除指定的列作为特征
    train_data['target']  # 目标数据:将 'target' 列作为模型的目标进行训练
)
  1. 对测试集进行预测,并保存结果到result_df中

    # 创建一个DataFrame来存储预测结果,其中包括两列:'uuid' 和 'target'
    # 'uuid' 列来自测试数据集中的 'uuid' 列,'target' 列将用来存储模型的预测结果
    result_df = pd.DataFrame({
        'uuid': test_data['uuid'],  # 使用测试数据集中的 'uuid' 列作为 'uuid' 列的值
        'target': clf.predict(test_data.drop(['udmap', 'common_ts', 'uuid'], axis=1))  # 使用模型 clf 对测试数据集进行预测,并将预测结果存储在 'target' 列中
    })
  2. 保存结果文件到本地

    # 将结果DataFrame保存为一个CSV文件,文件名为 'submit.csv'
    # 参数 index=None 表示不将DataFrame的索引写入文件中
    result_df.to_csv('submit.csv', index=None)
Author: Siyuan
URL: https://siyuan-zou.github.io/2023/08/24/%E7%94%A8%E6%88%B7%E6%96%B0%E5%A2%9E%E9%A2%84%E6%B5%8B%E6%8C%91%E6%88%98%E8%B5%9B/
This work is licensed under a CC BY-SA 4.0 .
keyboard_arrow_up