You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用sklearn.neighbors时出现keepdims相关FutureWarning问题求助

解决KNeighborsClassifier的FutureWarning问题

问题背景

更新Python及相关依赖包后,使用KNeighborsClassifier训练数据时出现重复的FutureWarning,由于训练过程包含大量循环,每次循环都会输出警告,严重干扰运行结果的查看;而使用AdaBoostClassifier时未出现该警告。

警告内容

sklearn/neighbors/_classification.py:237: FutureWarning: Unlike other reduction functions (e.g. skew, kurtosis), the default behavior of mode typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of keepdims will become False, the axis over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set keepdims to True or False to avoid this warning.
mode, _ = stats.mode(_y[neigh_ind, k], axis=1)

相关代码

KNeighborsClassifier训练代码

n_fold = 200
k_range = range(1,100)
misclassification = np.zeros((n_fold,len(k_range)))

for i in range(n_fold):
    x = gender.drop(["Lead"], axis=1).values
    y = gender["Lead"].values
    x_train, x_test, y_train, y_test = skl_ms.train_test_split(x, y , test_size= 0.2)
    
    for j ,k in enumerate(k_range):
        model = skl_nb.KNeighborsClassifier(n_neighbors=k, )
        model.fit(x_train, y_train)
        prdct = model.predict(x_test)
        misclassification[i,j] = np.mean(prdct!= y_test)

plts = np.linspace(1, 200, 200)
plt.plot(plts, misclassification, '.')
plt.title("K Fold Classification")
plt.ylabel('Misclassification')
plt.xlabel('number of neighbors')
plt.show()

mean_misclas= np.mean(misclassification, axis = 0)
min_prdct = min(mean_misclas)
for m in range(len(mean_misclas)):
    if mean_misclas[m] == min_prdct:
        ind = m 
        break
min_k = ind + 1

model = skl_nb.KNeighborsClassifier(n_neighbors=min_k)
model.fit(x_train, y_train)
prdct = model.predict(x_test)
result = np.mean(prdct!= y_test)
print('misclassification is: %.3f' %result)  
print('accuracy is: %.3f' %np.mean(prdct == y_test))

AdaBoostClassifier训练代码

x = gender.drop(["Lead"], axis=1).values 
y = gender["Lead"].values 
x_train, x_test, y_train, y_test = skl_ms.train_test_split(x, y , test_size= 0.2)

modelAda = AdaBoostClassifier() 
modelAda.fit(x_train, y_train) 
predict = modelAda.predict(x_test)

print('misclassification: %.3f' % np.mean(predict != y_test))

print('accuracy is: %.3f' %np.mean(predict == y_test)) 

解决方案

方式一:升级scikit-learn到最新版本

scikit-learn后续版本已修复该stats.mode调用的参数问题,直接升级到最新稳定版即可彻底消除警告:

pip install --upgrade scikit-learn

方式二:临时屏蔽特定警告

如果暂时不想升级依赖,可在代码开头添加警告过滤逻辑,仅屏蔽该FutureWarning:

import warnings
# 精准匹配警告来源模块
warnings.filterwarnings("ignore", category=FutureWarning, module="sklearn.neighbors._classification")

或者更精准匹配警告内容:

import warnings
warnings.filterwarnings("ignore", message="Unlike other reduction functions.*Set `keepdims` to True or False to avoid this warning.")

方式三:自定义投票逻辑(进阶)

若需手动修正底层逻辑,可继承KNeighborsClassifier并重写_predict_proba方法,显式为stats.mode设置keepdims参数,但该方式较为繁琐,推荐优先使用前两种方案。


内容的提问来源于stack exchange,提问作者MinaB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 22:25:23