svm之pcolormesh(),scatter()数据可视化详解

plt.pcolormesh(x_x, x_y, y_text, cmap=cm_light)  # pcolormesh(x,y,z,cmap)这里参数代入

x_x对应于x坐标,y_y对应于y坐标,y_test对应于每个坐标点(x_x, y_y)的分类结果。

plt.scatter(x_x, x_y, c=np.squeeze(yy), edgecolor='k', s=50, cmap=cm_dark)  # 样本点

同理x_x对应于x坐标,y_y对应于y坐标,c=np.squeeze(yy)实现根据分类结果的不同,点的颜色不同。
scatter()源代码实现如下

import numpy as np
import matplotlib.pyplot as plt

x1 = np.random.uniform(5, 20, (20, 2))
x2 = np.random.uniform(21, 36, (20, 2))
x_t1 = x1[:, 0].reshape(10, 2)
x_t2 = x2[:, 0].reshape(10, 2)
x_t = np.vstack((x_t1, x_t2))
print(x_t)
y_t1 = x1[:, 1].reshape(10, 2)
y_t2 = x2[:, 1].reshape(10, 2)
y_t = np.vstack((y_t1, y_t2))
y1 = np.ones((2, 10), float)
y = np.zeros((2, 10), float)
y_t1 = np.stack((y.flat, y1.flat), axis=0)
yy = y_t1.reshape(40, 1)
plt.scatter(x_t, y_t, c=np.squeeze(yy))

'''
print(x_t)
print(y_t)
print(np.squeeze(yy))
x_t
[[2 5]
 [0 6]
 [1 5]
 [2 5]
 [1 5]
 [0 5]
 [2 4]
 [2 6]
 [0 4]
 [2 5]
 [2 4]
 [3 4]
 [0 6]
 [1 6]
 [1 4]
 [2 5]
 [2 6]
 [3 4]
 [1 4]
 [1 5]]
 y_t
[[1 4]
 [0 5]
 [1 5]
 [1 4]
 [0 5]
 [0 6]
 [3 5]
 [3 4]
 [0 5]
 [1 5]
 [1 4]
 [1 6]
 [2 6]
 [1 5]
 [3 6]
 [2 5]
 [1 5]
 [1 4]
 [2 6]
 [1 6]]
 np.squezze(yy)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1.
 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
'''
plt.xlim(0, 40)
plt.ylim(0, 40)
plt.xlabel('x')
plt.ylabel('y')
plt.title('cwshi')
plt.grid()
plt.show()

svm之pcolormesh(),scatter()数据可视化详解_第1张图片pcolormesh()源代码实现

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.colors
x = np.random.uniform(5, 20, (20, 2))
x1 = np.random.uniform(21, 36, (20, 2))
x_x1 = x[:, 0].reshape(10, 2)
x_x2 = x1[:, 0].reshape(10, 2)
x_x = np.vstack((x_x1, x_x2))
x_y1 = x[:, 1].reshape(10, 2)
x_y2 = x1[:, 1].reshape(10, 2)
x_y = np.vstack((x_y1, x_y2))
print(x_y.shape)
y0 = np.ones((2, 10), float)
y00 = np.zeros((2, 10), float)
y_t0 = np.stack((y0.flat, y00.flat), axis=0)
yy0 = y_t0.reshape(40, 1)
y1 = np.ones((2, 10000), float)
y = np.zeros((2, 10000), float)
y_t = np.stack((y.flat, y1.flat), axis=0)
yy = y_t.reshape(40000, 1)
x1_min, x1_max = x_x[:, :].min(), x_x[:, :].max()  # 第0列的范围
x2_min, x2_max = x_y[:, :].min(), x_y[:, :].max()  # 第1列的范围
print(x1_min)
print(x1_max)
print(x2_min)
print(x2_max)
x11, x22 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]  # 生成网格采样点
print(x11)
y_text = y_t.reshape(x11.shape)
cm_dark = mpl.colors.ListedColormap(['g', 'b', 'r'])
cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF'])
plt.pcolormesh(x11, x22, y_text, cmap=cm_light)  # pcolormesh(x,y,z,cmap)这里参数代入
plt.scatter(x_x, x_y, c=np.squeeze(yy0), edgecolor='k', s=50, cmap=cm_dark)  # 样本点
plt.xlabel('a')
plt.ylabel('b')
plt.xlim(0, 40)
plt.ylim(0, 40)
plt.title('svm in iris data classification', fontsize=30)
plt.grid()
plt.show()

svm之pcolormesh(),scatter()数据可视化详解_第2张图片

你可能感兴趣的:(sklearn)