from sklearn.datasets import get_data_home
print(get_data_home())
C:\Users\HuaWei\scikit_learn_data
from sklearn.datasets import fetch_mldata
mnist=fetch_mldata('MNIST original')
mnist
c:\users\huawei\appdata\local\programs\python\python36\lib\site-packages\sklearn\utils\deprecation.py:85: DeprecationWarning: Function fetch_mldata is deprecated; fetch_mldata was deprecated in version 0.20 and will be removed in version 0.22. Please use fetch_openml.
warnings.warn(msg, category=DeprecationWarning)
c:\users\huawei\appdata\local\programs\python\python36\lib\site-packages\sklearn\utils\deprecation.py:85: DeprecationWarning: Function mldata_filename is deprecated; mldata_filename was deprecated in version 0.20 and will be removed in version 0.22. Please use fetch_openml.
warnings.warn(msg, category=DeprecationWarning)
{'DESCR': 'mldata.org dataset: mnist-original',
'COL_NAMES': ['label', 'data'],
'target': array([0., 0., 0., ..., 9., 9., 9.]),
'data': array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)}
import numpy as np
import matplotlib.pyplot as plt
print('type(mnist):',type(mnist),'\n')
print('样本数量:{}\n样本特征数:{}'.format(mnist.data.shape[0],mnist.data.shape[1]),'\n')
print('mnist.data[0].shape:',mnist.data[0].shape)
type(mnist):
样本数量:70000
样本特征数:784
mnist.data[0].shape: (784,)
x=np.arange(-14,14,1)
xx,yy=np.meshgrid(x,x)
z=mnist.data[62957].reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx,yy,np.rot90(z.T,1))
from sklearn.model_selection import train_test_split
X=mnist.data/255
print('打印mnist.data:\n',mnist.data,'\n')
print('mnist.data的维度:\n',(mnist.data).shape)
y=mnist.target
X_train,X_test,y_train,y_test=train_test_split(X,y,train_size=5000,test_size=1000,random_state=62)
打印mnist.data:
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
mnist.data的维度:
(70000, 784)
from sklearn.neural_network import MLPClassifier
mlp_hw=MLPClassifier(solver='lbfgs',hidden_layer_sizes=[100,100],activation='relu',alpha=1e-5,random_state=62)
mlp_hw.fit(X_train,y_train)
print('测试数据得分:{:.2f}%'.format(mlp_hw.score(X_test,y_test)*100))
测试数据得分:93.60%
from PIL import Image
image=Image.open('3.jpg').convert('F')
image=image.resize((28,28))
arr=[]
for i in range(28):
for j in range(28):
pixel=1.0-float(image.getpixel((j,i)))/255
arr.append(pixel)
arr1=np.array(arr).reshape(1,-1)
print("图片中的数字是:{:.0f}".format(mlp_hw.predict(arr1)[0]))
图片中的数字是:3
image=Image.open('汉.jpg').convert('F')
image=image.resize((28,28))
x=np.arange(-14,14,1)
xx,yy=np.meshgrid(x,x)
arr=[]
for i in range(28):
for j in range(28):
pixel=image.getpixel((j,27-i))
arr.append(pixel)
arr_=np.array(arr)
print(arr_)
z=arr_.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx,yy,z)
[207.7539978 210.98199463 213.03599548 216.92599487 220.76699829
222.66299438 225.64599609 229.31900024 228.94900513 232.70399475
234.68699646 236.1210022 236.40899658 237.81100464 237.76199341
234.76199341 234.66499329 232.8500061 228.8500061 229.06300354
225.07400513 228.1190033 221.34300232 220.022995 213.92599487
215.11099243 214.96499634 209.0039978 209.7539978 210.98199463
212.96499634 215.86099243 219.76699829 220.66299438 226.1190033
226.09100342 231.70399475 231.70399475 210.75300598 236.60499573
238.1210022 238.1210022 238.32699585 235.03900146 235.13800049
232.8500061 230.8500061 229.06300354 226.74299622 224.16200256
222.34300232 220.022995 216.86599731 158.18499756 210.73699951
210.0039978 210.76800537 210.91099548 212.11099243 193.69599915
220.65299988 221.66299438 224.1190033 228.102005 229.70399475
232.70399475 234.8500061 224.16000366 236.8500061 236.1210022
235.32699585 230.32699585 218.23699951 237.70399475 228.852005
227.09100342 221.13000488 204.69599915 206.8769989 170.13499451
216.63800049 153.21600342 208.79299927 207.14599609 165.83599854
208.39100647 163.04299927 161.19900513 219.79499817 222.95100403
223.64599609 205.22900391 231.70399475 232.70399475 232.8500061
234.96400452 205.9750061 215.76199341 236.32699585 234.09899902
216.8500061 236.26899719 233.30200195 199.64599609 227.08000183
232.897995 222.78399658 190.8769989 216.81199646 154.93800354
199.39100647 210.06399536 209.91099548 151.875 212.93299866
172.8500061 221.93299866 222.90800476 224.02200317 175.22900391
228.75300598 230.98100281 233.8500061 236.04400635 51.85400009
251.08200073 238.08200073 236.03900146 234.8500061 227.06300354
227.76400757 225.74299622 225.94299316 70.76399994 226.36099243
211.2749939 218.15400696 217.23199463 205.88299561 198.70799255
205.09100342 202.28900146 212.96499634 164.07299805 219.91900635
221.1190033 217.86399841 179.22900391 142.95300293 233.13800049
236.8500061 233.66499329 230.03900146 46.77199936 37.08200073
236.13800049 234.95300293 233.75300598 232.16000366 231.70399475
43.70700073 47.07099915 230.27999878 219.88299561 218.15400696
219.11300659 216.77600098 214.30900574 206.7539978 151.76800537
215.01400757 163.66900635 216.65299988 214.76199341 226.34300232
160.88600159 50.91799927 46. 232.8500061 241.96400452
218.1210022 246.85400391 49.2879982 200.03900146 220.70399475
231.13800049 208.852005 52.81100082 47.08200073 208.80299377
218.73500061 220.05499268 218.15400696 212.92599487 211.03599548
212.37399292 203.98199463 216.38699341 210.80099487 165.82299805
220.34300232 220.66299438 225.64599609 198.95100403 47.
47. 237.40899658 233.60499573 234.18099976 238.79400635
56.08200073 47.16400146 208.94900513 213.72099304 142.98100281
45.68999863 50.04399872 180.14399719 222.11599731 218.05499268
219.92599487 214.11099243 214.01499939 208.94599915 207.99899292
210.7539978 213.727005 186.71800232 220.65299988 223.27799988
226.88499451 224.82000732 56.25 46.04899979 46.94200134
238.70399475 215.94200134 235.08200073 237.8710022 47.31000137
202.80299377 46.7120018 174.18099976 231.852005 226.07400513
224.94099426 220.86599731 217.82200623 216.86599731 215.83399963
208.01499939 211.24299622 210.7539978 212.69400024 162.14599609
216.80099487 220.99499512 223.13000488 232.22900391 202.21200562
61.22999954 225.95300293 46.31000137 46.31000137 49.31000137
46.77199936 58.08200073 47.31000137 182.62199402 231.00900269
231.96000671 228.80299377 225.08500671 222.2440033 220.91499329
219.10499573 217.86599731 213.83399963 211.96499634 152.9960022
210.05299377 212.91099548 198.17999268 216.8769989 219.99499512
224.90800476 208.05000305 226.80299377 230.06300354 233.98100281
46.85400009 234.82800293 217.03900146 243.03900146 234.08200073
219.15299988 46.16799927 43.79399872 48.91799927 49.14599991
47.1570015 46.7120018 224.74499512 220.01199341 219.15400696
215.83399963 212.63800049 210.82499695 209.98199463 212.98199463
159.03900146 215.23599243 219.76699829 223.65299988 223.88499451
228.03100586 229.06300354 232.70399475 204.91000366 208.91000366
240.13800049 233.32699585 52.32699966 51.85400009 47.08200073
224.13800049 211.72099304 229.06300354 211.31599426 179.55599976
211.8769989 202.96499634 216.86599731 216.83399963 209.76600647
209.0039978 208.98199463 210.98199463 160.03599548 218.30000305
220.76699829 224.9019928 230.1190033 189.21800232 243.72099304
231.8500061 200.77000427 215.16499329 237.18099976 238.09899902
224.05000305 236.91000366 47. 45.77199936 46.95100021
220.16000366 231.08500671 164.74499512 187.8769989 219.17599487
217.86599731 213.83399963 163.72900391 202.30900574 207.98199463
205.06399536 143.1519928 219.07200623 216.76699829 221.66299438
208.00100708 190.07200623 232.11300659 230.8500061 215.8500061
230.98100281 235.18099976 83.08200073 49.32699966 47.08800125
44.77199936 235.13800049 232.04100037 224.97099304 225.74299622
222.66299438 225.94099426 226.19299316 217.15400696 213.83399963
205.91499329 210.92199707 209.02799988 208.69400024 210.82499695
215.09399414 219.76699829 220.66299438 224.44000244 225.82000732
230.19799805 232.8500061 234.96400452 202.8500061 208.18099976
47.08200073 68. 182.97099304 27.85400009 45.83200073
45.89099884 69.11599731 225.82499695 222.9019928 224.21800232
219.90800476 212.65299988 170.76800537 205.90800476 207.21200562
206.83599854 169.66900635 215.92199707 216.91499329 220.65299988
223.20100403 46.7120018 47. 47.1570015 33.72900009
234.66499329 222.8500061 239.18099976 39.07099915 49.2879982
78.82800293 41.7120018 230.86099243 29.93199921 46.86899948
47.02199936 226.19500732 222.0059967 219.77799988 217.86599731
216.11099243 211.69400024 169.70799255 204.7539978 203.0059967
210.13499451 215.06399536 217.30000305 197.17100525 45.07099915
230.91000366 223.62199402 232.98100281 234.81100464 232.8500061
236.147995 103.32700348 238.32699585 234.87600708 42.85400009
26.72900009 44.02199936 43.87099838 226.08000183 211.09399414
223.09899902 220.022995 216.86599731 216.11099243 218.01499939
201.97399902 211.7539978 211.96499634 144.01300049 192.89399719
219.76699829 223.03300476 230.92399597 226.05499268 214.2440033
237.06300354 229.75300598 234.8500061 237.40899658 238.32699585
199.8710022 47.31000137 47.31000137 75.03900146 232.30200195
230.06300354 227.10800171 199.78399658 217.29200745 214.79499817
216.86599731 213.86599731 212.3769989 210.05299377 210.05299377
208.13499451 218.66000366 214.80099487 219.99499512 224.91900635
222.90800476 224.1190033 211.88000488 234.95300293 235.32699585
239.03900146 233.16999817 238.08200073 93. 49.31000137
222.03900146 47.08200073 64.76200104 227.30200195 228.10800171
195.94099426 182.03700256 220.73500061 216.63800049 213.81199646
206.82600403 190.66900635 207.7539978 193.0039978 154.28399658
219.93200684 219.99499512 224.02200317 228.13000488 212.1190033
229.94299316 248.32699585 47.31000137 47.31000137 46.08200073
45.07099915 47. 49.31000137 45.72900009 43.08200073
47.32699966 225.9980011 227.88000488 223.26100159 183.79299927
219.022995 214.92599487 210.12199402 210.02099609 180.06399536
208.7539978 211.71099854 166.87199402 211.17599487 219.99499512
220.95100403 224.64599609 226.78100586 86.16400146 48.98300171
47.31000137 40.03900146 239.40899658 233.08200073 47.
50. 228.05000305 43.08200073 47.31000137 45.32699966
51.66500092 226.44000244 185.25300598 217.11099243 214.15400696
214.11099243 220.65499878 153.83999634 207.7539978 151.94599915
214.10299683 215.19299316 219.77799988 192.13499451 219.9019928
166.91499329 230.93200684 225.1210022 232.18099976 237.95300293
236.40899658 240.40899658 47. 231.15299988 232.22000122
206.1289978 47.31000137 127.13800049 226.93400574 223.20100403
222.9019928 219.79499817 218.15400696 216.11099243 178.8500061
149.95500183 201.22599792 209.98199463 214.25300598 217.07200623
220.05499268 226.90800476 223.9019928 227.13000488 192.23899841
233.70399475 232.93200684 216.8500061 234.1210022 236.8500061
240.72900391 234.76199341 234.8500061 239.91000366 47.31000137
49.95100021 224.03100586 205.25300598 216.15400696 218.11099243
216.86599731 215.67700195 209.99200439 212.95199585 204.29400635
212.02099609 214.12199402 215.36000061 220.01199341 223.13000488
226.102005 189.55599976 233.88000488 232.70399475 235.86099243
214.70399475 216.95300293 238.03900146 238.05000305 208.66499329
234.9750061 235.96400452 43.85400009 248.17199707 223.04499817
214.94099426 225.92399597 217.11099243 217.86599731 151.83900452
140.05099487 210.71099854 208.11300659 157.40499878 156.02799988
204.92599487 220.99499512 221.26100159 189.21800232 198.08900452
226.96000671 232.70399475 201.80299377 216.16000366 234.60499573
235.81100464 237.72900391 201.26899719 242.23699951 224.93400574
226.80799866 229.06300354 218.08799744 222.30000305 212.27600098
213.88299561 215.92599487 215.00100708 144.06700134 211.33099365
208.08599854 157.91099548 212.04699707 226.03599548 219.99499512
223.02200317 224.82000732 201.30000305 200.23699951 233.06300354
235.13800049 212.75300598 238.16999817 235.81100464 236.08799744
238.98100281 235.16000366 200.2440033 230.70399475 229.06300354
225.05499268 174.95500183 222.34300232 201.67700195 216.63800049
215.19299316 160.19900513 165.22399902 208.7539978 149.33700562
213.11099243 168.67700195 219.76699829 222.82000732 226.89100647
232.1190033 230.23699951 231.8500061 194.75300598 204.70399475
234.89300537 238.03900146 238.1210022 234.60499573 234.86099243
204.1190033 233.94299316 230.06300354 227.21200562 168.16799927
214.95599365 217.01199341 216.63800049 215.83399963 170.66900635
196.28399658 209.03100586 211.67199707 215.63800049 217.09399414
219.15400696 219.98399353 224.1190033 226.96000671 228.72099304
232.99200439 232.96400452 237.18099976 233.1210022 238.03900146
238.03900146 237.09899902 235.1210022 229.93200684 231.06300354
230.06300354 226.852005 229.84199524 226.34300232 187.98699951
217.79499817 214.83399963 212.92199707 204.7539978 ]
x=np.array([1,2,3,4,5,6,7,8,9])
y=np.array([[1,1,2],[1, 2, 1],[1, 1, 1]])
x=x.reshape(y.shape)
print(x)
[[1 2 3]
[4 5 6]
[7 8 9]]
image=Image.open('四色.jpg').convert('F')
x=np.arange(-10,10,1)
xx,yy=np.meshgrid(x,x)
arr=[]
for i in range(20):
for j in range(20):
pixel=image.getpixel((j,19-i))
arr.append(pixel)
arr_=np.array(arr)
z=arr_.reshape(xx.shape)
print(z)
plt.figure()
plt.pcolormesh(xx,yy,z)
[[2.52750000e+02 2.53205994e+02 2.53516006e+02 2.52330994e+02
2.54201996e+02 2.51919998e+02 2.54070999e+02 2.54412994e+02
2.51951004e+02 2.53826004e+02 1.12500000e+00 8.97000015e-01
3.84299994e+00 2.98999995e-01 2.98999995e-01 5.27000010e-01
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.53048996e+02 2.52917999e+02 2.52341995e+02 2.51000000e+02
2.51921997e+02 2.51009995e+02 2.51807999e+02 2.52149994e+02
2.52035995e+02 2.51694000e+02 6.81099987e+00 5.92500019e+00
3.55500007e+00 3.55500007e+00 3.55500007e+00 5.02799988e+00
8.85999978e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.54412994e+02 2.54412994e+02 2.53238998e+02 2.52947998e+02
2.47134003e+02 2.44854004e+02 2.45651993e+02 2.45994003e+02
2.45880005e+02 2.45423996e+02 9.76799965e+00 9.18099976e+00
8.88199997e+00 1.06540003e+01 1.03549995e+01 7.09899998e+00
2.36999989e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.54412994e+02 2.53826004e+02 2.53826004e+02 2.52834000e+02
2.16563995e+02 2.17845001e+02 2.18656006e+02 2.17738007e+02
2.15895004e+02 2.17895004e+02 2.19026001e+02 2.17009003e+02
2.18718994e+02 2.18007004e+02 2.16867004e+02 2.19136002e+02
2.95700002e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.53804001e+02 2.54701004e+02 2.53772003e+02 2.53860001e+02
2.52606003e+02 2.49755997e+02 2.45651993e+02 2.16949005e+02
2.45651993e+02 2.46677994e+02 9.45800018e+00 7.09899998e+00
5.62599993e+00 4.44099998e+00 2.18643997e+02 5.61499977e+00
2.07100010e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.54701004e+02 2.51287994e+02 2.54544006e+02 2.53843002e+02
2.54412994e+02 2.52149994e+02 2.45994003e+02 2.20220001e+02
2.48388000e+02 2.12800995e+02 2.13563995e+02 8.85999966e+00
2.36999989e+00 1.48399997e+00 4.44099998e+00 4.72900009e+00
8.85999978e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.54412994e+02 2.51082001e+02 2.53255997e+02 2.53957001e+02
2.54070999e+02 2.48363007e+02 2.45651993e+02 2.13779007e+02
2.50781998e+02 2.51123993e+02 2.20227997e+02 8.84899998e+00
5.01700020e+00 2.98999995e-01 1.19599998e+00 1.19599998e+00
2.98999995e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.52897003e+02 2.53598007e+02 2.53957001e+02 2.50854004e+02
2.54772003e+02 2.51352005e+02 2.43942001e+02 2.18639008e+02
2.51123993e+02 2.51807999e+02 2.15600006e+02 1.09309998e+01
6.20200014e+00 4.72900009e+00 1.78299999e+00 5.02799988e+00
2.98999995e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.52772003e+02 2.54772003e+02 2.51059998e+02 2.54059998e+02
2.53000000e+02 2.50781998e+02 2.42003998e+02 2.20854004e+02
2.50554001e+02 2.50326004e+02 2.17130997e+02 2.21694000e+02
2.17798004e+02 2.20908005e+02 2.15089005e+02 4.13100004e+00
8.85999978e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.54473007e+02 2.54059998e+02 2.54701004e+02 2.54287994e+02
2.54701004e+02 2.51807999e+02 2.15817993e+02 2.17177002e+02
2.48792999e+02 2.50326004e+02 2.15914001e+02 1.53940001e+01
1.44969997e+01 8.58300018e+00 5.90299988e+00 3.23399997e+00
1.17400002e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.69000006e+00 5.87000012e-01 5.27000010e-01 7.54999995e-01
1.04299998e+00 3.53299999e+00 3.55500007e+00 4.45200014e+00
5.32700014e+00 3.85400009e+00 2.19878006e+02 1.21379995e+01
6.22399998e+00 4.44099998e+00 2.05999994e+00 3.83200002e+00
5.87000012e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[5.87000012e-01 1.28799999e+00 2.17899990e+00 1.46700001e+00
1.04299998e+00 1.40199995e+00 2.00000000e+00 2.98999995e-01
1.48399997e+00 6.21299982e+00 2.16121002e+02 9.46899986e+00
5.03900003e+00 2.36999989e+00 2.35899997e+00 5.87000012e-01
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[5.87000012e-01 2.00000000e+00 1.76600003e+00 1.35300004e+00
1.22800004e+00 5.87000012e-01 1.17400002e+00 1.17400002e+00
2.95700002e+00 5.32700014e+00 2.16294998e+02 1.24150000e+01
6.51200008e+00 3.54399991e+00 1.18499994e+00 7.97999978e-01
2.28000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.07100010e+00 2.98999995e-01 2.28000000e-01 2.98999995e-01
1.48399997e+00 4.72900009e+00 3.24499989e+00 5.01700020e+00
5.61499977e+00 2.19820007e+02 2.14084000e+02 1.06429996e+01
1.06540003e+01 2.18882996e+02 3.25600004e+00 1.98300004e+00
2.28000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[1.19599998e+00 1.18499994e+00 5.87000012e-01 6.77799988e+00
5.02799988e+00 2.13882996e+02 2.20891006e+02 2.19294998e+02
2.17084000e+02 2.17460007e+02 2.21787003e+02 2.14869003e+02
2.17048004e+02 2.17300003e+02 3.26699996e+00 5.87000012e-01
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.09299994e+00 1.19599998e+00 4.69000006e+00 1.76100004e+00
3.85400009e+00 6.22399998e+00 8.58300018e+00 9.75699997e+00
9.74600029e+00 9.15900040e+00 9.15900040e+00 1.06540003e+01
1.00889997e+01 8.01799965e+00 7.69700003e+00 8.85999978e-01
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[3.21799994e+00 1.79400003e+00 2.98999995e-01 2.07100010e+00
1.19599998e+00 2.38100004e+00 4.73999977e+00 7.68599987e+00
3.55500007e+00 3.84299994e+00 2.65799999e+00 2.95700002e+00
5.92500019e+00 2.68000007e+00 8.97000015e-01 2.98999995e-01
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[2.61999989e+00 1.19599998e+00 2.98999995e-01 0.00000000e+00
7.54999995e-01 8.97000015e-01 4.15299988e+00 1.19599998e+00
2.95700002e+00 8.85999978e-01 2.28000000e-01 2.28000000e-01
3.00000000e+00 5.27000010e-01 7.54999995e-01 3.16799998e+00
2.28000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[5.87000012e-01 3.05999994e+00 5.87000012e-01 2.21700001e+00
9.29000020e-01 5.27000010e-01 2.43499994e+00 2.61999989e+00
8.97000015e-01 5.87000012e-01 7.05999994e+00 5.87000012e-01
1.28799999e+00 2.28000000e-01 2.69499993e+00 8.69000018e-01
2.28000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[1.76100004e+00 1.76100004e+00 1.76100004e+00 2.69000006e+00
5.87000012e-01 3.22799993e+00 2.66300011e+00 2.54900002e+00
1.30999994e+00 5.87000012e-01 5.87000012e-01 2.94600010e+00
5.87000012e-01 9.29000020e-01 1.09700000e+00 1.09700000e+00
5.27000010e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
- 附件:
汉.jpg
四色.jpg