movielens数据集下载
时间: 2025-06-22 12:45:59 AIGC 浏览: 23
### 下载MovieLens数据集的方法
为了下载MovieLens数据集并展示下载进度,可以使用Python中的`tqdm`库来配合标准库函数`urlretrieve()`完成这一操作。下面是一个具体的实现方法[^2]。
#### 使用 `tqdm` 和 `urllib.request.urlretrieve`
通过定义一个自定义回调函数,在每次接收到一部分文件时更新进度条状态:
```python
from urllib.request import urlretrieve
from tqdm import tqdm
import os
class TqdmUpTo(tqdm):
"""Provides `update_to(n)` which uses `tqdm.update(delta_n)`."""
def update_to(self, b=1, bsize=1, tsize=None):
"""
Parameters
----------
b : int, optional
Number of blocks transferred so far [default: 1].
bsize : int, optional
Size of each block (in tqdm units) [default: 1].
tsize : int, optional
Total size (in tqdm units). If [default: None] remains unchanged.
"""
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n)
def download_movielens(url='https://files.grouplens.org/datasets/movielens/ml-1m.zip', output_path='./ml-1m.zip'):
with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t:
urlretrieve(url, filename=output_path, reporthook=t.update_to, data=None)
t.close()
if __name__ == '__main__':
# 创建保存目录如果不存在的话
save_dir = './datasets'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
file_path = os.path.join(save_dir, 'ml-1m.zip')
download_movielens(output_path=file_path)
```
上述脚本会自动创建名为`./datasets`的文件夹用于存储下载的数据集压缩包,并且会在终端中实时显示当前的下载进度。
阅读全文
相关推荐













