Arbeiten mit numpy

Erzeugen eines numpy array

1. Beispiel


In [4]:
import numpy as np
x = np.array([1,2,3,4,5])
#Ausgabe des Arrays, des Speicherortes und der Länge des arrays
print(x,id(x),len(x))


(array([1, 2, 3, 4, 5]), 107646448L, 5)

Erzeugen eines numpy array aus einer Liste

2. Beispiel

  • Bestimmen Sie die Länge des arrays
  • [6,8,3,2,5,4,7,6]
  • Berechnen Sie die Summe und den Mittelwert des arrays
  • 
    
    In [6]:
    y = np.array([6,8,3,2,5,4,7,6])
    print(y)
    print(len(y))
    print(y.sum())
    print(y.mean())
    
    
    
    
    [6 8 3 2 5 4 7 6]
    8
    41
    5.125
    

    Festlegung des type

    Nützlich, wenn man einen Text direkt in ein numerisches array streamed

    
    
    In [7]:
    import numpy as np
    x=['1','2','3','7','8','9']
    xi = np.array(x,'int')
    xf = np.array(x,'float')
    xs = np.array(x,'str')
    print(xi)
    print(xf)
    print(xs)
    #print(xi,xf,xs,sep='\n')#python 3.0
    
    
    
    
    [1 2 3 7 8 9]
    [ 1.  2.  3.  7.  8.  9.]
    ['1' '2' '3' '7' '8' '9']
    

    Grundlegende Operationen

  • Summe sum>()
  • Mittelwert mean()
  • Standardabweichung std()
  • Varianz var()
  • Minimum min()
  • Maximum max()
  • Kumulierte Summe cumsum()
  • 
    
    In [8]:
    x = np.array([13,24,21.2,17.6,21.7],'float')
    print(x.sum(),x.mean(),x.std(), x.cumsum())
    
    
    
    
    (97.500000000000014, 19.500000000000004, 3.8429155598321434, array([ 13. ,  37. ,  58.2,  75.8,  97.5]))
    

    Mehr-dimensionale arrays

    
    
    In [9]:
    x=[[0,1,2,3,4,5],[10,11,12,13,14,15],[20,21,22,23,24,25]]
    ax=np.array(x,float)
    print(ax)
    
    
    
    
    [[  0.   1.   2.   3.   4.   5.]
     [ 10.  11.  12.  13.  14.  15.]
     [ 20.  21.  22.  23.  24.  25.]]
    

    Über einen Index Werte auslesen

    
    
    In [10]:
    ax[1,3] #indexing
    
    
    
    
    Out[10]:
    13.0

    Teilbereiche wählen (Slicing)

    
    
    In [11]:
    ax[1:3,2:4]
    #ax[:,2:]
    
    
    
    
    Out[11]:
    array([[ 12.,  13.],
           [ 22.,  23.]])

    Ändern der Form (Reshaping)

    
    
    In [12]:
    print(ax.shape)
    ax.reshape(9,2)
    #ax.reshape(10,3)
    
    
    
    
    (3L, 6L)
    
    Out[12]:
    array([[  0.,   1.],
           [  2.,   3.],
           [  4.,   5.],
           [ 10.,  11.],
           [ 12.,  13.],
           [ 14.,  15.],
           [ 20.,  21.],
           [ 22.,  23.],
           [ 24.,  25.]])

    Erzeugen initialisierter arrays

    
    
    In [13]:
    ax = np.arange(10)
    print(ax)
    ay = np.array([np.arange(10),np.arange(10)])
    print(ay)
    
    
    
    
    [0 1 2 3 4 5 6 7 8 9]
    [[0 1 2 3 4 5 6 7 8 9]
     [0 1 2 3 4 5 6 7 8 9]]
    
    
    
    In [14]:
    ax = np.ones(10)
    print(ax)
    
    
    
    
    [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
    
    
    
    In [15]:
    ax = np.arange(10)**2
    print(ax)
    
    
    
    
    [ 0  1  4  9 16 25 36 49 64 81]
    
    
    
    In [16]:
    np.identity(10)
    
    
    
    
    Out[16]:
    array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]])

    Matritzen Multiplikation

    
    
    In [18]:
    ax = np.arange(10)
    ay = np.array([ax,ax])
    #Skalar multiplikation
    ay*2
    
    
    
    
    Out[18]:
    array([[ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18],
           [ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18]])
    
    
    In [19]:
    np.dot(ay,ay.reshape(10,2)) #Dot product
    
    
    
    
    Out[19]:
    array([[220, 265],
           [220, 265]])

    Vergleich von numpy arrays mit listen

    
    
    In [ ]:
    n=10
    ax = np.array([np.arange(n)**2,np.arange(n)**3])
    ay = ax.transpose()
    #print(ax)
    #print(ay)
    np.dot(ax,ay)
    

    Functionalize this

    
    
    In [ ]:
    def dotproduct(n):
        ax = np.array([np.arange(n)**2,np.arange(n)**3])
        ay = ax.transpose()
        import datetime
        start = datetime.datetime.now()
        np.dot(ax,ay)
        end = datetime.datetime.now()
        return end-start
        
    dotproduct(10)
    

    Do the same with python lists

    
    
    In [ ]:
    def dot_product_lists(n):
        x = [x**2 for x in range(n)]
        y = [x**3 for x in range(n)]
        ax = [x,y]
        ay = [list(i) for i in zip(*ax)]
        import datetime
        start = datetime.datetime.now()
        [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*ay)] for X_row in ax]
        end = datetime.datetime.now()
        return end-start
        
    dot_product_lists(10000)
    
    
    
    In [ ]:
    for n in [10,100,1000,10000]:
        numpy_result = dotproduct(n)
        list_result = dot_product_lists(n)
        print(n,numpy_result,list_result,sep='\t')
    

    Auswahl von Elementen aus einem numpy array

    
    
    In [21]:
    x=[[0,1,2,3,4,5],[10,11,12,13,14,15],[20,21,22,23,24,25]]
    ax=np.array(x,float)
    np.where(ax%2==0,1,0)
    
    
    
    
    Out[21]:
    array([[1, 0, 1, 0, 1, 0],
           [1, 0, 1, 0, 1, 0],
           [1, 0, 1, 0, 1, 0]])
    
    
    In [22]:
    #linalg, a linear algebra module
    #functions dealing with polynomials, differentials, etc
    
    
    
    In [23]:
    import scipy
    scipy.nanmean(x)
    
    
    
    
    Out[23]:
    12.5

    Unterstützung von Zufallszahlen in numpy

    
    
    In [24]:
    np.random.normal(size=10)
    #np.random.normal(size=(100,100))
    #np.random.exponential()
    #np.random.exponential(1.0,size=(6,3))
    #np.random.randint(-10,10,size=(9,9))
    
    
    
    
    Out[24]:
    array([-1.52471679, -0.05601817, -0.51855536,  1.02650137,  1.35791375,
           -0.45170683, -0.86650886,  0.90444694, -0.13974108, -0.85290045])

    numpy - mehrdimensionale arrays

  • Welches Ergebnis liefert a[2,3,4] in folgendem array a
  • ! Beachten Sie dass Indizes mit 0 beginnen !
  • 
    
    In [25]:
    import numpy as np
    a=np.arange(120).reshape(6,4,5)
    print(a)
    
    
    
    
    [[[  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]]
    
     [[100 101 102 103 104]
      [105 106 107 108 109]
      [110 111 112 113 114]
      [115 116 117 118 119]]]
    
    
    
    In [26]:
    a[2,3,4]
    
    
    
    
    Out[26]:
    59
    
    
    In [27]:
    a[0,0,0]
    
    
    
    
    Out[27]:
    0
    
    
    In [28]:
    a[5,3,4]
    
    
    
    
    Out[28]:
    119

    Welches Ergebnis liefert a[:, 3, 4] in diesem array ?

    
    
    In [29]:
    a[:,3,4]
    
    
    
    
    Out[29]:
    array([ 19,  39,  59,  79,  99, 119])

    Welches Ergebnis liefert a[:, :, 4] in diesem array ?

    
    
    In [30]:
    a[:,:,4]
    
    
    
    
    Out[30]:
    array([[  4,   9,  14,  19],
           [ 24,  29,  34,  39],
           [ 44,  49,  54,  59],
           [ 64,  69,  74,  79],
           [ 84,  89,  94,  99],
           [104, 109, 114, 119]])

    Welches Ergebnis liefert a[5, 1:, 1:] in diesem array ?

    
    
    In [31]:
    a[5,1:,1:]
    
    
    
    
    Out[31]:
    array([[106, 107, 108, 109],
           [111, 112, 113, 114],
           [116, 117, 118, 119]])

    Füllen Sie eine vergleichbares array mit normalverteilten Zufallszahlen (mu = 0, sigma =1) (ohne seed)

    Was liefert s[3,:2,:3] zurück ?

    
    
    In [33]:
    mu = 0
    sigma = 1
    s = np.random.normal(mu, sigma, 120)
    s = s.reshape(6,4,5)
    print(s)
    
    
    
    
    [[[ 0.14621811  1.62514303  0.77170545 -1.92737137  0.27017053]
      [-0.83083151 -0.38978772 -1.49163403  0.41503647 -0.87427506]
      [-0.65057346  1.44684205  0.0458341  -0.16612804  0.13876456]
      [-0.5588333  -0.66969525 -0.53420154 -1.58819261 -1.48215201]]
    
     [[-0.73039434  1.37945457 -0.69627526  0.45676297 -1.15947467]
      [-1.57221187  1.37308231  0.10990095 -0.36766776 -0.40440983]
      [-0.4807761   0.61218628 -0.15024159 -2.72104946  0.86709463]
      [ 1.50982025 -0.49729595 -0.49515749  1.20502167  1.09741339]]
    
     [[-0.69362492 -0.00477715 -1.25047911 -0.11528027  0.48711022]
      [ 1.66435     0.72983061 -0.49824074 -0.79301522  1.43542523]
      [-0.19394884 -3.55017444  1.12924325 -1.2862282  -0.40218222]
      [-0.84642049  1.10106792 -2.78129276  0.10877478  1.25495858]]
    
     [[-0.9306871   1.79529986  1.57592786  0.21849421  0.16335236]
      [ 0.55098577 -0.0095384  -0.85787884 -1.35454582  1.24666132]
      [ 0.12381867 -0.76291356  0.51918997  0.85739078  1.65851309]
      [ 0.82376585 -0.74718322  0.14928738 -1.21519669  1.11017298]]
    
     [[-0.27717094 -1.29981928 -2.16949587 -0.04545267  0.9170314 ]
      [ 0.68818999 -0.21540799 -1.2574809  -1.37609232 -1.36746916]
      [ 2.12057346  0.0983555   0.49569238 -1.2257776   0.93381527]
      [ 0.1535011   0.01442746  0.11141768  0.77811402 -0.00800541]]
    
     [[-0.53092978 -0.84969634  0.52045525  0.74986731 -0.25263846]
      [ 0.75462008  0.33808538  1.49749002  0.77428997 -1.47212878]
      [ 0.33854592  0.1940615  -1.39523031 -0.55075781  1.26845574]
      [ 0.69195909  0.93748587  0.23146517  0.07265208 -0.2732753 ]]]
    
    
    
    In [34]:
    print(s[3,:2,:3])
    
    
    
    
    [[-0.9306871   1.79529986  1.57592786]
     [ 0.55098577 -0.0095384  -0.85787884]]
    

    Füllen Sie eine vergleichbares array mit normalverteilten Zufallszahlen (mu = 0, sigma =1) (seed = 1000) Was liefert s[3,:2,:3] zurück ?

    
    
    In [35]:
    np.random.seed(10000)
    mu = 0
    sigma = 1
    s = np.random.normal(mu, sigma, 120)
    s = s.reshape(6,4,5)
    print(s)
    
    
    
    
    [[[-1.27109064  0.17613707 -0.29621638  0.32692882 -0.97341942]
      [ 0.227681   -0.81932804  0.10722255  0.97527763  0.08197388]
      [-0.47662253 -1.49217154 -0.40346495  0.69477827  0.2121528 ]
      [ 0.09098984 -1.54885465 -0.04482736  1.55676333  1.94711068]]
    
     [[-0.78514011 -2.13921194 -1.26425853  0.29498718  0.80648927]
      [ 0.27767032 -1.24384504  0.78727437  0.75203041  0.17298001]
      [-0.73783565  0.96919093 -0.64637056  1.81109463 -0.99044486]
      [-1.36911291  0.07248481  0.40832729  0.1687738  -0.58670124]]
    
     [[-1.51733757  0.72040884 -2.14986374 -1.23310196 -0.12122475]
      [ 0.68214936  1.26832604 -0.02907836 -0.80203131  0.67907827]
      [-1.04111161  0.93386042  0.34352489  1.78771666  0.48640788]
      [ 0.46039938 -0.83387849  0.04040953 -0.09290729 -0.75064297]]
    
     [[-0.37392144  0.3453563   0.49987149 -1.1674225  -0.38700182]
      [-0.96593647 -2.36935061  1.11943353  1.14754071  1.1925147 ]
      [ 0.41127294  0.62608869  0.04526669  0.96049828 -0.29280139]
      [ 0.01127911  0.80587441 -0.866476   -1.04311829  0.57402145]]
    
     [[ 1.07180389 -0.32848638 -0.38128694  0.43285888 -1.85676958]
      [ 0.40907527 -2.19006338  2.6135831   1.00517651  0.86358165]
      [ 1.24250273  1.2676498  -1.70135138  0.67964854  0.56041301]
      [-2.567103    0.63869907 -0.66318587  1.51255843  0.21451128]]
    
     [[ 0.06944245  0.49807934 -0.50392124 -0.7710644   1.3924703 ]
      [-0.73915102 -0.13088607  0.74360133 -0.38177041 -0.15789323]
      [ 2.39360098  0.71233502  0.16225767  1.06413219 -1.14560502]
      [-1.0620317   0.24752527 -0.17114712 -0.41134637  0.66517636]]]
    
    
    
    In [36]:
    s[3,:2,:3]
    
    
    
    
    Out[36]:
    array([[-0.37392144,  0.3453563 ,  0.49987149],
           [-0.96593647, -2.36935061,  1.11943353]])
    
    
    In [37]:
    np.random.seed(10000)
    mu = 0
    sigma = 1
    s = np.random.normal(mu, sigma, 120)
    s = s.reshape(6,4,5)
    print(s[3,:2,:3])
    
    
    
    
    [[-0.37392144  0.3453563   0.49987149]
     [-0.96593647 -2.36935061  1.11943353]]
    

    Nur mit einem seed, bekommen wir nachvollziehbare Zufallszahlen. Jeder die gleichen !

    
    
    In [ ]: