How to use meshgrid (numpy), generate a grid array



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding


Release date:2024/9/8         

In Japanese


■Explanation of meshgrid function

Generates a grid array. A similar function is mgrid.

■Examples of meshgrid functions

The following must be written in common for each concrete example. To install numpy, click here.

import numpy as np

Example

x = [1,2,3,4]
y = [10,20,30]
X,Y = np.meshgrid(x,y)
print(X)

  array([[1, 2, 3, 4],
     [1, 2, 3, 4],
     [1, 2, 3, 4]])


print(Y)

  array([[10, 10, 10, 10],
     [20, 20, 20, 20],
     [30, 30, 30, 30]])


X represents the horizontal axis and Y represents the vertical axis.


The advantage of doing this is that it allows you to perform lattice calculations all at once, as shown below.

print(X+Y)

  array([[11, 12, 13, 14],
     [21, 22, 23, 24],
     [31, 32, 33, 34]])



You can also create combinations of axes as follows:

X=X.reshape(12,1) #Rearrange to 12x1
Y=Y.reshape(12,1)
print(np.vstack((X,Y))) #Combine axes horizontally

 array([[ 1, 10],
    [ 2, 10],
    [ 3, 10],
    [ 4, 10],
    [ 1, 20],
    [ 2, 20],
    [ 3, 20],
    [ 4, 20],
    [ 1, 30],
    [ 2, 30],
    [ 3, 30],
    [ 4, 30]])











List of related articles



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding