shekhar pandey
3 min readNov 21, 2021

Numpy

Post 02: Meshgrid

Meshgrid is a numpy function that creates 2d rectangular arrays from two 1d arrays.
It provides all possible combinations of 1d array.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["figure.figsize"] = (10,7)
x = np.arange(-3,4,1)
y = np.arange(-5,6,1)
print("X:")
print(x)
print("\nY:")
print(y)
X:
[-3 -2 -1 0 1 2 3]

Y:
[-5 -4 -3 -2 -1 0 1 2 3 4 5]
image.png
xm, ym = np.meshgrid(x,y)plt.plot(xm, ym, 'ro')
plt.show()
png
plt.plot(xm, ym, 'r-')
plt.plot(xm.T, ym.T, 'b-')
plt.show()
png

Concatenate & Split

a = np.arange(12).reshape(3,4)
a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
b = a * 2
b
array([[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])

concatenate horizontally

# hstack takes only one single argument, we put both arrays as a tuple or list
np.hstack((a, b))
array([[ 0, 1, 2, 3, 0, 2, 4, 6],
[ 4, 5, 6, 7, 8, 10, 12, 14],
[ 8, 9, 10, 11, 16, 18, 20, 22]])
np.column_stack([a,b])array([[ 0, 1, 2, 3, 0, 2, 4, 6],
[ 4, 5, 6, 7, 8, 10, 12, 14],
[ 8, 9, 10, 11, 16, 18, 20, 22]])
np.concatenate([a,b], axis=1)array([[ 0, 1, 2, 3, 0, 2, 4, 6],
[ 4, 5, 6, 7, 8, 10, 12, 14],
[ 8, 9, 10, 11, 16, 18, 20, 22]])

concatenate vertically

np.vstack([a,b])array([[ 0,  1,  2,  3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])
np.row_stack([a,b])array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])
np.concatenate([a,b], axis=0)array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])

Splitting array

arr = np.concatenate([a,b])
arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])
# split vertically into two pieces (horizontal cut)
np.vsplit(arr,2)
[array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]),
array([[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]])]
# split horizontally into two pieces (vertical cut)
np.hsplit(arr, 2)
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[ 0, 2],
[ 8, 10],
[16, 18]]),
array([[ 2, 3],
[ 6, 7],
[10, 11],
[ 4, 6],
[12, 14],
[20, 22]])]
# Read data from file , split it into smaller dataasets containing 5 rows each

data = np.genfromtxt(r'./Numpy_Datasets/data.txt')
L = np.vsplit(data, 5)
for i, ds in enumerate(L):
print(f'Dataset {i+1}:')
print(r'-'*30)
print(ds)
np.save(f"./TEMP/dataset_{i+1}.npz", ds)
Dataset 1:
------------------------------
[[ 1. 87. 57.54435]
[ 2. 8. 7.31704]
[ 3. 56. 56.82095]
[ 4. 63. 64.15579]
[ 5. 2. 5.74522]]
Dataset 2:
------------------------------
[[ 6. 45. 19.56758]
[ 7. 43. 39.62271]
[ 8. 47. 34.95107]
[ 9. 2. 9.38692]
[10. 79. 36.41022]]
Dataset 3:
------------------------------
[[11. 67. 49.83894]
[12. 24. 23.47974]
[13. 61. 72.55357]
[14. 85. 39.24693]
[15. 63. 53.6279 ]]
Dataset 4:
------------------------------
[[16. 2. 16.72441]
[17. 29. 37.25533]
[18. 45. 18.78498]
[19. 33. 19.8089 ]
[20. 28. 46.03384]]
Dataset 5:
------------------------------
[[21. 21. 23.7864 ]
[22. 27. 44.42627]
[23. 65. 34.94804]
[24. 61. 53.49576]
[25. 10. 25.98564]]

No responses yet