import time
import math
xs = range(1000000)
t11 = time.time()
ys = []
for x in xs:
ys.append(math.sqrt(x))
t12 = time.time()
print(t12-t11)
The good example, with numpy:
import numpy as np
xs = np.arange(1000000)
t21 = time.time()
ys = np.sqrt(xs)
t22 = time.time()
print(t22-t21)
The ratio:
print ((t12-t11)/(t22-t21))
Array objets are sets of numbers of same type.
Some definitions:
name | definition |
---|---|
size | total number of elements |
shape | integer tuple giving the number of elements in each dimension |
rank | size of "shape" corresponds to the number of dimensions |
dtype | type of elements in the array |
Some types:
type name | type code |
---|---|
bolean | Bool |
unsigned | UInt8, UInt16, UInt32, UInt64 |
integer | Int8, Int16, Int32 (Int), Int64 |
float | Float32, Float64 (Float) |
complex | Complex32, Complex64 (Complex) |
import numpy as np
from numpy import *
sqrt(3)
np.sqrt()
creating it explicitely:
x = np.array([1,2,3])
x
y = np.array([[1,2],[3,4],[5,6]],dtype=float)
y
x.size
y.shape
x.dtype
y.size
y.shape
y.dtype
creating from a python list:
np.asarray((1,2,3))
creating from building function:
np.zeros(3)
np.zeros((5,5))
np.ones((2,3),float)
np.arange(10)
np.identity(10)
np.linspace(0,np.pi,10)
creating from a function:
def x2(x):
return x*x
def xy(x,y):
return x*y
np.fromfunction(x2,(5,))
np.fromfunction(xy,(5,5))
change dimension:
x = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
x
np.reshape(x,(2,6))
x.shape = (6,2)
x
y = np.reshape(x,(3,4))
y
np.reshape(y,(12,))
y
casting:
x = np.array([1,2,3],float)
x
y = np.array([1,2,3],int)
y
(x+y).dtype
Retreieve an element or a group of elements:
a = np.arange(12)
a
a[0]
a[1]=-a[1]
a
a[:]
a[2:4] = [-2,-3]
a
a[5:]
a.shape = (3,4)
a
a[2,0]
a[0]
a[0,:]
a[:,0]
a[1:,1:]
a[1:,1:] = np.ones((2,3))
a
Using a list of indexes:
a = np.arange(12)**2
a
index = [0,4,3]
a[index]
a[np.arange(3)]
another example:
a = np.reshape(a,(3,4))
a
ind1 = [1,2]
ind2 = [0,3]
a[ind1,ind2]
and another one:
ind1 = np.array([[1,2],[0,2]])
ind1
ind2 = np.array([[0,3],[1,2]])
ind2
a[ind1,ind2]
nan, inf:
x = np.array([0,1],float)
y = x/0.
y
track special values:
np.isnan(y)
np.isinf(y)
np.isfinite(y)
np.isfinite(x)
replace bad values:
y[np.isnan(y)]=0
y[np.isinf(y)]=1e10
y
Some important methods:
x = np.array([0,4,3,2])
x.min()
x.max()
x.mean()
x.std()
x.argmax()
x.argmin()
x.argsort()
x.sum()
x.conjugate()
Operations elements by elements return an array of same shape.
add (+) | substract (-) | multiply (*) | divide () |
---|---|---|---|
remainder (%) | power (**) | ||
abs | fabs | floor | ceil |
fmod | conjugate | ||
maximum | minimum | ||
cos | sin | arccos | arcsin |
cosh | sinh | arccosh | arcsinh |
tan | arctan | tanh | arctanh |
log | log10 | exp | |
greater (>) | greater_equal(>=) | equal(==) | |
less (<) | less_equal(<=) | not_equal(!=) | |
logical_or | logical_xor | logical_not | logical_and |
bitwise_or () | bitwise_xor (^) | bitwise_not (~) | bitwise_and (&) |
rshift(>>) | lshift(<<) |
Examples:
x = np.array([1,2,3],float)
y = np.array([4,5,6],float)
x/y
x**2
x = np.array([1,2,3])
x
y = np.reshape(np.arange(12),(4,3))
y
sum a vector with a matrix
x+y
y*= 10
y
y+= y
y
compare arrays
x = np.array([1,2,3])
y = np.array([0,2,3],float)
(x>y)
(x==2)
(x>y) | (x==2)
General functions on arrays:
Example: replace specific values in a vectors:
x = np.arange(16)
x = np.reshape(x,(4,4))
x
y = np.fmod(x,3)
y
np.where((y==0),-1,y)
Replace values using a mask:
x = np.arange(16)
x = np.reshape(x,(4,4))
x
mask = np.where((y==0),1,0)
mask
np.putmask(x,mask,y)
x
Example: remove bad values in a vector:
x = np.array([0,1,2,3,4,5],float)
y = np.array([1,0,3,0,5,2],float)
ly = np.log10(y)
ly
c = (y!=0)
c
x = np.compress(c,x)
x
y = np.compress(c,y)
y
ly = np.log10(y)
ly
Other very usefull functions:
ravel:
x = np.identity(3)
x
np.ravel(x)
x = np.array([1,2,3])
y = np.array([4,5,6])
concatenate:
z = np.concatenate((x,y))
z
clip:
np.clip(z,2,5)
sum:
np.sum(z)
Matrix operations: transpose, diagonal, trace, dot product:
a = np.array([1,2])
b = np.array([4,5])
np.dot(a,b)
Polynomials:
from numpy import poly1d
p1 = poly1d([3,4,5])
p2 = poly1d([1,1,1])
print(p1)
print(p1+p2)
print(p1.deriv())
print(p1.integ(k=6))