1


我正在使用Kobe Bryant Dataset
我希望预测shot_made_flagwith KnnRegressor
我试图通过按 、 和 对数据进行分组来避免season数据year泄漏month
season是预先存在的列,year并且month是我添加的列,如下所示:

kobe_data_encoded['year'] = kobe_data_encoded['game_date'].apply(lambda x: int(re.compile('(\d{4})').findall(x)[0]))
kobe_data_encoded['month'] = kobe_data_encoded['game_date'].apply(lambda x: int(re.compile('-(\d+)-').findall(x)[0]))

这是我的功能预处理代码的完整代码:

import re
# drop unnecesarry columns
kobe_data_encoded = kobe_data.drop(columns=['game_event_id', 'game_id', 'lat', 'lon', 'team_id', 'team_name', 'matchup', 'shot_id'])

# use HotEncoding for action_type, combined_shot_type, shot_zone_area, shot_zone_basic, opponent
kobe_data_encoded = pd.get_dummies(kobe_data_encoded, prefix_sep="_", columns=['action_type'])
kobe_data_encoded = pd.get_dummies(kobe_data_encoded, prefix_sep="_", columns=['combined_shot_type'])
kobe_data_encoded = pd.get_dummies(kobe_data_encoded, prefix_sep="_", columns=['shot_zone_area'])
kobe_data_encoded = pd.get_dummies(kobe_data_encoded, prefix_sep="_", columns=['shot_zone_basic'])
kobe_data_encoded = pd.get_dummies(kobe_data_encoded, prefix_sep="_", columns=['opponent'])

# covert season to years
kobe_data_encoded['season'] = kobe_data_encoded['season'].apply(lambda x: int(re.compile('(\d+)-').findall(x)[0]))

# covert shot_type to numeric representation
kobe_data_encoded['shot_type'] = kobe_data_encoded['shot_type'].apply(lambda x: int(re.compile('(\d)PT').findall(x)[0]))

# add year and month using game_date
kobe_data_encoded['year'] = kobe_data_encoded['game_date'].apply(lambda x: int(re.compile('(\d{4})').findall(x)[0]))
kobe_data_encoded['month'] = kobe_data_encoded['game_date'].apply(lambda x: int(re.compile('-(\d+)-').findall(x)[0]))
kobe_data_encoded = kobe_data_encoded.drop(columns=['game_date'])

# covert shot_type to numeric representation
kobe_data_encoded.loc[kobe_data_encoded['shot_zone_range'] == 'Back Court Shot', 'shot_zone_range'] = 4
kobe_data_encoded.loc[kobe_data_encoded['shot_zone_range'] == '24+ ft.', 'shot_zone_range'] = 3
kobe_data_encoded.loc[kobe_data_encoded['shot_zone_range'] == '16-24 ft.', 'shot_zone_range'] = 2
kobe_data_encoded.loc[kobe_data_encoded['shot_zone_range'] == '8-16 ft.', 'shot_zone_range'] = 1
kobe_data_encoded.loc[kobe_data_encoded['shot_zone_range'] == 'Less Than 8 ft.', 'shot_zone_range'] = 0

# transform game_date to date time object
# kobe_data_encoded['game_date'] = pd.to_numeric(kobe_data_encoded['game_date'].str.replace('-',''))

kobe_data_encoded.head()

然后我使用以下方法缩放了数据MinMaxScaler

# scaling
min_max_scaler = preprocessing.MinMaxScaler()
scaled_features_df = kobe_data_encoded.copy()
column_names = ['loc_x', 'loc_y', 'minutes_remaining', 'period',
                'seconds_remaining', 'shot_distance', 'shot_type', 'shot_zone_range']
scaled_features = min_max_scaler.fit_transform(scaled_features_df[column_names])
scaled_features_df[column_names] = scaled_features

season并按上述,year等分组month

seasons_date = scaled_features_df.groupby(['season', 'year', 'month'])

我的任务是使用分数 KFold找到最好的 K。 这是我的实现:roc_auc

neighbors = [x for x in range(1,50) if x % 2 != 0]
cv_scores = []
for k in neighbors:
    print('k: ', k)
    knn = KNeighborsClassifier(n_neighbors=k, n_jobs=-1)
    scores = []
    accumelated_X = pd.DataFrame()
    accumelated_y = pd.Series()
    for group_name, group in seasons_date:
        print(group_name)
        group = group.drop(columns=['season', 'year', 'month'])
        not_classified_df = group[group['shot_made_flag'].isnull()]
        classified_df = group[group['shot_made_flag'].notnull()]

        X = classified_df.drop(columns=['shot_made_flag'])
        y = classified_df['shot_made_flag']
        accumelated_X = pd.concat([accumelated_X, X])
        accumelated_y = pd.concat([accumelated_y, y])
        cv = StratifiedKFold(n_splits=10, shuffle=True)
        scores.append(cross_val_score(knn, accumelated_X, accumelated_y, cv=cv, scoring='roc_auc'))
    cv_scores.append(scores.mean())

#graphical view
#misclassification error
MSE = [1-x for x in cv_scores]
#optimal K
optimal_k_index = MSE.index(min(MSE))
optimal_k = neighbors[optimal_k_index]
print(optimal_k)
# plot misclassification error vs k
plt.plot(neighbors, MSE)
plt.xlabel('Number of Neighbors K')
plt.ylabel('Misclassification Error')
plt.show()

我不确定在这种情况下我是否正确处理了数据泄漏因为如果我积累上一季的数据然后将其传递给cross_val_score我可能会因为 cv 可以拆分而导致数据泄漏以某种方式测试新赛季数据和上赛季数据的数据我就在这里吗?如果是这样,我想知道如何处理这种情况,在这种情况下,我想用这个定时数据K-Fold找到最好的,而不会泄露数据。使用拆分数据而不是按游戏日期拆分以避免数据泄漏k是否明智?K-Fold

4

1 回答 1

1

简而言之,当你想做一些听起来像时间序列的事情时,你不能使用标准的 k-fold 交叉验证。

您将使用来自未来的一些数据来预测过去,这是被禁止的。

您可以在这里找到一个好方法:https ://stats.stackexchange.com/questions/14099/using-k-fold-cross-validation-for-time-series-model-selection

fold 1 : training [1], test [2]
fold 2 : training [1 2], test [3]
fold 3 : training [1 2 3], test [4]
fold 4 : training [1 2 3 4], test [5]
fold 5 : training [1 2 3 4 5], test [6]

其中数字按数据时间的时间顺序排列

于 2019-08-19T07:59:54.367 回答