numpy.clip() i Python

numpy.clip()> funksjonen brukes til å klippe (begrense) verdiene i en matrise.

Gitt et intervall, blir verdier utenfor intervallet klippet til intervallkantene. For eksempel, hvis et intervall på [0, 1] er spesifisert, blir verdier mindre enn 0 0, og verdier større enn 1 blir 1.

Syntaks: numpy.clip(a, a_min, a_max, out=Ingen)

Parametere:
en : Array som inneholder elementer som skal klippes.
a_min : Minimumsverdi.
–> Hvis Ingen, utføres ikke klipping på nedre intervallkant. Ikke mer enn én av a_min og a_max kan være Ingen.
a_max : Maksimal verdi.
–> Hvis Ingen, utføres ikke klipping på øvre intervallkant. Ikke mer enn én av a_min og a_max kan være Ingen.
–> Hvis a_min eller a_max er array_like, vil de tre arrayene bli kringkastet for å matche formene deres.
ut: Resultatene vil bli plassert i denne matrisen. Det kan være inngangsmatrisen for klipping på plass. ut må ha riktig form for å holde utgangen. Dens type er bevart.

Komme tilbake : klippet_array

Kode #1:




# Python3 code demonstrate clip() function> > # importing the numpy> import> numpy as np> > in_array> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ,> 6> ,> 7> ,> 8> ]> print> (> 'Input array : '> , in_array)> > out_array> => np.clip(in_array, a_min> => 2> , a_max> => 6> )> print> (> 'Output array : '> , out_array)>

Utgang:

 Input array : [1, 2, 3, 4, 5, 6, 7, 8] Output array : [2 2 3 4 5 6 6 6] 


Kode #2:




# Python3 code demonstrate clip() function> > # importing the numpy> import> numpy as np> > in_array> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ,> 6> ,> 7> ,> 8> ,> 9> ,> 10> ]> print> (> 'Input array : '> , in_array)> > out_array> => np.clip(in_array, a_min> => [> 3> ,> 4> ,> 1> ,> 1> ,> 1> ,> 4> ,> 4> ,> 4> ,> 4> ,> 4> ],> > a_max> => 9> )> print> (> 'Output array : '> , out_array)>

Utgang:

 Input array : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Output array : [3 4 3 4 5 6 7 8 9 9]