鱼C论坛

 找回密码
 立即注册
查看: 1225|回复: 3

[已解决]吴恩达机器学习

[复制链接]
发表于 2023-7-17 17:02:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 2830680393 于 2023-7-17 17:04 编辑

吴恩达机器学习中代码:
import numpy as np
%matplotlib widget
import matplotlib.pyplot as plt
from lab_utils_uni import plt_intuition, plt_stationary, plt_update_onclick, soup_bowl
plt.style.use('./deeplearning.mplstyle')

在运行代码时报错如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 4
      2 get_ipython().run_line_magic('matplotlib', 'widget')
      3 import matplotlib.pyplot as plt
----> 4 from lab_utils_uni import plt_intuition, plt_stationary, plt_update_onclick, soup_bowl
      5 plt.style.use('deeplearning.mplstyle')

File ~\Desktop\2022-Machine-Learning-Specialization-main\Supervised Machine Learning Regression and Classification\week1\4.Regression Model\lab_utils_uni.py:11
      9 from matplotlib.colors import LinearSegmentedColormap
     10 from ipywidgets import interact
---> 11 from lab_utils_common import compute_cost
     12 from lab_utils_common import dlblue, dlorange, dldarkred, dlmagenta, dlpurple, dlcolors
     14 plt.style.use('./deeplearning.mplstyle')

File ~\Desktop\2022-Machine-Learning-Specialization-main\Supervised Machine Learning Regression and Classification\week1\4.Regression Model\lab_utils_common.py:9
      6 import numpy as np
      7 import matplotlib.pyplot as plt
----> 9 plt.style.use('./deeplearning.mplstyle')
     10 dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0';
     11 dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple]

File D:\tool\anaconda\lib\site-packages\matplotlib\style\core.py:153, in use(style)
    151 pkg, _, name = style.rpartition(".")
    152 try:
--> 153     path = (importlib_resources.files(pkg)
    154             / f"{name}.{STYLE_EXTENSION}")
    155     style = _rc_params_in_file(path)
    156 except (ModuleNotFoundError, IOError) as exc:
    157     # There is an ambiguity whether a dotted name refers to a
    158     # package.style_name or to a dotted file path.  Currently,
   (...)
    161     # either use Path objects or be prepended with "./" and use
    162     # the slash as marker for file paths.

File D:\tool\anaconda\lib\importlib\_common.py:22, in files(package)
     17 def files(package):
     18     # type: (Package) -> Traversable
     19     """
     20     Get a Traversable resource from a package
     21     """
---> 22     return from_package(get_package(package))

File D:\tool\anaconda\lib\importlib\_common.py:66, in get_package(package)
     60 def get_package(package):
     61     # type: (Package) -> types.ModuleType
     62     """Take a package name or module object and return the module.
     63
     64     Raise an exception if the resolved module is not a package.
     65     """
---> 66     resolved = resolve(package)
     67     if wrap_spec(resolved).submodule_search_locations is None:
     68         raise TypeError(f'{package!r} is not a package')

File D:\tool\anaconda\lib\importlib\_common.py:57, in resolve(cand)
     55 def resolve(cand):
     56     # type: (Package) -> types.ModuleType
---> 57     return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)

File D:\tool\anaconda\lib\importlib\__init__.py:121, in import_module(name, package)
    118 if not package:
    119     msg = ("the 'package' argument is required to perform a relative "
    120            "import for {!r}")
--> 121     raise TypeError(msg.format(name))
    122 for character in name:
    123     if character != '.':

TypeError: the 'package' argument is required to perform a relative import for './deeplearning'


请问怎么解决啊?

最佳答案
2023-7-21 12:14:21


根据我从网上搜索到的信息,这个错误是由于您使用了相对导入的方式,但没有指定包名。相对导入需要使用`package`参数来指定相对于哪个包进行导入。

在您的代码中,您使用了`from .upload.urls import ...`这样的语句,但没有告诉Python`.upload.urls`是相对于哪个包的。这可能会导致Python无法找到正确的模块路径,从而抛出`TypeError`异常。

为了解决这个问题,您可以尝试以下几种方法:
- 使用绝对导入的方式,即不要在模块名前加点,而是直接写出完整的模块名,例如`from upload.urls import ...`。
- 在相对导入的语句中,添加`package`参数,指定当前模块所属的包名,例如`from .upload.urls import ..., package='courseworkupload'`。
- 确保您的项目目录中有一个`__init__.py`文件,以便Python将其识别为一个包。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-17 17:02:26 | 显示全部楼层
这个错误提示是因为相对导入时缺少了包名。在您的代码中,plt.style.use('./deeplearning.mplstyle')中的'./deeplearning.mplstyle'应该是一个相对路径,但是缺少了包名。

解决方法是将相对路径改为绝对路径,或者使用正确的包名进行相对导入。您可以尝试以下两种方法:

1. 将相对路径改为绝对路径:
   - 从错误提示中可以看到File ~\Desktop\2022-Machine-Learning-Specialization-main\Supervised Machine Learning Regression and Classification\week1\4.Regression Model\lab_utils_common.py,这是lab_utils_common.py文件的路径。
   - 您可以根据这个路径,找到deeplearning.mplstyle文件的绝对路径,并将相对路径修改为绝对路径。
   - 替换代码中的plt.style.use('./deeplearning.mplstyle')plt.style.use('/path/to/deeplearning.mplstyle'),其中/path/to/deeplearning.mplstyledeeplearning.mplstyle文件的实际路径。

2. 使用正确的包名进行相对导入:
   - 根据错误提示,可以看到from lab_utils_common import compute_costfrom lab_utils_common import dlblue, dlorange, dldarkred, dlmagenta, dlpurple, dlcolors这两行代码是在lab_utils_uni.py文件中使用的。
   - 您可以检查一下lab_utils_common.py文件所在的包名,并在lab_utils_uni.py文件中使用正确的包名进行相对导入。
   - 替换代码中的from lab_utils_common import compute_costfrom lab_utils_common import dlblue, dlorange, dldarkred, dlmagenta, dlpurple, dlcolors为正确的包名形式,例如from package_name.lab_utils_common import compute_costfrom package_name.lab_utils_common import dlblue, dlorange, dldarkred, dlmagenta, dlpurple, dlcolors,其中package_namelab_utils_common.py文件所在的包名。

请根据您的实际情况选择适合的解决方法。如果仍然有问题,请提供更多相关代码和详细的错误信息,以便我能够更好地帮助您解决问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-17 17:02:38 | 显示全部楼层
错误信息显示,当你试图使用自定义的 matplotlib 样式时,系统无法找到你所引用的 'deeplearning.mplstyle' 文件。以下是几种可能的解决方案:

1. 确保 'deeplearning.mplstyle' 文件的路径正确。你可能需要提供一个相对路径或者绝对路径。根据你给出的错误信息,你可能是将样式文件放在了当前工作目录,那么应该这样引用它:

   plt.style.use('./deeplearning.mplstyle')   
   如果样式文件不在当前工作目录,你需要提供相对路径或绝对路径,例如:

   plt.style.use('/absolute/path/to/deeplearning.mplstyle')   # or   plt.style.use('relative/path/to/deeplearning.mplstyle')   
2. 你也可以检查是否有权限读取样式文件。如果你没有权限,你可能需要更改文件权限。

3. 如果以上建议都不能解决问题,你可以尝试升级你的 matplotlib 到最新版本,可能是你的版本与这个特性不兼容。

希望以上的建议对你有所帮助。

以上是我对你问题的理解,如果有任何不对或者你有其他问题,请随时告诉我。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-21 12:14:21 | 显示全部楼层    本楼为最佳答案   


根据我从网上搜索到的信息,这个错误是由于您使用了相对导入的方式,但没有指定包名。相对导入需要使用`package`参数来指定相对于哪个包进行导入。

在您的代码中,您使用了`from .upload.urls import ...`这样的语句,但没有告诉Python`.upload.urls`是相对于哪个包的。这可能会导致Python无法找到正确的模块路径,从而抛出`TypeError`异常。

为了解决这个问题,您可以尝试以下几种方法:
- 使用绝对导入的方式,即不要在模块名前加点,而是直接写出完整的模块名,例如`from upload.urls import ...`。
- 在相对导入的语句中,添加`package`参数,指定当前模块所属的包名,例如`from .upload.urls import ..., package='courseworkupload'`。
- 确保您的项目目录中有一个`__init__.py`文件,以便Python将其识别为一个包。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 09:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表