Scatter简介
Scatter(散点图)由两个变量构成,分别作为散点图的横,竖坐标,通过散点图可以看出变量之间的关系。
如上如,一些基本的相关性,可以分为正相关,负相关等。
Scatter 相关代码
x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5] # 画布:尺寸 p = figure(plot_width=400, plot_height=400) # 画图 p.scatter(x, y, size=20, # screen units 显示器像素单位 # radius=1, # data-space units 坐标轴单位 marker="circle", color="navy", alpha=0.5) # p.circle(x, y, size=20, color="navy", alpha=0.5) # 显示 show(p)
Scatter图 横纵坐标一 一对应。
关于Scatter的应用 绘制数据并分类
import matplotlib.pyplot as plt # for visualisationimport random # for random number generationimport numpy as np # for numerical librariesrandom.seed(242785) # seed the random number generatorsnp.random.seed(64254)w0 = 0 # parameter values used to generate dataw1 = -1.5w2 = 2.5n = 500 # number of training samples to generatemean = [0, 0] # meancov = [[3, 0.5], [0.5, 3]] # covariance matrixx1, x2 = np.random.multivariate_normal(mean, cov, n).T # sample from a multivariate normal distributionz = w1*x1 + w2*x2 + w0 # generate the latent variable zy = np.sign(z) # generate the output y based on the sign of zcdict = {1: 'red', 2: 'blue', 3: 'green'} # colour schemefig, ax = plt.subplots()j = 1for g in np.unique(y):ix = np.where(y == g)ax.scatter(x1[ix], x2[ix], c = cdict[j], label = "y = " + str(g), s = 16, alpha=0.3)j = j + 1ax.legend()plt.xlabel("$x_1$")plt.ylabel("$x_2$")plt.show()