怎么解决无法从sklearn.datasets中导入fetch_mldata。
时间: 2024-02-03 22:15:54 浏览: 165
`fetch_mldata`已经被弃用,所以无法从`sklearn.datasets`中导入。您可以尝试以下替代方案:
1. 使用`fetch_openml`代替`fetch_mldata`,例如:
```
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
```
2. 从外部下载数据集并手动加载数据集,例如:
```
import numpy as np
import gzip
def load_mnist(path, kind='train'):
"""Load MNIST data from `path`"""
labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind)
images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind)
with gzip.open(labels_path, 'rb') as lbpath:
labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8)
with gzip.open(images_path, 'rb') as imgpath:
images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784)
return images, labels
X_train, y_train = load_mnist('path/to/mnist', kind='train')
X_test, y_test = load_mnist('path/to/mnist', kind='t10k')
```
阅读全文