test_df = data[data["label"].isnull() == True].copy().reset_index(drop=True) train_df = data[~data["label"].isnull() == True].copy().reset_index(drop=True) feature_name = [f for f in data.columns if f not in drop_cols] X_train = train_df[feature_name].reset_index(drop=True) X_test = test_df[feature_name].reset_index(drop=True) y = train_df['label'].reset_index(drop=True)
时间: 2023-06-26 20:09:44 浏览: 81
这段代码的作用是将原始数据集分为训练集和测试集,并获取特征名称和标签。
首先,`data` 是原始数据集。`data["label"].isnull() == True` 会返回一个布尔数组,表示每个数据点的 "label" 是否缺失。`data[data["label"].isnull() == True]` 会根据这个布尔数组筛选出缺失 "label" 的数据点,也就是测试集。`data[~data["label"].isnull() == True]` 则是选择不缺失 "label" 的数据点,也就是训练集。
`reset_index(drop=True)` 用来重新设置索引,把新的数据集索引从 0 开始排列。
`feature_name` 是特征名称列表,其中 `drop_cols` 是需要删除的特征列名列表。`X_train` 和 `X_test` 分别是训练集和测试集的特征数据。`y` 是训练集的标签数据。
阅读全文
相关推荐















