logo

Com crear una matriu buida amb NumPy a Python?

El terme matriu buida no té files ni columnes. Una matriu que conté valors que falten té almenys una fila i una columna, igual que una matriu que conté zeros. Python numèric ( NumPy ) proporciona una gran quantitat de característiques i funcions útils per a operacions sobre matrius i matrius numèriques a Python. Si voleu crear una matriu buida amb l'ajuda de NumPy. Podem utilitzar una funció:

    numpy.buit numpy.zeros

1. numpy.empty: Retorna una nova matriu de forma i tipus donats, sense inicialitzar entrades.



Sintaxi: numpy.empty (forma, dtype=float, ordre='C')

Paràmetres:

  • shape :int o tupla d'int, és a dir, forma de la matriu (5,6) o 5.
  • dtype data-type, opcional, és a dir, el tipus de dades de sortida desitjat per a la matriu, per exemple, numpy.int8. Per defecte ésnumpy.float64.
  • ordre{'C', 'F'}, opcional, per defecte: 'C', és a dir, si s'han d'emmagatzemar dades multidimensionals en ordre de fila principal (estil C) o columna principal (estil Fortran) a la memòria.

Comencem amb la funció buida a NumPy tenint en compte un exemple que voleu crear una matriu buida 5 x 5



Exemple 1: Per crear una matriu buida de 5 columnes i 0 fila:

Python 3




elimina la memòria cau npm



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)>

>

>

Sortida:

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

Aquí, la matriu consta de 0 files i 5 columnes, per això el resultat és '[ ]'. Prenguem un altre exemple de funció buida a NumPy tenint en compte un exemple que voleu crear una matriu buida 4 x 2 amb alguns nombres aleatoris.

Exemple 2: Inicialització d'una matriu buida, utilitzant les dimensions/mida esperades:

Python 3


suprimeix l'últim commit git



# 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)>

>

java localdate
>

Sortida:

La matriu és:
[[1.41200958e-316 3.99539825e-306]
[3.38460865e+125 1.06264595e+248]
[1.33360465e+241 6.76067859e-311]
[1.80734135e+185 6.47273003e+170]]

La matriu amb 25 valors aleatoris: [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]

Aquí, definim el nombre de files i columnes perquè la matriu s'ompli amb nombres aleatoris.

2. numpy.zeros : Retorna una nova matriu de forma i tipus determinats, plena de zeros.

Sintaxi: numpy.zeros (forma, dtype=float, ordre='C')

Paràmetres:

  • shape : int o tupla d'int, és a dir, forma de la matriu (5,6) o 5.
  • dtype data-type, opcional, és a dir, el tipus de dades de sortida desitjat per a la matriu, per exemple, numpy.int8. El valor per defecte és numpy.float64.
  • ordre{'C', 'F'}, opcional, per defecte: 'C', és a dir, si s'han d'emmagatzemar dades multidimensionals en ordre de fila principal (estil C) o columna principal (estil Fortran) a la memòria.

Comencem amb la funció de zeros a NumPy tenint en compte un exemple que voleu crear una matriu amb zeros.

Exemple: Per crear una matriu de zeros de 7 columnes i 5 files:

Python 3


java obrint un fitxer



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

>

>

Sortida:

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')>