[Machine Learning] The solution to the failure of Sklearn to import the handwritten digit data set Mnist

The solution to the failure of Sklearn to import the handwritten digit dataset Mnist



1. Problem description

As a newcomer who has just stepped into the direction of "machine learning", I often learn its theoretical knowledge and experiments on major websites. Since this direction has been developed for a certain period of time, most of these tutorials have existed for a certain period of time. As the foundation of a subject, theoretical knowledge often has relatively complete mathematical proofs, so most of the content on these online courses (websites) is accurate; however, some related experiments are inconsistent because they rely on certain frameworks. There may be some discrepancies with the version upgrade, which will lead to the failure of the experiment. So, I ran into today's problem:

insert image description here

The specific code and error message are as follows:

# 引用代码
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')
mnist

# 错误提示
ImportError: cannot import name 'fetch_mldata' from 'sklearn.datasets' (D:\Python\Python3.9\lib\site-packages\sklearn\datasets\__init__.py)


2. Online solutions (failed)

When encountering a problem, Baidu will always be the first time!
Someone said: "fetch_mldata() can no longer be used because the resources it depends on are no longer applicable, and it can be replaced by fetch_openml()", so there are:

# 重写代码
from sklearn.datasets import fetch_openml
mnist = fetch_openml("mnist_784")
mnist

I tried it, but still get an error:

insert image description here
Maybe something is wrong with me. Anyway, I didn’t succeed (given the reference blog I consulted ), if someone knows the reason, welcome to give a positive answer in the comment area.



3. My solution (success)

After checking a lot of methods, it seems that this data set can be downloaded locally and then loaded locally (the advantage of this is that no matter what framework you use in the future, how it is upgraded, the method and code you use are still the ones you wrote originally) .
Here is a direct link to the download . rest assured! I know brother Meng! free! Extraction code: qqhc.
After downloading and decompressing, the content you see is:
Alt
"mnist-original.mat" file is stored in the "All Data Summary" folder, which integrates all the data into one file. When writing code, we usually use this. The method of use is very simple, copy this file to the project folder you wrote, and then use the relative path to access it in the code. The code for local loading and use is given below:

import scipy.io
mnist = scipy.io.loadmat('mnist-original.mat')

X = mnist['data']
y = mnist['label']

print("X的内容:\n",X)
print("X的规格:",X.shape)
print()
print("y的内容:\n",y)
print("y的规格:",y.shape)

The effect is as follows:
Alt
hereby record!


END


Guess you like

Origin blog.csdn.net/the_ZED/article/details/127672699