Arrays speichern und laden

Einführung in NumPy

Izzy Weber

Curriculum Manager, DataCamp

RGB-Arrays

rgb = np.array([[[255, 0, 0], [255, 0, 0], [255, 0, 0]],
                [[0, 255, 0], [0, 255, 0], [0, 255, 0]],
                [[0, 0, 255], [0, 0, 255], [0, 0, 255]]])
plt.imshow(rgb)
plt.show()

RGB-Daten mit Rot in der oberen Reihe, Grün in der Mitte und Blau unten aufgetragen

Einführung in NumPy

RGB-Arrays

Ein farbcodierter Codeausschnitt, der zeigt, wie gemischte Farben wie Pink und Gelb in RGB-Daten dargestellt werden Mehrfarbiges 3×3-Farbraster, das mit dem Code im vorherigen Bild erstellt wurde

Einführung in NumPy

Laden von .npy-Dateien

 

Speichere Arrays in diversen Formaten:

  • .csv
  • .txt
  • .pkl
  • .npy
with open("logo.npy", "rb") as f:
    logo_rgb_array = np.load(f)
plt.imshow(logo_rgb_array)
plt.show()

Bild des blauen NumPy-Logos auf weißem Hintergrund

Einführung in NumPy

RGB-Daten untersuchen

red_array = logo_rgb_array[:, :, 0]
blue_array = logo_rgb_array[:, :, 1]
green_array = logo_rgb_array[:, :, 2]

Ein 3D-RGB-Array, das in drei 2D-Arrays aufgeteilt ist: einen roten, einen grünen und einen blauen

Einführung in NumPy

RGB-Daten untersuchen

red_array[1], green_array[1], blue_array[1]
(array([255, 255, 255, ..., 255, 255, 255]),
 array([255, 255, 255, ..., 255, 255, 255]),
 array([255, 255, 255, ..., 255, 255, 255]))
Einführung in NumPy

RGB-Daten aktualisieren

dark_logo_array = np.where(logo_rgb_array == 255, 50, logo_rgb_array)
plt.imshow(dark_logo_array)
plt.show()

Das gleiche NumPy-Logo wie vorher, aber mit einem dunkelgrauen Hintergrund statt einem weißen

Einführung in NumPy

Arrays als .npy-Dateien speichern

with open("dark_logo.npy", "wb") as f:
    np.save(f, dark_logo_array)
Einführung in NumPy

Falls wir Hilfe brauchen...

help(np.unique)
Help on function unique in module numpy:
unique(ar, return_index=False, return_inverse=False, return_counts=False,
        axis=None)

Find the unique elements of an array.

Returns the sorted unique elements of an array. There are three optional
outputs in addition to the unique elements:

* the indices of the input array that give the unique values...
Einführung in NumPy

Ein Screenshot der numpy.org-Dokumentation für np.unique()

Einführung in NumPy

help() mit Methoden

help(np.ndarray.flatten)
Help on method_descriptor: flatten(...)
a.flatten(order='C')

Return a copy of the array collapsed into one dimension.

Parameters
<hr />-------
order : {'C', 'F', 'A', 'K'}, optional
    'C' means to flatten in row-major (C-style) order.
    'F' means to flatten in column-major (Fortran- ...
Einführung in NumPy

Lass uns üben!

Einführung in NumPy

Preparing Video For Download...