Numpy Vorlesungs-Notebook

Vorlesungs-Notebook

In [1]:
import numpy as np

Die Einträge zweier Listen addieren (ohne Numpy)

In [2]:
x = [1, 2, 3]
y = [4, 5, 6]

result = []
for i, j in zip(x, y):
    result.append(i + j)
print(result)
[5, 7, 9]

-> ein bisschen umständlich

Mit Numpy:

In [3]:
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

result = x + y
print(result)
[5 7 9]
In [4]:
print(x - y)
print(x * y)
print(x ** y)
[-3 -3 -3]
[ 4 10 18]
[  1  32 729]
In [5]:
type(x)
Out[5]:
numpy.ndarray
In [6]:
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
In [7]:
print(matrix)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
In [8]:
matrix + matrix
Out[8]:
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [14, 16, 18]])
In [9]:
matrix + np.array([1, 2, 3])
Out[9]:
array([[ 2,  4,  6],
       [ 5,  7,  9],
       [ 8, 10, 12]])
In [10]:
matrix + np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
Out[10]:
array([[ 2,  4,  6],
       [ 5,  7,  9],
       [ 8, 10, 12]])
In [11]:
arr = np.array([1, 2, 3], dtype=np.int64)
In [12]:
division = matrix / arr
print(division, division.dtype)
ganzzahl_division = matrix // arr
print(ganzzahl_division, ganzzahl_division.dtype)
[[1.  1.  1. ]
 [4.  2.5 2. ]
 [7.  4.  3. ]] float64
[[1 1 1]
 [4 2 2]
 [7 4 3]] int64
In [13]:
mit_nullen_gefüllt = np.zeros((4, 3, 3), dtype=np.int32)
print(mit_nullen_gefüllt)
[[[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 0]]]
In [14]:
# range von Zahlen
range_arr = np.arange(0, 100)
print(range_arr)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]
In [15]:
# Berechne den Sinus im Interval von 0 bis 2 Pi
x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)
In [16]:
print(y.max())
print(y.min())
print(y.mean())
print(y.std())
0.9994862162006879
-0.9994862162006879
-3.788364171964256e-18
0.7000000000000001
In [17]:
def welche_dimension_hat_mein_array(arr):
    shape = arr.shape
    dimension = len(shape)
    return dimension
In [18]:
arr = np.zeros((15, 1, 2, 3, 5, 1,7, 8))
In [19]:
print("Dimension des Arrays:", welche_dimension_hat_mein_array(arr))
Dimension des Arrays: 8
In [20]:
arr = np.array(list("abcdefghiklmnopqrstuvwxyz"))
arr2d = arr[:20].reshape((4, 5))
In [21]:
x = "abcdefghiklmnopqrstuvwxyz"
In [22]:
x[3:5]
Out[22]:
'de'
In [23]:
arr2d[-2, -3:]
Out[23]:
array(['n', 'o', 'p'], dtype='<U1')
In [24]:
arr2d
Out[24]:
array([['a', 'b', 'c', 'd', 'e'],
       ['f', 'g', 'h', 'i', 'k'],
       ['l', 'm', 'n', 'o', 'p'],
       ['q', 'r', 's', 't', 'u']], dtype='<U1')
In [25]:
matrix * np.array([1, 2, 3])
Out[25]:
array([[ 1,  4,  9],
       [ 4, 10, 18],
       [ 7, 16, 27]])
In [26]:
masses = np.array([1, 2, 3])
In [27]:
masses.sum()
Out[27]:
6
In [28]:
position_atom1 = [0, 0, 0]
position_atom2 = [2.5, 4, 1.3]
position_atom3 = [8, -3, 7.5]


masses = np.array([0.5, 1.3, 2.7])


atoms = np.array([position_atom1,
                  position_atom2,
                  position_atom3]
                 )

gewichtete_koordinaten = atoms * masses.reshape((3, 1)) / masses.sum()
In [29]:
gewichtete_koordinaten.sum(axis=0)
Out[29]:
array([ 5.52222222, -0.64444444,  4.87555556])
In [30]:
# noch ein sum Beispiel

print(matrix)
print(matrix.sum(axis=0, keepdims=True))
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[12 15 18]]
In [31]:
print(matrix)
print(matrix.sum(axis=1, keepdims=True))
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[ 6]
 [15]
 [24]]

Monte Carlo

In [32]:
x_0 = 0.5
y_0 = 0.3

x_0**2 + y_0**2 <= 1
Out[32]:
True
In [33]:
gesamt_anzahl_punkte = 10_000

x = np.random.random(gesamt_anzahl_punkte)
y = np.random.random(gesamt_anzahl_punkte)

punkte_drinnen = x**2 + y**2 <= 1
print(punkte_drinnen.dtype)
bool
In [34]:
import matplotlib.pyplot as plt
plt.figure().set_size_inches((5, 5))

plt.scatter(x[punkte_drinnen], y[punkte_drinnen], s=2)
plt.scatter(x[~punkte_drinnen], y[~punkte_drinnen], s=2)
plt.show()
In [ ]: