机器学习之画图

关于画图

在现实数字的时候代码如下:

 1 # 形成X,y两个数组
 3 >>> X, y = mnist["data"], mnist["target"] 
 5 >>> X.shape 
 7 (70000, 784)
10 
11 import matplotlib
13 import matplotlib.pyplot as plt
15 some_digit = X[36000]
17 some_digit_image = some_digit.reshape(28, 28)
19 plt.imshow(some_digit_image, cmap = matplotlib.cm.binary,
21 interpolation="nearest")
23 plt.axis("off")
25 plt.show() 

注意reshape参数里面是长和宽,一定要保证长*宽的值是784;否则就会和数据本身不符,运行报错;(28,28)可过,(1, 784)亦可以通过。

imshow是设置图片的展示,cmap意思是color map,颜色方案,binary代表是白底黑字;

关于图谱可以参看:

下图是图谱(摘自http://www.cnblogs.com/denny402/p/5122594.html)但是并没有发现binary,因为是不全。

颜色图谱

描述

autumn

红-橙-黄

bone

黑-白,x线

cool

青-洋红

copper

黑-铜

flag

红-白-蓝-黑

gray

黑-白

hot

黑-红-黄-白

hsv

hsv颜色空间, 红-黄-绿-青-蓝-洋红-红

inferno

黑-红-黄

jet

蓝-青-黄-红

magma

黑-红-白

pink

黑-粉-白

plasma

绿-红-黄

prism

红-黄-绿-蓝-紫-...-绿模式

spring

洋红-黄

summer

绿-黄

viridis

蓝-绿-黄

winter

蓝-绿

参考:

https://matplotlib.org/tutorials/colors/colormaps.html

各种图谱的颜色图谱展示,在这里可以看到binary是白→黑,bone是黑→白

https://matplotlib.org/examples/color/colormaps_reference.html

图片的位移

1 from scipy.ndimage.interpolation import shift
2 
3 some_digit_shift = shift(some_digit.reshape(28, 28), [10, 1], cval=100).reshape(784)
4 some_digit_shift_img = some_digit_shift.reshape(28, 28)
5 plt.imshow(some_digit_shift_img, cmap=matplotlib.cm.binary, interpolation="nearest")
6 plt.axis("off")
7 
8 plt.show()
关键是[10,1],代表指定图形向纵向向下迁移10个单位,向右迁移1个单位
原文地址:https://www.cnblogs.com/xiashiwendao/p/9326169.html