gpt4 book ai didi

pandas - Knn 对距离上的特定特征赋予更多权重

转载 作者:行者123 更新时间:2023-11-30 09:41:54 27 4
gpt4 key购买 nike

我正在使用Kobe Bryant Dataset 。我希望用 KnnRegressor 预测 shot_made_flag。

我使用game_date来提取yearmonth特征:

# covert season to years
kobe_data_encoded['season'] = kobe_data_encoded['season'].apply(lambda x: int(re.compile('(\d+)-').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'])

并且我希望使用seasonyearmonth功能来赋予它们在距离函数中更大的权重,以便日期更接近的事件当前事件将是更近的邻居,但仍然与潜在的其他数据点保持合理的距离,因此,例如,我不希望同一天的事件仅仅因为日期特征而成为最近的邻居,但它会考虑到其他功能,例如 shot_range 等。
为了赋予它更多的权重,我尝试将 metric 参数与自定义距离函数一起使用,但函数的参数只是 numpy 数组,没有 pandas 的列信息,所以我不确定我能做什么以及如何实现我想要做的事情。

编辑:

对日期特征使用较大的权重,以在 [1, 100]k 上运行 cv 为 10 时找到最佳 k:

from IPython.display import display
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score

# 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

not_classified_df = scaled_features_df[scaled_features_df['shot_made_flag'].isnull()]
classified_df = scaled_features_df[scaled_features_df['shot_made_flag'].notnull()]
X = classified_df.drop(columns=['shot_made_flag'])
y = classified_df['shot_made_flag']
cv = StratifiedKFold(n_splits=10, shuffle=True)

neighbors = [x for x in range(1, 100)]
cv_scores = []

weight = np.ones((X.shape[1],))
weight[[X.columns.get_loc("season"),
X.columns.get_loc("year"),
X.columns.get_loc("month")
]] = 5
weight = weight/weight.sum() #Normalize weights

def my_distance(x, y):
dist = ((x-y)**2)
return np.dot(dist, weight)

for k in neighbors:
print('k: ', k)
knn = KNeighborsClassifier(n_neighbors=k, metric=my_distance)
cv_scores.append(np.mean(cross_val_score(knn, X, y, cv=cv, scoring='roc_auc')))

#optimal K
optimal_k_index = cv_scores.index(min(cv_scores))
optimal_k = neighbors[optimal_k_index]
print('best k: ', optimal_k)
plt.plot(neighbors, cv_scores)
plt.xlabel('Number of Neighbors K')
plt.ylabel('ROC AUC')
plt.show()

运行速度非常慢,有什么办法让它更快吗?加权特征的想法是找到更接近数据点日期的邻居以避免数据泄漏和cv来找到最佳k。

最佳答案

首先,您必须准备一个 numpy 1D weight 数组,指定每个特征的权重。你可以这样做:

weight = np.ones((M,))  # M is no of features
weight[[1,7,10]] = 2 # Increase weight of 1st,7th and 10th features
weight = weight/weight.sum() #Normalize weights

您可以使用kobe_data_encoded.columns在数据框中查找seasonyearmonth特征的索引替换上面的第二行。

现在定义一个距离函数,根据准则,该函数必须采用两个一维 numpy 数组。

def my_dist(x,y):
global weight #1D array, same shape as x or y
dist = ((x-y)**2) #1D array, same shape as x or y
return np.dot(dist,weight) # a scalar float

并将KNeighborsRegressor初始化为:

knn = KNeighborsRegressor(metric=my_dist)

编辑:为了提高效率,您可以预先计算距离矩阵,并在 KNN 中重用它。这应该通过减少对 my_dist 的调用来显着加速,因为这个非向量化的自定义 python 距离函数非常慢。所以现在 -

dist = np.zeros((len(X),len(X)))  #Computing NXN distance matrix
for i in range(len(X)): # You can halve this by using the fact that dist[i,j] = dist[j,i]
for j in range(len(X)):
dist[i,j] = my_dist(X[i],X[j])

for k in neighbors:
print('k: ', k)
knn = KNeighborsClassifier(n_neighbors=k, metric='precomputed') #Note: metric='precomputed'
cv_scores.append(np.mean(cross_val_score(knn, dist, y, cv=cv, scoring='roc_auc'))) #Note: passing dist instead of X

我无法测试它,所以如果有问题请告诉我。

关于pandas - Knn 对距离上的特定特征赋予更多权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57521656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com