今天因为需要重新定制Kmeans聚类算法,碰到一个比较严重的问题,因为采用肘部法计算最佳K值,重写后的Kmeans算法需要调用Kneed模块计算拐点,程序报错:ModuleNotFoundError: No module named 'numpy.testing.decorators',仔细查看发现时因为numpy版本太高,Kneed需要调用的一个模块中找不到numpy.testing.decorators。网上查看的解决方案,很多让给numpy降级,我很多项目使用现在的版本的numpy制作的,就怕降级之后引发其他项目的问题,这个方法显然不可取。站内一个大佬提了另一个不降级方案:【不降级的解决方案】ModuleNotFoundError: No module named ‘numpy.testing.decorators‘_link_in_csdn的博客-CSDN博客,虽然最终使用发现并没有什么用,但启发我想了另一个办法。
根据报错的代码:
File "D:\anaconda3\lib\site-packages\scipy\stats\morestats.py", line 12, in <module>
from numpy.testing.decorators import setastest
ModuleNotFoundError: No module named 'numpy.testing.decorators'
问题出在morestats文件中,定位到文件,发现源码from numpy.testing.decorators import setastest呈现灰色,找不到引用,如果最终需要使用setastest函数的话,我只需要找到现在setastes函数位置就行了(本质上还是在numpy中decorators的新位置),在网上查找之后,发现新位置在numpy.testing._private.decorators,那替换掉这段引用为from numpy.testing._private.decorators import setastest就很好地解决了这个问题,一切恢复正常,这里记录一下这个方案,算是对大佬给出方案的一个补充。