Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法

本文介绍了在使用Python的sklearn库时遇到的cross_validation模块被弃用的问题及解决方案。作者首先尝试了直接调用该模块,遇到了AttributeError错误;接着通过from...import的方式调用,虽然解决了错误但出现了DeprecationWarning警告;最终按照警告提示改用model_selection模块,成功解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法

在使用Python的sklearn库时,发现sklearn的cross_validation不能使用,在pycharm上直接显示为被横线划掉。
运行程序:

import sklearn
...
X, Xt, y, yt = sklearn.cross_validation.train_test_split(X, y)

报错:

AttributeError: 'module' object has no attribute 'cross_validation'

这里尝试修改为:

from sklearn.cross_validation import train_test_split
X, Xt, y, yt = train_test_split(X, y)

发现不再报错,而是提示警告信息:

DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)

程序能够正常执行。

但是注意看提示信息,实际上是说cross_validation模块被弃用了,改为支持model_selection这个模块,因此,将程序改为:

from sklearn.model_selection import train_test_split
X, Xt, y, yt = train_test_split(X, y)

不再出现警告信息,一切正常。

转载于:https://www.cnblogs.com/alanma/p/6877354.html

(01) wangtianze24@b0215dbf77d3:~/project_all_wang/qqAttnSleep-main/AttnSleep-main/prepare_datasets$ python prepare_physionet.py --data_dir /home/wangtianze24/sleep_edf20/SLEEP_EDF_20 --output_dir data_edf_20_npz/fpzcz --select_ch "EEG Fpz-Cz" Extracting EDF parameters from /home/wangtianze24/sleep_edf20/SLEEP_EDF_20/SC4001E0-PSG.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 7949999 = 0.000 ... 79499.990 secs... /home/wangtianze24/project_all_wang/qqAttnSleep-main/AttnSleep-main/prepare_datasets/prepare_physionet.py:95: DeprecationWarning: scaling_time is deprecated in version 0.20 and will be removed in version 0.21. To replicate old behavior, use time_format="ms" to get time in milliseconds, or use time_format=None and scale the time column (in seconds) after DataFrame creation. raw_ch_df = raw.to_data_frame(scaling_time=100.0)[select_ch] Traceback (most recent call last): File "/home/wangtianze24/project_all_wang/qqAttnSleep-main/AttnSleep-main/prepare_datasets/prepare_physionet.py", line 218, in <module> main() File "/home/wangtianze24/project_all_wang/qqAttnSleep-main/AttnSleep-main/prepare_datasets/prepare_physionet.py", line 131, in main label_epoch = np.ones(duration_epoch, dtype=np.int) * label File "/home/wangtianze24/anaconda3/envs/01/lib/python3.9/site-packages/numpy/__init__.py", line 305, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'int'. `np.int` was a deprecated alias for the builtin `int`. To avoid this error in existing code, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: 什么意思
03-22
<think>我们正在讨论Python中的DeprecationWarning,特别是关于setDaemon()方法被弃用的问题,以及如何正确使用daemon属性替代。 根据用户的问题,我们需要解释: 1. DeprecationWarning的含义 2. 为什么setDaemon()被弃用 3. 如何使用daemon属性 同时,我们需要提供代码示例来说明如何正确使用。 注意:在Python中,线程模块(threading)的Thread对象有一个setDaemon()方法,它用于设置线程是否为守护线程(daemon thread)。在Python 3.10中,setDaemon()方法被标记为弃用(deprecated),并建议使用daemon属性直接赋值。 步骤: 1. 解释DeprecationWarning:这意味着该特性已被弃用,虽然目前还能使用,但在未来的版本中可能会被移除,建议使用新的替代方法。 2. 说明弃用原因:通常是为了让API更一致、更清晰。使用属性(property)来设置和获取比使用方法更符合Python风格。 3. 展示如何修改:将原来的t.setDaemon(True)改为t.daemon = True。 代码示例: 旧代码(会产生警告): t = Thread(target=foo) t.setDaemon(True) 新代码(推荐): t = Thread(target=foo) t.daemon = True 注意:daemon属性必须在启动线程(t.start())之前设置。 另外,我们也可以在线程创建时直接设置daemon属性: t = Thread(target=foo, daemon=True) 我们还需要说明守护线程的作用:守护线程会在主线程结束时自动退出,不管它是否执行完成。而非守护线程会阻止主线程退出,直到所有非守护线程完成。 最后,生成相关问题。 引用说明:由于问题中没有提供具体的引用来源,我们不会添加引用标识,除非有明确的引用内容。但根据示例,如果有引用可以添加。这里我们可能没有引用,所以不添加。 现在,我们按照要求用中文回答。</think>### Python DeprecationWarning: setDaemon() 已弃用的解决方案 在 Python 中,`setDaemon()` 方法已被标记为弃用(Deprecated),这意味着虽然它目前仍可使用,但将在未来版本中被移除。Python 官方推荐使用 `daemon` 属性替代该方法。以下是详细解释和解决方案: #### 1. **DeprecationWarning 的含义** 当你在代码中使用 `setDaemon()` 时,Python 会抛出类似警告: ``` DeprecationWarning: setDaemon() is deprecated, set the daemon attribute instead ``` 这表示: - `setDaemon()` 是过时的 API - 未来 Python 版本将彻底移除该方法 - 需改用更简洁的 `daemon` 属性[^1] #### 2. **弃用原因** `setDaemon()` 被弃用主要是因为: - Python 趋向使用属性(property)替代 getter/setter 方法 - `daemon` 属性语法更符合 Python 的简洁风格 - 减少冗余代码量(直接赋值 vs 调用方法) #### 3. **正确替代方案** ##### ✅ 旧方法(已弃用): ```python from threading import Thread def task(): print("守护线程运行中") t = Thread(target=task) t.setDaemon(True) # ⚠️ 已弃用 t.start() ``` ##### ✅ 新方法(推荐): ```python # 方式1:创建线程后直接赋值 t = Thread(target=task) t.daemon = True # ✅ 使用属性替代 t.start() # 方式2:构造函数内初始化 t = Thread(target=task, daemon=True) # ✅ 更简洁 t.start() ``` #### 4. **关键注意事项** 1. **设置时机** `daemon` 属性必须在调用 `start()` 前设置: ```python t = Thread(target=task) t.daemon = True # 正确 t.start() # t.start() # t.daemon = True # ❌ 错误!线程启动后修改无效 ``` 2. **守护线程特性** - 守护线程会随主线程退出而强制终止 - 非守护线程(`daemon=False`)会阻止主线程退出 - 典型应用:后台日志、心跳检测等无需阻塞主进程的任务 3. **兼容性建议** 若需兼容旧版 Python,可使用条件判断: ```python t = Thread(target=task) if hasattr(t, 'daemon'): t.daemon = True # Python 3.3+ else: t.setDaemon(True) # 旧版本回退 t.start() ``` #### 5. **完整示例** ```python import threading import time def background_task(): while True: print("守护线程运行...") time.sleep(1) # 创建并启动守护线程 t = threading.Thread(target=background_task, daemon=True) t.start() # 主线程工作 print("主线程开始工作") time.sleep(3) print("主线程结束,守护线程自动终止") ``` 输出: ``` 主线程开始工作 守护线程运行... 守护线程运行... 守护线程运行... 主线程结束,守护线程自动终止 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值