【Python问题笔记1】joblib包memory模块参数变更导致mglearn使用时报错

前言

在学习机器学习算法的过程中,我使用到了名为mglearn的包,它可以用于美化绘图

问题

在运行程序时,程序发生了崩溃,下面是Traceback信息

Traceback (most recent call last):
  File "example1_flower.py", line 6, in <module>
    import mglearn
  File "D:\软件\python\lib\site-packages\mglearn\__init__.py", line 1, in <module>
    from . import plots
  File "D:\软件\python\lib\site-packages\mglearn\plots.py", line 14, in <module>
    from .plot_pca import plot_pca_illustration, plot_pca_whitening, plot_pca_faces
  File "D:\软件\python\lib\site-packages\mglearn\plot_pca.py", line 7, in <module>
    memory = Memory(cachedir="cache")
TypeError: __init__() got an unexpected keyword argument 'cachedir'

定位问题

在plot_pca.py文件中,调用了joblib包中的memory类,并对其进行实例化,我找到了该类的源码,发现了问题所在。(为了节约篇幅删去了部分内容)

class Memory(Logger):
    """ 
        Parameters
        ----------
        location: str, pathlib.Path or None
            The path of the base directory to use as a data store
            or None. If None is given, no caching is done and
            the Memory object is completely transparent. This option
            replaces cachedir since version 0.12.
    """
    # ------------------------------------------------------------------------
    # Public interface
    # ------------------------------------------------------------------------

    def __init__(self, location=None, backend='local',
                 mmap_mode=None, compress=False, verbose=1, bytes_limit=None,
                 backend_options=None):

可以发现类中确实没有出现参数cachedir,再仔细查看注释:

This option replaces cachedir since version 0.12.

原来该参数已被弃置,现在应使用参数location。

解决方案

将文件plot_pca.py、plot_nmf.py中的memory = Memory(cachedir="cache")改成memory = Memory(location="cache")

小结

养成读源码的习惯是非常重要的!

你可能感兴趣的:(排错笔记)