scipy 当使用sklearn时得到警告.neighbors about keepdims

xuo3flqw  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(403)

我在不同的分类器上训练一些数据。直到几天前我更新了我所有的包和python本身,我才遇到问题。警告只在Knighbor分类器上显示,因为我使用了一个巨大的循环和Jupyter,我看不到结果,因为每个循环都有这样的警告:...

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)

这是我的代码:

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))

我也在使用Ada分类器,但没有收到同样的警告。代码如下:

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))
6ss1mwsb

6ss1mwsb1#

得到了同样的问题,对我来说,摆脱这些信息最简单的方法是这样的:

from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)

相关问题