机器学习教学 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 visualisation
import random # for random number generation
import numpy as np # for numerical libraries

random.seed(242785) # seed the random number generators
np.random.seed(64254)


w0 = 0 # parameter values used to generate data
w1 = -1.5
w2 = 2.5

n = 500 # number of training samples to generate

mean = [0, 0] # mean
cov = [[3, 0.5], [0.5, 3]] # covariance matrix
x1, x2 = np.random.multivariate_normal(mean, cov, n).T # sample from a multivariate normal distribution

z = w1*x1 + w2*x2 + w0 # generate the latent variable z
y = np.sign(z) # generate the output y based on the sign of z

cdict = {1: 'red', 2: 'blue', 3: 'green'} # colour scheme
fig, ax = plt.subplots()
j = 1
for 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 + 1
ax.legend()
plt.xlabel("$x_1$")
plt.ylabel("$x_2$")
plt.show()

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值