Stacking și împărțire

Introducere în NumPy

Izzy Weber

Core Curriculum Manager, DataCamp

Slicing pe dimensiuni

rgb = np.array([[[255, 0, 0], [255, 255, 0], [255, 255, 255]],
                [[255, 0, 255], [0, 255, 0], [0, 255, 255]],
                [[0, 0, 0], [0, 255, 255], [0, 0, 255]]])
red_array = rgb[:, :, 0]
green_array = rgb[:, :, 1]
blue_array = rgb[:, :, 2]
red_array
array([[255, 255, 255],
       [255,   0,   0],
       [0,   0,   0]])
Introducere în NumPy

Împărțirea array-urilor

red_array, green_array, blue_array = np.split(rgb, 3, axis=2)
red_array
array([[[255], [255], [255]],
       [[255], [  0], [  0]],
       [[  0], [  0], [  0]]])
red_array.shape
(3, 3, 1)
Introducere în NumPy

Dimensiuni finale

red_array_2D = red_array.reshape((3, 3))
red_array_2D
array([[255, 255, 255],
       [255,   0,   0],
       [  0,   0,   0]])
red_array_2D.shape
(3, 3)
Introducere în NumPy

Reguli de împărțire a array-urilor

red_array, green_array, blue_array = np.split(rgb, 5, axis=2)
ValueError: array split does not result in an equal division
Introducere în NumPy

Stacking array-uri

  Stacking a două array-uri 2D creează un array 3D

Introducere în NumPy

Vizualizarea datelor de imagine 2D

red_array, green_array, blue_array = np.split(logo_rgb_array, 3, axis=2)
plt.imshow(red_array)
plt.show()

Un grafic al valorilor roșu din logo-ul NumPy

Introducere în NumPy

Stacking array-uri 2D

red_array = np.zeros((1001, 1001)).astype(np.int32)
green_array = green_array.reshape((1001, 1001))
blue_array = blue_array.reshape((1001, 1001)) 
Introducere în NumPy

Stacking array-uri 2D

stacked_rgb = np.stack([red_array, green_array, blue_array], axis=2)
plt.imshow(stacked_rgb)
plt.show()

Logo-ul NumPy cu toate valorile RGB roșu înlocuite cu zero

Introducere în NumPy

Să exersăm!

Introducere în NumPy

Preparing Video For Download...