logo

Grundlagen von NumPy-Arrays

NumPy steht für Numerical Python. Es handelt sich um eine Python-Bibliothek, die zum Arbeiten mit einem Array verwendet wird. In Python verwenden wir die Liste für das Array, die Verarbeitung ist jedoch langsam. Das NumPy-Array ist ein leistungsstarkes N-dimensionales Array-Objekt und wird in der linearen Algebra, der Fourier-Transformation und bei Zufallszahlenfunktionen verwendet. Es stellt ein Array-Objekt viel schneller bereit als herkömmliche Python-Listen.

Arten von Arrays:

  1. Eindimensionales Array
  2. Mehrdimensionales Array

Eindimensionales Array:

Ein eindimensionales Array ist eine Art lineares Array.



Eindimensionales Array

Beispiel:

Python3
# importing numpy module import numpy as np # creating list list = [1, 2, 3, 4] # creating numpy array sample_array = np.array(list) print('List in python : ', list) print('Numpy Array in python :', sample_array)>




Ausgabe:

List in python : [1, 2, 3, 4] Numpy Array in python : [1 2 3 4]>

Datentyp für Liste und Array prüfen:

Python3
print(type(list_1)) print(type(sample_array))>

Ausgabe:



>

Mehrdimensionales Array:

Daten in mehrdimensionalen Arrays werden in Tabellenform gespeichert.

Was sind 10 von 1 Million?

Zweidimensionales Array

Beispiel:

Python3
# importing numpy module import numpy as np # creating list  list_1 = [1, 2, 3, 4] list_2 = [5, 6, 7, 8] list_3 = [9, 10, 11, 12] # creating numpy array sample_array = np.array([list_1, list_2, list_3]) print('Numpy multi dimensional array in python
', sample_array)>

Ausgabe:

Numpy multi dimensional array in python [[ 1 2 3 4]  [ 5 6 7 8]  [ 9 10 11 12]]>

Notiz: verwenden [ ] Operatoren in numpy.array() für mehrdimensional

Anatomie eines Arrays:

1. Achse: Die Achse eines Arrays beschreibt die Reihenfolge der Indizierung im Array.

Achse 0 = eindimensional

Achse 1 = Zweidimensional

Achse 2 = Dreidimensional

2. Form: Die Anzahl der Elemente entlang jeder Achse. Es stammt aus einem Tupel.

Beispiel:

Python3
# importing numpy module import numpy as np # creating list  list_1 = [1, 2, 3, 4] list_2 = [5, 6, 7, 8] list_3 = [9, 10, 11, 12] # creating numpy array sample_array = np.array([list_1, list_2, list_3]) print('Numpy array :') print(sample_array) # print shape of the array print('Shape of the array :', sample_array.shape)>

Ausgabe:

Numpy array :  [[ 1 2 3 4]  [ 5 6 7 8]  [ 9 10 11 12]] Shape of the array : (3, 4)>

Beispiel:

Python3
import numpy as np sample_array = np.array([[0, 4, 2], [3, 4, 5], [23, 4, 5], [2, 34, 5], [5, 6, 7]]) print('shape of the array :', sample_array.shape)>

Ausgabe:

shape of the array : (5, 3)>

3. Rang: Der Rang eines Arrays ist einfach die Anzahl seiner Achsen (oder Dimensionen).

Das eindimensionale Array hat Rang 1.

Rang 1

Das zweidimensionale Array hat Rang 2.

Versuchen Sie es mit Catch Block Java

Rang 2

4. Datentypobjekte (dtype): Datentypobjekte (dtype) sind eine Instanz von numpy.dtype Klasse. Es beschreibt, wie die Bytes im Speicherblock mit fester Größe, der einem Array-Element entspricht, interpretiert werden sollten.

Beispiel:

Python3
# Import module import numpy as np # Creating the array  sample_array_1 = np.array([[0, 4, 2]]) sample_array_2 = np.array([0.2, 0.4, 2.4]) # display data type print('Data type of the array 1 :', sample_array_1.dtype) print('Data type of array 2 :', sample_array_2.dtype)>

Ausgabe:

Data type of the array 1 : int32 Data type of array 2 : float64>

Eine andere Möglichkeit, ein Numpy-Array zu erstellen:

1. numpy.array() : Das Numpy-Array-Objekt in Numpy heißt ndarray. Wir können ndarray mit erstellen numpy.array() Funktion.

Syntax: numpy.array(Parameter)

Beispiel:

Python3
# import module import numpy as np #creating a array arr = np.array([3,4,5,5]) print('Array :',arr)>

Ausgabe:

Array : [3 4 5 5]>

2. numpy.fromiter() : Die Funktion fromiter() erstellt ein neues eindimensionales Array aus einem iterierbaren Objekt.

Was sind Selektoren in CSS?

Syntax: numpy.fromiter(iterable, dtype, count=-1)

Beispiel 1:

Python3
#Import numpy module import numpy as np # iterable iterable = (a*a for a in range(8)) arr = np.fromiter(iterable, float) print('fromiter() array :',arr)>

Ausgabe:

fromiter()-Array: [ 0. 1. 4. 9. 16. 25. 36. 49.]

Beispiel 2:

Python3
import numpy as np var = 'Geekforgeeks' arr = np.fromiter(var, dtype = 'U2') print('fromiter() array :', arr)>

Ausgabe:

fromiter() Array: [‘G’ ‘e’ ‘e’ ‘k’ ‘f’ ‘o’ ‘r’ ‘g’ ‘e’ ‘e’ ‘k’ ‘s’]

3. numpy.arange() : Dies ist eine integrierte NumPy-Funktion, die gleichmäßig verteilte Werte innerhalb eines bestimmten Intervalls zurückgibt.

Syntax: numpy.arange([start, ]stop, [step, ]dtype=None)

Beispiel:

Python3
import numpy as np np.arange(1, 20 , 2, dtype = np.float32)>

Ausgabe:

array([ 1., 3., 5., 7., 9., 11., 13., 15., 17., 19.], dtype=float32)

4. numpy.linspace() : Diese Funktion gibt gleichmäßig verteilte Zahlen über einen angegebenen Wert zwischen zwei Grenzwerten zurück.

Syntax: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

Beispiel 1:

Python3
import numpy as np np.linspace(3.5, 10, 3)>

Ausgabe:

array([ 3.5 , 6.75, 10. ])>

Beispiel 2:

eine Million in Zahlen
Python3
import numpy as np np.linspace(3.5, 10, 3, dtype = np.int32)>

Ausgabe:

array([ 3, 6, 10])>

5. numpy.empty() : Diese Funktion erstellt ein neues Array mit einer bestimmten Form und einem bestimmten Typ, ohne den Wert zu initialisieren.

Syntax: numpy.empty(shape, dtype=float, order=’C’)

Beispiel:

Python3
import numpy as np np.empty([4, 3], dtype = np.int32, order = 'f')>

Ausgabe:

array([[ 1, 5, 9],  [ 2, 6, 10],  [ 3, 7, 11],  [ 4, 8, 12]])>

6. numpy.ones(): Diese Funktion wird verwendet, um ein neues Array mit einer bestimmten Form und einem bestimmten Typ zu erhalten, das mit Einsen (1) gefüllt ist.

Syntax: numpy.ones(shape, dtype=None, order=’C’)

Beispiel:

Python3
import numpy as np np.ones([4, 3], dtype = np.int32, order = 'f')>

Ausgabe:

array([[1, 1, 1],  [1, 1, 1],  [1, 1, 1],  [1, 1, 1]])>

7. numpy.zeros() : Diese Funktion wird verwendet, um ein neues Array mit einer bestimmten Form und einem bestimmten Typ zu erhalten, das mit Nullen (0) gefüllt ist.

Syntax: numpy.ones(shape, dtype=None)

Beispiel:

Python3
import numpy as np np.zeros([4, 3], dtype = np.int32, order = 'f')>

Ausgabe:

array([[0, 0, 0],  [0, 0, 0],  [0, 0, 0],  [0, 0, 0]])>