import numpy as np
linspace
function:
x = np.linspace(0,1,8) x
array([0. , 0.14285714, 0.28571429, 0.42857143, 0.57142857, 0.71428571, 0.85714286, 1. ])
np.set_printoptions(precision=3)
array([0. , 0.143, 0.286, 0.429, 0.571, 0.714, 0.857, 1. ])
x
remains unchanged. It is only the printing of it that is affected.
The array(...)
part of the print reveals that the variable x
is a NumPy array:
type(x)
numpy.ndarray
tolist
:
x.tolist()
[0.0, 0.14285714285714285, 0.2857142857142857, 0.42857142857142855, 0.5714285714285714, 0.7142857142857142, 0.8571428571428571, 1.0]
x_list
, and as a NumPy array, x_array
:
x_list = [1, 4, 9] x_array = np.array(x_list) print('x_list: ',x_list) print('x_array: ',x_array) print('x_array.tolist():',x_array.tolist())
x_list: [1, 4, 9] x_array: [1 4 9] x_array.tolist(): [1, 4, 9]
print('2 * x_list: ',2 * x_list) print('2 * x_array:',2 * x_array)
2 * x_list: [1, 4, 9, 1, 4, 9] 2 * x_array: [ 2 8 18]
x_array + 100
array([101, 104, 109])
np.sqrt
applied to either a Python list or a NumPy array:
np.sqrt(x_list) np.sqrt(x_array)
array([1., 2., 3.])
an_array = np.arange(10000) print(len(an_array)) print(np.sum(an_array)) %timeit np.sum(an_array)
10000 49995000 11.3 µs ± 575 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
a_list = list(an_array) print(len(a_list)) print(sum(a_list)) %timeit sum(a_list)
10000 49995000 1.02 ms ± 5.48 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit
is a so-called magic command of the Jupyter Notebook. It repeats
the following Python statement a great number of
times and evaluates the average time it takes to execute it. The output
obviously depends on your hardware.
import numpy as np a = np.arange(6)*20 a
array([ 0, 20, 40, 60, 80, 100])
a[2]
40
a[:2]
array([ 0, 20])
a[3:]
array([60, 80, 100])
a[-1]
100
a = np.arange(6)*20 a b = (np.arange(2)+1)*30 b np.concatenate( (a,b) )
array([ 0, 20, 40, 60, 80, 100]) array([30, 60]) array([ 0, 20, 40, 60, 80, 100, 30, 60])
np.delete
function that takes as arguments the existing
NumPy array and indices of elements to delete.
a np.delete(a,3) np.delete(a,(1,2)) np.delete(a,range(len(a)-2)) a
array([ 0, 20, 40, 60, 80, 100]) array([ 0, 20, 40, 80, 100]) array([ 0, 60, 80, 100]) array([ 80, 100]) array([ 0, 20, 40, 60, 80, 100])
np.delete
returns a new NumPy array, so if you just
want to delete the element in place, you have to reassign the variable:
a = np.delete(a,range(len(a)-2)) a
array([ 80, 100])
random
,
that provides random numbers.
You get a single random number by calling the
function rand()
from the module:
import numpy as np np.random.rand()
0.4375872112626925
np.random.rand()
0.8917730007820798
np.set_printoptions(precision=3) # only 3 decimals in print np.random.rand(5)
array([0.964, 0.383, 0.792, 0.529, 0.568])
random
module:
np.random.seed(0) np.random.rand(2)
array([0.549, 0.715])
np.random.rand(2)
array([0.603, 0.545])
np.random.seed(0) np.random.rand(2)
array([0.549, 0.715])
np.random.rand()
are uniformly distributed
between 0 and 1. It means that the likelihood of getting
a random number between and is independent on as
long as . Making histograms of 100 or 10000 random numbers
illustrates how a uniform distribution looks like. Writing this:
%matplotlib inline import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() r = np.random.rand(100) ax.hist(r)
r = np.random.rand(10000) ax.hist(r)
a1 = np.arange(10,14) a2 = np.arange(20,24) a12 = np.concatenate((a1,a2)) print(a1) print(a2) print(a12) a = np.concatenate((a12,20+a1)) print(a)
[10 11 12 13] [20 21 22 23] [10 11 12 13 20 21 22 23] [10 11 12 13 20 21 22 23 30 31 32 33]
a = a.reshape(3,4) a
array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]])
shape
attribute:
a.shape
(3, 4)
print(a[0]) print(a[1])
[10 11 12 13] [20 21 22 23]
for row in a: print(row)
[10 11 12 13] [20 21 22 23] [30 31 32 33]
a = np.array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]])
a[0]
, a[1]
, and a[2]
,
one may inspect the individual members of the inner lists, by adding another index in square brackets, e.g. [2]
that will select for the 3rd element in every row:
print(a[0][2]) print(a[1][2]) print(a[2][2])
12 22 32
print
statements effectively select
the 3rd row of the array a
. However, getting three
separate numbers is rather impractical. A more rational way to get the 3rd
coloumn is the following:
print(a[:,2])
[12 22 32]
:
, means that all rows (i.e. elements of the outer list) should be considered, while ,2
means that all 3rd
elements of these rows are selected. One may get inspired and use the :
for the second coordinate, selecting all elements
in a given row:
print(a[1,:]) print(a[1])
[20 21 22 23] [20 21 22 23]
,:
is not required to pick out a row, since [1]
already is sufficient.
a = np.array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]]) a
array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]])
np.transpose(a)
array([[10, 20, 30], [11, 21, 31], [12, 22, 32], [13, 23, 33]])
for col in np.transpose(a): print(col)
[10 20 30] [11 21 31] [12 22 32] [13 23 33]
a
can be gotten like this:
np.transpose(a)[2]
array([12, 22, 32])
transpose
function from the NumPy package is passed an
array as done above, or the transpose
method on a NumPy object is invoked as done here:
a.transpose()[2]
array([12, 22, 32])
flatten
method:
a = np.array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]]) a.flatten()
array([10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33])
transpose
and flatten
, lead to an actual change of the NumPy object,
which maintains its original shape:
a
array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]])
a = a.reshape(12,1) a
array([[10], [11], [12], [13], [20], [21], [22], [23], [30], [31], [32], [33]])
squeeze
method to reduce the dimensionality:
a.squeeze()
array([10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33])
reshape
method:
a
array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]])
a.reshape(12)
array([10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33])
Choose which booklet to go to: