Python NumPy Module Cheatsheet: Unterschied zwischen den Versionen
Aus MattWiki
Matt (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Matt (Diskussion | Beiträge) |
||
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
== NumPy Module == | == Import NumPy Module == | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
import numpy as np | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== NumPy Array == | == NumPy Arrays == | ||
=== Array Definition === | |||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
my_list = [92, 94, 88, 91, 87] | my_list = [92, 94, 88, 91, 87] | ||
Zeile 10: | Zeile 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | === Array from CSV File === | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
csv_file_name = 'test_2.csv' | csv_file_name = 'test_2.csv' | ||
Zeile 16: | Zeile 18: | ||
numpy_array_from_csv_file = np.genfromtxt(csv_file_name, delimiter=',') | numpy_array_from_csv_file = np.genfromtxt(csv_file_name, delimiter=',') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Two-Dimensional Array === | |||
<syntaxhighlight lang="python3"> | |||
two_dim_array = np.array([[1, 2, 3, 4, 5], | |||
[6, 7, 8, 9, 10], | |||
[11, 12, 13, 14, 15]]) | |||
</syntaxhighlight> | |||
The two-dimensional array itself must have outer brackets. Each array must have the same number of elements. | |||
== Operations on NumPy Arrays == | == Operations on NumPy Arrays == | ||
<syntaxhighlight lang="python3"> | |||
numpy_array = np.array([1, 2, 3, 4, 5]) | === Adding or Multiplying === | ||
<syntaxhighlight lang="python3">numpy_array = np.array([1, 2, 3, 4, 5]) | |||
print(numpy_array + 1) | print(numpy_array + 1) | ||
# returns [2, 3, 4, 5, 6] | # returns [2, 3, 4, 5, 6] | ||
print(numpy_array ** 2) | print(numpy_array ** 2) | ||
# returns [1, 2, 9, 16, 25] | # returns [1, 2, 9, 16, 25]</syntaxhighlight> | ||
=== Adding Values from Two Arrays === | |||
<syntaxhighlight lang="python3"> | |||
numpy_array1 = np.array([1, 2, 3, 4, 5]) | |||
numpy_array2 = np.array([6, 7, 8, 9, 10]) | |||
print(numpy_array1 + numpy_array2) | |||
# returns [7, 9, 11, 13, 15] | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Variance == | == Math Functions == | ||
=== Variance === | |||
<syntaxhighlight lang="python3">import numpy as np | <syntaxhighlight lang="python3">import numpy as np | ||
Zeile 35: | Zeile 56: | ||
print('The variance of Microsoft stock dividends in 2023 and 2024 is', variance_dividends_microsoft)</syntaxhighlight> | print('The variance of Microsoft stock dividends in 2023 and 2024 is', variance_dividends_microsoft)</syntaxhighlight> | ||
== Standard Deviation == | === Standard Deviation === | ||
[[Kategorie:Python]] | [[Kategorie:Python]] |
Aktuelle Version vom 28. Februar 2025, 10:29 Uhr
Import NumPy Module
import numpy as np
NumPy Arrays
Array Definition
my_list = [92, 94, 88, 91, 87]
numpy_array_from_list = np.array(my_list)
Array from CSV File
csv_file_name = 'test_2.csv'
numpy_array_from_csv_file = np.genfromtxt(csv_file_name, delimiter=',')
Two-Dimensional Array
two_dim_array = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
The two-dimensional array itself must have outer brackets. Each array must have the same number of elements.
Operations on NumPy Arrays
Adding or Multiplying
numpy_array = np.array([1, 2, 3, 4, 5])
print(numpy_array + 1)
# returns [2, 3, 4, 5, 6]
print(numpy_array ** 2)
# returns [1, 2, 9, 16, 25]
Adding Values from Two Arrays
numpy_array1 = np.array([1, 2, 3, 4, 5])
numpy_array2 = np.array([6, 7, 8, 9, 10])
print(numpy_array1 + numpy_array2)
# returns [7, 9, 11, 13, 15]
Math Functions
Variance
import numpy as np
dividends_microsoft_eur = [0.6358, 0.6308, 0.6364, 0.6854, 0.6876, 0.7016, 0.7016, 0.7906]
variance_dividends_microsoft = np.var(dividends_microsoft_eur)
print('The variance of Microsoft stock dividends in 2023 and 2024 is', variance_dividends_microsoft)