logo

Wie erstelle ich eine leere Matrix mit NumPy in Python?

Der Begriff leere Matrix hat keine Zeilen und keine Spalten. Eine Matrix, die fehlende Werte enthält, hat mindestens eine Zeile und eine Spalte, ebenso wie eine Matrix, die Nullen enthält. Numerisches Python ( NumPy ) bietet eine Fülle nützlicher Features und Funktionen für Operationen an numerischen Arrays und Matrizen in Python. Wenn Sie mit Hilfe von NumPy eine leere Matrix erstellen möchten. Wir können eine Funktion verwenden:

    numpy.empty numpy.nulls

1. numpy.empty: Es gibt ein neues Array mit gegebener Form und gegebenem Typ zurück, ohne Einträge zu initialisieren.

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



Parameter:

  • Form: int oder Tupel von int, d. h. Form des Arrays (5,6) oder 5.
  • dtype-Datentyp, optional, d. h. gewünschter Ausgabedatentyp für das Array, z. B. numpy.int8. Der Standardwert ist numpy.float64.
  • order{'C', 'F'}, optional, Standard: 'C', d. h. ob mehrdimensionale Daten in Zeilenreihenfolge (C-Stil) oder Spaltenhauptreihenfolge (Fortran-Stil) im Speicher gespeichert werden sollen.

Beginnen wir mit der leeren Funktion in NumPy und betrachten ein Beispiel, in dem Sie eine leere Matrix 5 x 5 erstellen möchten

Beispiel 1: So erstellen Sie eine leere Matrix mit 5 Spalten und 0 Zeilen:

Python3


in.next Java



import> numpy as np> > > x>=> np.empty((>0>,>5>))> print>(>'The value is :'>, x)> > # if we check the matrix dimensions> # using shape:> print>(>'The shape of matrix is :'>, x.shape)> > # by default the matrix type is float64> print>(>'The type of matrix is :'>, x.dtype)>

>

Abrechnungsdeckung
>

Ausgabe:

The value is : [] The shape of matrix is : (0, 5) The type of matrix is : float64>

Hier besteht die Matrix aus 0 Zeilen und 5 Spalten, weshalb das Ergebnis „[ ]“ ist. Nehmen wir ein weiteres Beispiel einer leeren Funktion in NumPy und betrachten ein Beispiel, bei dem Sie eine leere Matrix 4 x 2 mit einigen Zufallszahlen erstellen möchten.

Beispiel 2: Initialisieren eines leeren Arrays mit den erwarteten Abmessungen/Größen:

Python3




# import the library> import> numpy as np> > # Here 4 is the number of rows and 2> # is the number of columns> y>=> np.empty((>4>,>2>))> > # print the matrix> print>(>'The matrix is : '>, y)> > # print the matrix consist of 25 random numbers> z>=> np.empty(>25>)> > # print the matrix> print>(>'The matrix with 25 random values:'>, z)>

>

>

Ausgabe :

So erhalten Sie iPhone-Emojis auf Android

Die Matrix ist:
[[1.41200958e-316 3.99539825e-306]
[3,38460865e+125 1,06264595e+248]
[1,33360465e+241 6,76067859e-311]
[1,80734135e+185 6,47273003e+170]]

Die Matrix mit 25 Zufallswerten: [1,28430744e-316 8,00386346e-322 0,00000000e+000 0,00000000e+000
0,00000000e+000 1,16095484e-028 5,28595592e-085 1,04316726e-076
1,75300433e+243 3,15476290e+180 2,45128397e+198 9,25608172e+135
4.73517493e-120 2.16209963e+233 3.99255547e+252 1.03819288e-028
2.16209973e+233 7.35874688e+223 2.34783498e+251 4.52287158e+217
8,78424170e+247 4,62381317e+252 1,47278596e+179 9,08367237e+223
1.16466228e-028]

Hier definieren wir die Anzahl der Zeilen und Spalten, damit die Matrix mit Zufallszahlen gefüllt wird.

2. numpy.zeros: Es gibt ein neues Array mit gegebener Form und gegebenem Typ zurück, das mit Nullen gefüllt ist.

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

Parameter:

  • Form: int oder Tupel von int, d. h. Form des Arrays (5,6) oder 5.
  • dtype-Datentyp, optional, d. h. gewünschter Ausgabedatentyp für das Array, z. B. numpy.int8. Der Standardwert ist numpy.float64.
  • order{'C', 'F'}, optional, Standard: 'C', d. h. ob mehrdimensionale Daten in Zeilenreihenfolge (C-Stil) oder Spaltenhauptreihenfolge (Fortran-Stil) im Speicher gespeichert werden sollen.

Beginnen wir mit der Nullenfunktion in NumPy und betrachten ein Beispiel, in dem Sie eine Matrix mit Nullen erstellen möchten.

Beispiel: So erstellen Sie eine Nullenmatrix mit 7 Spalten und 5 Zeilen:

Python3


c-Code abs



import> numpy as np> x>=> np.zeros((>7>,>5>))> > # print the matrix> print>(>'The matrix is : '>, x)> > # check the type of matrix> x.dtype>

>

>

Ausgabe :

The matrix is : [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] dtype('float64')>