Erste Schritte mit Numpy: Lernen Sie die am häufigsten verwendeten Teile von Numpy kennen | von Shiva Kumar Mangina | Juni 2023

0
27


Wenn Sie neu bei Numpy sind, finden Sie hier die am häufigsten verwendeten Numpy-Funktionen, um Ihnen den Einstieg in die Numpy-Bibliothek zu erleichtern.

Foto von Mika Baumeister An Unsplash

Hyperlink zur Numpy-Dokumentation https://numpy.org/doc/stable/

# import numpy
import numpy as np

Erstellen Sie 1D-, 2D- und 3D-Numpy-Arrays

# Create a 1-dimensional NumPy array utilizing np.array()
a1 = np.array([1,2,3])

# Create a 2-dimensional NumPy array utilizing np.array()
a2 = np.array([[1, 2.0, 3.3],
[4, 5, 6.5]])

# Create a three-dimensional Numpy array utilizing np.array()
a3 = np.array([[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])

Verstehen Sie, was die Attribute des Arrays sind

a1.form  # (3,)
a1.ndim # 1
a1.dtype # dtype('int64')
a1.dimension # 3
sort(a1) # numpy.ndarray

In der Pandas-Bibliothek kann ein Numpy-Array verwendet werden

# Import pandas and create a DataFrame out of 1
# of the arrays you have created
import pandas as pd

pd.DataFrame(a2)

# To create prefilled arrays
zeros = np.zeros((10,2))
ones = np.ones((10,2))
zeros, ones

arrangieren

# Returns evenly spaced values inside a given interval.
ar = np.arange(1,100,1)
ar

willkürlich

# Returns random integers at a given vary.
ran = np.random.randint(0,excessive=10,dimension=(7,2))
ran

# Returns random numbers between 0 and 1.
np.random.random((5, 3))

Den Samen setzen

Wenn Sie den Startwert auf eine Konstante setzen, wird immer derselbe Satz von Zufallszahlen generiert. wird verwendet, um für mehrere Iterationen denselben Satz von Stichproben aus den Daten zu erhalten.

# Set the random seed to 42
np.random.seed(42)

# Create a random array of numbers between 0 & 10 of dimension (4, 6)
np.random.randint(0,excessive=10,dimension=(4, 6))

Prüfen Sie, welche Kopie für Sie geeignet ist

h = a.view() # Create a view of the array with the identical information
np.copy(a) # Create a replica of the array
h = a.copy() # Create a deep copy of the array

Über diese Auswahl können Sie einen Teil der Daten auswählen

# Discover the 0'th index of the most recent array you created
arr[0]

# Get the primary 2 values of the primary 2 rows of the most recent array
arr[:2, :2 ]

# will get a all the weather larger than 2 in arr
arr[arr>2]

Erstellen Sie für das Beispiel zwei zufällige Arrays

randy1 = np.random.randint(0,excessive=10,dimension=(3,5))
randy2 = np.random.randint(0,excessive=10,dimension=(3,5))
randy1,randy2

Operationen auf Matrizen


sum = randy1 + randy2 # Aspect sensible sum (Arrays needs to be of similar form)
energy = randy1 ** 2 # Aspect sensible energy
sq. = np.sq.(randy1) # Do the identical factor with np.sq.()

sum,energy,sq.

Skalarprodukt

Die Erklärung zum Dot-Produkt finden Sie hier https://www.mathsisfun.com/algebra/vectors-dot-product.html

# Create two arrays of random integers between 0 to 10
# one in every of dimension (3, 3) the opposite of dimension (3, 2)
dot1 = np.random.randint(0,excessive=10,dimension=(3,3))
dot2 = np.random.randint(0,excessive=10,dimension=(3,2))

dot1,dot2

# Carry out a dot product on the 2 latest arrays you created
dot1.dot(dot2)

Anhäufung

Berechnen Sie die mittleren Max. Min. Mathematischen Operationen

# Discover the imply of the most recent array utilizing np.imply()
imply = np.imply(sq.) # 38.46666666666667
max = np.max(sq.) # 81
min = np.min(sq.) # 0
sum = np.sum(sq.) # 577

Lassen Sie uns die Arrays so manipulieren, wie wir sie brauchen

Umformen

Verleiht einem Array eine neue Kind, ohne seine Daten zu ändern.

# Reshape the most recent array to (3, 5, 1)
reshape = np.reshape(sq.,(3, 5, 1))
reshape

Transponieren

Gibt ein Array mit vertauschten Achsen zurück

# Transpose the most recent array
transpose = sq..T
transpose
# Create two arrays of random integers between 0 to 10
# each of dimension (4, 3)
arr1 = np.random.randint(0,excessive=10,dimension=(3,2))
arr2 = np.random.randint(0,excessive=10,dimension=(3,2))
arr1,arr2

np.array_equal(a, b) # Array-wise comparability returns Boolean
arr1>arr2 # Aspect-wise comparability returns Array
arr1>=arr2 # Aspect-wise comparability returns Array
arr1>7 # Aspect-wise comparability returns Array
arr1==1 # Aspect-wise comparability returns Array

Lasst uns etwas klären

# To type the the array
arr = [1,6,3]

np.type(arr) # array([1, 3, 6])
np.argsort(arr) # array([0, 2, 1])
np.argmin(arr) # 1
np.argmax(arr, axis=0) # 6 (for column axis=0 )

Um die eindeutigen Werte zu erhalten

# To Get Distinctive Values
arr = [1,1,2,2,3,3]
np.distinctive(arr) # array([1, 2, 3])

Konvertieren eines Bildes in ein NP-Array

# Changing a picture in np array

from matplotlib.picture import imread

nature = imread('./nature.jpg')
print(sort(nature)). # <class 'numpy.ndarray'>

nature.form # (682, 1024, 3)

Damit können Sie loslegen und sich mit der Arbeit an den Datensätzen die Hände schmutzig machen.

Hier sind einige weitere Methoden, die es zu erkunden gilt

# Strive these as effectively
b.ravel()
h.resize((2,6))
np.append(h,g)
np.insert(a, 1, 5)
np.delete(a,[1])
np.concatenate((a,d),axis=0)
np.vstack((a,b))

Viel Spaß beim Codieren!!



Source link

HINTERLASSEN SIE EINE ANTWORT

Please enter your comment!
Please enter your name here