Let's import it...
In [284]:
import numpy as np
Vectorised
array2 = array1*k+c
Non-vectorised, here with a list
for index in range(len(list1)):
list2[index] = list1[index]*k + c
help(np.mean)
In [220]:
np.lookfor('weighted average')
In [285]:
a1d = np.array([3, 4, 5, 6])
a1d
Out[285]:
In [222]:
a2d = np.array([[10., 20, 30], [9, 8, 7]])
a2d
Out[222]:
In [223]:
print( type( a1d[0] ) )
print( type( a2d[0,0] ) )
In [224]:
type(a1d)
Out[224]:
The core class of NumPy is the ndarray
(homogeneous n-dimensional array).
To find methods or attributes:
a1d. ->tab
In [225]:
try:
a = np.array(1,2,3,4) # WRONG, only 2 non-keyword arguments accepted
except ValueError as err:
print(err)
In [226]:
a = np.array([1,2,3,4]) # RIGHT
In [227]:
np.ndarray([1,2,3,4]) # ndarray is is a low level method. Use np.array() instead
Out[227]:
In [228]:
np.arange(1, 9, 2)
Out[228]:
In [229]:
# for integers, np.arange is same as range but returns an array insted of a list
np.array( range(1,9,2) )
Out[229]:
In [296]:
np.linspace(0, 1, 10) # start, end, num-points
Out[296]:
In [231]:
np.zeros((2, 3))
Out[231]:
By default, the dtype of the created array is float64 but other dtypes can be used:
In [232]:
np.zeros((2,2),dtype=int)
Out[232]:
In [233]:
np.ones((2, 3))
Out[233]:
In [234]:
np.random.rand(4) # uniform in [0, 1]
Out[234]:
In [235]:
np.random.normal(0,1,4) # Gaussian (mean,std dev, num samples)
Out[235]:
In [298]:
np.random.gamma(1,1,(2,2)) # Gamma (shape, scale , num samples)
Out[298]:
meshgrid
function is very useful:
In [237]:
x = np.linspace(-5, 5, 3)
y = np.linspace(10, 40, 4)
x2d, y2d = np.meshgrid(x, y)
print(x2d)
print(y2d)
In [238]:
print(np.transpose(y2d)) # or equivalentely
print(y2d.transpose()) # using the method of y2d
print(y2d.T) # using the property of y2d
In [239]:
a2d
Out[239]:
In [240]:
a2d.ndim
Out[240]:
In [241]:
a2d.shape
Out[241]:
This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.
In [242]:
a2d.size
Out[242]:
Note that size
is not equal to len()
. The latter returns the length of the first dimension.
In [243]:
len(a2d)
Out[243]:
In [306]:
print('array a1d :',a1d)
print('Minimum and maximum :', a1d.min(), a1d.max())
print('Sum and product of all elements :', a1d.sum(), a1d.prod())
print('Mean and standard deviation :', a1d.mean(), a1d.std())
In [314]:
print(a2d)
print('sum :',a2d.sum())
print('sum :',a2d.sum(axis=0))
print('sum :',a2d.sum(axis=1))
In [245]:
np.exp(a/100.)/a
Out[245]:
In [246]:
# Non-vectorised
r=np.zeros(a.shape) # create empy array for results
for i in range(len(a)):
r[i] = np.exp(a[i]/100.)/a[i]
r
Out[246]:
Vectorization is generally faster than a for loop.
But it is not always possible or the most readable:
for i in range(len(a)):
if isprime(i):
r[i] = a[i]
else
r[i]= a[i]+a[i-1]
In [247]:
a = np.arange(10, 100, 10)
a
Out[247]:
In [248]:
a[2:9:3] # [start:end:step]
Out[248]:
In [249]:
a[:3] # last is not included
Out[249]:
In [250]:
a[-2] # negative index counts from the end
Out[250]:
In [251]:
x = np.random.rand(6)
x
Out[251]:
In [252]:
xs = np.sort(x)
xs
Out[252]:
In [253]:
xs[1:] - xs[:-1]
Out[253]:
Create a 2D NumPy array from the following list and assign it to the variable "arr":
In [254]:
# [[2, 3.2, 5.5, -6.4, -2.2, 2.4],
# [1, 22, 4, 0.1, 5.3, -9],
# [3, 1, 2.1, 21, 1.1, -2]]
Can you guess what the following slices are equal to? Print them to check your understanding.
In [255]:
# a[:, 3]
In [256]:
# a[1:4, 0:4]
In [257]:
# a[1:, 2]
NumPy arrays can be indexed with slices, but also with boolean or integer arrays (masks)
In [258]:
a = np.random.randint(1, 100, 6) # array of 6 random integers between 1 and 100
a
Out[258]:
In [259]:
mask = ( a % 3 == 0 ) # Where divisible by 3 (% is the modulus operator).
mask
Out[259]:
In [260]:
a[mask]
Out[260]:
Consider an 4x5 2D array of negative integers:
In [261]:
a = np.arange(-100, 0, 5).reshape(4, 5)
a
Out[261]:
Suppose you want to return an array result
, which has the squared value when an element in array a
is greater than -90
and less than -40
, and is 1 otherwise.
In [262]:
result = np.zeros(a.shape, dtype=a.dtype)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
if a[i, j] > -90 and a[i, j] < -40:
result[i, j] = a[i, j]**2
else:
result[i, j] = 1
result
Out[262]:
But a more less verbose and quicker approach would be:
In [263]:
condition = (a > -90) & (a < -40)
condition
Out[263]:
In [264]:
result[condition] = a[condition]**2
result[~condition] = 1
print(result)
In [265]:
result = np.where(condition, a**2, 1)
print(result)
All operations related to masked arrays live in numpy.ma
submodule.
The simplest example of manual creation of a masked array:
In [266]:
a = np.ma.masked_array(data=[1, 2, 3],
mask=[True, True, False],
fill_value=-999)
a
Out[266]:
Often, a task is to mask array depending on a criterion.
In [267]:
a = np.linspace(1, 15, 15)
In [268]:
masked_a = np.ma.masked_greater_equal(a, 11)
In [269]:
masked_a
Out[269]:
np.linspace
or np.arange
functionsnp.where
function
In [270]:
# Your code:
# arr =
In [271]:
# Your code:
# condition =
In [272]:
# masked_arr = np.ma.masked_where(condition, arr)
# print(masked_arr)
In [273]:
a = np.array([[1, 2, 3], [4, 5, 6]])
In [274]:
print('{} <-- array'.format(a))
print('{} <-- its shape'.format(a.shape))
In [275]:
a.flatten()
Out[275]:
In [315]:
a.repeat(4)
Out[315]:
In [278]:
a.reshape((3, 2))
Out[278]:
In [279]:
print('Old shape: {}'.format(a.shape))
print('New shape: {}'.format(a.reshape((3, 2)).shape))
In [280]:
a[..., np.newaxis].shape
Out[280]:
Generate a 2d array with 5x5. The first value is 0 and it grows left to right and top to bottom in increments on 0.1.
In [281]:
e4 = np.arange(0.,2.5,.1)
e4
Out[281]:
In [282]:
e4.reshape([5,5])
Out[282]:
The fact that NumPy operates on an element-wise basis means that in principle arrays must always match one another's shape. However, NumPy will also helpfully "broadcast" dimensions when possible.