300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 机器学习教学 plt.scatter()绘制散点图

机器学习教学 plt.scatter()绘制散点图

时间:2021-01-29 07:31:50

相关推荐

机器学习教学 plt.scatter()绘制散点图

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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。