numpy

Creating a numpy array


In [1]:
import numpy as np
ax = np.array([1,2,3,4,5])
print(ax,id(ax),len(ax))


[1 2 3 4 5] 4409889936 5

Specifying the type

Useful when reading a text stream directly into a numerical array


In [2]:
x=['1','2','3']
xi = np.array(x,'int')
xf = np.array(x,'float')
xs = np.array(x,'str')
print(xi,xf,xs,sep='\n')


[1 2 3]
[ 1.  2.  3.]
['1' '2' '3']

Basic operations


In [3]:
x = np.array([13,24,21.2,17.6,21.7],'float')
print(x.sum(),x.mean(),x.std(),sep='\n')


97.5
19.5
3.84291555983

Multi-dimensional arrays


In [4]:
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.]]

Indexing


In [5]:
ax[1,3] #indexing


Out[5]:
13.0

Slicing


In [6]:
ax[1:3,2:4]
#ax[:,2:]


Out[6]:
array([[ 12.,  13.],
       [ 22.,  23.]])

Reshaping


In [7]:
print(ax.shape)
ax.reshape(9,2)
#ax.reshape(10,3)


(3, 6)
Out[7]:
array([[  0.,   1.],
       [  2.,   3.],
       [  4.,   5.],
       [ 10.,  11.],
       [ 12.,  13.],
       [ 14.,  15.],
       [ 20.,  21.],
       [ 22.,  23.],
       [ 24.,  25.]])

Creating initialized arrays


In [25]:
ax = np.arange(10)
print(ax)
ay = np.array([np.arange(2,10,2),np.arange(10)])
print(ay)


[0 1 2 3 4 5 6 7 8 9]
[array([2, 4, 6, 8]) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]

In [9]:
ax = np.ones(10)
print(ax)


[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]

In [10]:
ax = np.arange(10)**2
print(ax)


[ 0  1  4  9 16 25 36 49 64 81]

In [11]:
np.identity(10)


Out[11]:
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.]])

Matrix multiplication


In [12]:
ax = np.arange(10)
ay = np.array([ax,ax])
#Scalar multiplication
ay*2


Out[12]:
array([[ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18],
       [ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18]])

In [13]:
np.dot(ay,ay.reshape(10,2)) #Dot product


Out[13]:
array([[220, 265],
       [220, 265]])

Comparing numpy arrays with lists


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


[[  0   1   4   9  16  25  36  49  64  81]
 [  0   1   8  27  64 125 216 343 512 729]]
[[  0   0]
 [  1   1]
 [  4   8]
 [  9  27]
 [ 16  64]
 [ 25 125]
 [ 36 216]
 [ 49 343]
 [ 64 512]
 [ 81 729]]
Out[15]:
array([[ 15333, 120825],
       [120825, 978405]])

Functionalize this


In [16]:
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)


Out[16]:
datetime.timedelta(0, 0, 16)

Do the same with python lists


In [30]:
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)


Out[30]:
datetime.timedelta(0, 0, 11155)

In [31]:
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')


10	0:00:00.000027	0:00:00.000017
100	0:00:00.000015	0:00:00.000064
1000	0:00:00.000010	0:00:00.000727
10000	0:00:00.000094	0:00:00.009134

Selecting elements from an np array


In [32]:
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[32]:
array([[1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0]])

In [20]:
#linalg, a linear algebra module
#functions dealing with polynomials, differentials, etc

In [21]:
import scipy
scipy.nanmean(x)


Out[21]:
12.5

Random number support in numpy


In [ ]:
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))

In [49]:
Z = np.random.random(10)

In [50]:
print(Z)


[ 0.65212175  0.07971864  0.72846868  0.6889183   0.71662985  0.37784052
  0.11910494  0.93122747  0.47815458  0.36542629]

In [53]:
Z = Z.view().sort(axis=0)

In [54]:
print(Z)


None

In [57]:
a = np.arange(12)
b = a
c = a.view()
d = a.copy()
print(id(a),id(b),id(c),id(d))


4421157712 4421157712 4421157872 4421168624

In [58]:
print(a,b,c,d,sep='\n')


[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11]

In [59]:
d[0]=1

In [60]:
print(b)


[ 0  1  2  3  4  5  6  7  8  9 10 11]

In [77]:
Z = np.random.random(10)
#Z.sort()
#print(Z)
Z = np.sort(Z)
print(Z)


[ 0.01083607  0.30515308  0.56858203  0.57043603  0.68314802  0.73209212
  0.74254179  0.83849308  0.85256966  0.96569611]

In [123]:
Z = np.random.uniform(0,1,10)
print(Z)
q = 0.5 
print(np.abs(Z-q))
print(np.min(np.abs(Z-q)))
print(np.argmin(np.abs(Z-q)))
#m = np.argmin(np.abs(Z-q))
m = Z.flat[np.abs(Z-q).argmin()]
print(m)


[ 0.01897367  0.53155186  0.40343722  0.23426815  0.50469805  0.23194347
  0.95251334  0.35704683  0.68932607  0.43236506]
[ 0.48102633  0.03155186  0.09656278  0.26573185  0.00469805  0.26805653
  0.45251334  0.14295317  0.18932607  0.06763494]
0.00469804695729
4
0.504698046957

In [ ]: