NumPy: Arrays

http://docs.scipy.org/doc/numpy/reference/

a = [1, 2]
2*a
[1, 2, 1, 2]
a = [1, 2]
b = [3, 4]
c = a + b
c
[1, 2, 3, 4]
import numpy as np

a = np.array([1, 2])
print(2*a)
b = np.array([3, 4])
print(a + b)
print(type(a))
print(np.shape(a))
print(np.shape(c))
[2 4]
[4 6]
<class 'numpy.ndarray'>
(2,)
(4,)
x =  np.array(range(5))
print(x)
[0 1 2 3 4]
# explizite Typ-Angabe
np.array([1, 2, 3], dtype=complex)
array([1.+0.j, 2.+0.j, 3.+0.j])
# Mehrdimensional arrays
meineListe = [[1,2,3],[4,5,6]]
meinArray = np.array(meineListe)
print(meinArray)
np.shape(meinArray)
[[1 2 3]
 [4 5 6]]
(2, 3)
type(meinArray)
# ndaray steht für: N-dimensional array
numpy.ndarray
# Array initalisieren mit 0
nullArray = np.zeros((2,4))
print(nullArray)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
# Array initalisieren mit 1
einserArray = np.ones((2,4))
print(einserArray)
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]]
# Array initalisieren mit Zufallswerten
randomArray = np.empty([2, 3])
print(randomArray)
[[0. 0. 0.]
 [0. 0. 0.]]
# Array initalisieren mit Zufallswerten
np.empty([2, 2])
array([[4.9e-324, 9.9e-324],
       [1.5e-323, 2.0e-323]])
# numpy.arange([start], stop[, step], dtype=None)

a = np.arange(5, 10, 0.5)
print(a)
[5.  5.5 6.  6.5 7.  7.5 8.  8.5 9.  9.5]
# Array initalisieren mit Werten zw. 0 to 1
randomArray2 = np.random.random((4,3))
print(randomArray2)
[[0.0067767  0.45884586 0.43208584]
 [0.15784792 0.97948398 0.5203546 ]
 [0.31923238 0.79828092 0.33487702]
 [0.99323566 0.19165176 0.67402207]]
def pythonsum(n):

    a = list(range(n))
    b = list(range(n))
    c = []

    for i in range(len(a)):
        a[i] = i ** 2
        b[i] = i ** 3
        c.append(a[i] + b[i])

    return c
print(pythonsum(10))
[0, 2, 12, 36, 80, 150, 252, 392, 576, 810]
def numpysum(n):
  a = np.arange(n) ** 2
  b = np.arange(n) ** 3
  c = a + b
  return c
print(numpysum(10))
[  0   2  12  36  80 150 252 392 576 810]
# einzeilig
print(np.arange(1,50))
# mehrzeilig
7*7
multidim = np.arange(1,50).reshape((7,7))
print(multidim)
#print(multidim.shape)
[ 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]
[[ 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]]
# Umformungen
lotto = multidim.copy()
lotto.flatten() # speichert Ergebnis in neuen Speicherbereich
lotto.ravel()   # nutzt gleichen Speicher
lotto.T         # transpose
array([[ 1,  8, 15, 22, 29, 36, 43],
       [ 2,  9, 16, 23, 30, 37, 44],
       [ 3, 10, 17, 24, 31, 38, 45],
       [ 4, 11, 18, 25, 32, 39, 46],
       [ 5, 12, 19, 26, 33, 40, 47],
       [ 6, 13, 20, 27, 34, 41, 48],
       [ 7, 14, 21, 28, 35, 42, 49]])
# Zugriff auf Werte und Teile
from random import randint
for tip in range(6):
    print(multidim[randint(0,6),randint(0,6)])
#multidim[4,3]
47
3
26
1
43
11
multidim[:,3:]
array([[ 4,  5,  6,  7],
       [11, 12, 13, 14],
       [18, 19, 20, 21],
       [25, 26, 27, 28],
       [32, 33, 34, 35],
       [39, 40, 41, 42],
       [46, 47, 48, 49]])
durchschnitt = np.average(multidim)
durchschnitt
ueberdurchschnittlich = multidim > durchschnitt
ueberdurchschnittlich
array([[False, False, False, False, False, False, False],
       [False, False, False, False, False, False, False],
       [False, False, False, False, False, False, False],
       [False, False, False, False,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True]])
alle_ueber_druchschnitt = multidim[ueberdurchschnittlich]
alle_ueber_druchschnitt
array([26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
       43, 44, 45, 46, 47, 48, 49])
standardAbweichung = np.std(multidim)
print(standardAbweichung)
14.142135623730951
# Multiplikation

null_array = multidim * np.zeros((7,7))
null_array
array([[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., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0.]])
# Addition

alles_plus_eins = multidim + np.ones((7,7))
alles_plus_eins
array([[ 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.]])
np.append(alles_plus_eins, [51,52,53])
array([ 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.])
# Typecast
alles_plus_eins.astype(int)
array([[ 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]])