NumPy(十七):Meshgrid函数【应用场景:等高线、SVC中超平面的绘制】

本文详细介绍了Numpy库中的Meshgrid函数,通过实例展示了如何使用该函数创建网格坐标,并结合matplotlib进行可视化。文章还探讨了Meshgrid在等高线绘制和机器学习,特别是SVM超平面绘制中的应用。同时,提供了等高线图和SVM超平面的绘制代码示例,帮助读者深入理解其工作原理。
摘要由CSDN通过智能技术生成

一、Meshgrid函数

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 3)
print("x = ", x)
print("-" * 50)
print("y = ", y)
print("-" * 100)

X, Y = np.meshgrid(x, y)
print("\nX.shape = {0} \nX = \n{1}".format(X.shape, X))
print("-" * 50)
print("Y.shape = {0} \nY = \n{1}".format(Y.shape, Y))
print("-" * 100)

打印结果:

x =  [0.   0.25 0.5  0.75 1.  ]
--------------------------------------------------
y =  [0.  0.5 1. ]
----------------------------------------------------------------------------------------------------

X.shape = (3, 5) 
X = 
[[0.   0.25 0.5  0.75 1.  ]
 [0.   0.25 0.5  0.75 1.  ]
 [0.   0.25 0.5  0.75 1.  ]]
--------------------------------------------------
Y.shape = (3, 5) 
Y = 
[[0.  0.  0.  0.  0. ]
 [0.5 0.5 0.5 0.5 0.5]
 [1.  1.  1.  1.  1. ]]
----------------------------------------------------------------------------------------------------

Process finished with exit code 0

meshgrid函数的运行过程,可以通过下面的示意图来加深理解:
在这里插入图片描述
再者,也可以通过在matplotlib中进行可视化,来查看函数运行后得到的网格化数据的结果

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 3)
print("x = ", x)
print("-" * 50)
print("y = ", y)
print("-" * 100)

X, Y = np.meshgrid(x, y)
print("\nX.shape = {0} \nX = \n{1}".format(X.shape, X))
print("-" * 50)
print("Y.shape = {0} \nY = \n{1}".format(Y.shape, Y))
print("-" * 100)

plt.plot(X, Y, marker='.', color='blue', linestyle='none')
plt.show()

在这里插入图片描述
当然,我们也可以获得网格平面上坐标点的数据,如下:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 3)
print("x = ", x)
print("-" * 50)
print("y = ", y)
print("-" * 100)

X, Y = np.meshgrid(x, y)
print("\nX.shape = {0} \nX = \n{1}".format(X.shape, X))
print("-" * 50)
print("Y.shape = {0} \nY = \n{1}".format(Y.shape, Y))
print("-" * 100)

z = [i for i in zip(X.flat, Y.flat)]
print("z = {0}".format(z))

打印结果:

x =  [0.   0.25 0.5  0.75 1.  ]
--------------------------------------------------
y =  [0.  0.5 1. ]
----------------------------------------------------------------------------------------------------

X.shape = (3, 5) 
X = 
[[0.   0.25 0.5  0.75 1.  ]
 [0.   0.25 0.5  0.75 1.  ]
 [0.   0.25 0.5  0.75 1.  ]]
--------------------------------------------------
Y.shape = (3, 5) 
Y = 
[[0.  0.  0.  0.  0. ]
 [0.5 0.5 0.5 0.5 0.5]
 [1.  1.  1.  1.  1. ]]
----------------------------------------------------------------------------------------------------
z = [(0.0, 0.0), (0.25, 0.0), (0.5, 0.0), (0.75, 0.0), (1.0, 0.0), (0.0, 0.5), (0.25, 0.5), (0.5, 0.5), (0.75, 0.5), (1.0, 0.5), (0.0, 1.0), (0.25, 1.0), (0.5, 1.0), (0.75, 1.0), (1.0, 1.0)]

Process finished with exit code 0

二、Meshgrid函数的一些应用场景

Meshgrid函数常用的场景有等高线绘制及机器学习中SVC超平面的绘制(二维场景下)。

1、等高线

在这里插入图片描述

import tensorflow as tf

import matplotlib.pyplot as plt


def func(x):
    """

    :param x: [b, 2]
    :return:
    """
    z = tf.math.sin(x[..., 0]) + tf.math.sin(x[..., 1])

    return z


x = tf.linspace(0., 2 * 3.14, 500)
y = tf.linspace(0., 2 * 3.14, 500)
# [50, 50]
point_x, point_y = tf.meshgrid(x, y)
# [50, 50, 2]
points = tf.stack([point_x, point_y], axis=2)
# points = tf.reshape(points, [-1, 2])
print('points:', points.shape)
z = func(points)
print('z:', z.shape)

plt.figure('plot 2d func value')
plt.imshow(z, origin='lower', interpolation='none')
plt.colorbar()

plt.figure('plot 2d func contour')
plt.contour(point_x, point_y, z)
plt.colorbar()
plt.show()

打印结果:

points: (500, 500, 2)
z: (500, 500)

在这里插入图片描述
在这里插入图片描述

2、SVM中超平面的绘制

在这里插入图片描述




参考资料:
MatLab:meshgrid
Numpy中Meshgrid函数介绍及2种应用场景

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值