import numpy as np
= np.array([1, 2, 3, 4, 5]); type(x) x
numpy.ndarray
Programming with Python
.py
files containing Python code. . .
We can import entire modules or individual functions, classes or variables.
random
provides functions for random numbersos
allows interaction with the operating systemcsv
is used for reading and writing CSV filesre
is used for working with regular expressionspip install <package_name>
. . .
Virtual environments are not that important for you right now, as they are mostly used if you work on several projects with different dependecies at once.
. . .
The name of the package comes from Numerical Python.
C
and C++
. . .
Question: Have you heard of C and C++?
pip install numpy
Tools -> Manage Packages...
import numpy as np
. . .
import numpy as np
= np.array([1, 2, 3, 4, 5]); type(x) x
numpy.ndarray
. . .
You don’t have to use as np
. But it is a common practice to do so.
ndarray
import numpy as np
= np.array([1, 1, 1, 1])
array_from_list print(array_from_list)
[1 1 1 1]
import numpy as np
= np.array((2, 2, 2, 2))
array_from_tuple print(array_from_tuple)
[2 2 2 2]
ndarray
import numpy as np
= np.array(["s", 2, 2.0, "i"])
array_different_types print(array_different_types)
['s' '2' '2.0' 'i']
. . .
But it is mostly not recommended, as it can lead to performance issues. If possible, try to keep the types homogenous.
Improve performance by allocating memory upfront
np.zeros(shape)
: to create an array of zerosnp.random.rand(shape)
: array of random valuesnp.arange(start, stop, step)
: evenly spacednp.linspace(start, stop, num)
: evenly spaced. . .
The shape refers to the size of the array. It can have one or multiple dimensions.
(2)
or 2
creates a 1-dimensional array (vetor)(2,2)
creates a 2-dimensional array (matrix)(2,2,2)
3-dimensional array (3rd order tensor)(2,2,2,2)
4-dimensional array (4th order tensor). . .
import numpy as np
= np.array([1, 2, 3, 4, 5])
x + 1 x
array([2, 3, 4, 5, 6])
Task: Practice working with Numpy:
# TODO: Create a 3-dimensional tensor with filled with zeros
# Choose the shape of the tensor, but it should have 200 elements
# Add the number 5 to all values of the tensor
# Your code here
assert sum(tensor) == 1000
# TODO: Print the shape of the tensor using the method shape()
# TODO: Print the dtype of the tensor using the method dtype()
# TODO: Print the size of the tensor using the method size()
ndarray
works as before. . .
Question: What do you expect will be printed?
import numpy as np
= np.random.randint(0, 10, size=(3, 3))
x print(x); print("---")
print(x[0:2,0:2])
[[3 9 0]
[1 9 9]
[4 0 6]]
---
[[3 9]
[1 9]]
i
: integerb
: booleanf
: floatS
: stringU
: unicode. . .
= np.array(["Hello", "World"]); string_array.dtype string_array
dtype('<U5')
. . .
= np.array([1, 2, 3, 4, 5], dtype = 'f'); print(x.dtype) x
float32
. . .
= np.array([1, 2, 3, 4, 5], dtype = 'f'); print(x.astype('i').dtype) x
int32
. . .
Note, how the types are specified as int32
and float32
.
Question: Do you have an idea what 32
stands for?
. . .
int16
is a 16-bit integerfloat32
is a 32-bit floating point numberint64
is a 64-bit integerfloat128
is a 128-bit floating point number. . .
int8
has to be in the range of -128 to 127int16
has to be in the range of -32768 to 32767. . .
Question: Size difference between int16
and int64
?
concatenate
two join arraysaxis
you can specify the dimensionhstack()
and vstack()
are easier. . .
Question: What do you expect will be printed?
import numpy as np
= np.array((1,1,1,1))
ones = np.array((1,1,1,1)) *2
twos print(np.vstack((ones,twos))); print(np.hstack((ones,twos)))
[[1 1 1 1]
[2 2 2 2]]
[1 1 1 1 2 2 2 2]
sort()
: sort the array from low to highreshape()
: reshape the array into a new shapeflatten()
: flatten the array into a 1D arraysqueeze()
: squeeze the array to remove 1D entriestranspose()
: transpose the array. . .
Try experiment with these methods, they can make your work much easier.
Task: Complete the following task to practice with Numpy:
# TODO: Create a 2-dimensional matrix with filled with ones of size 1000 x 1000.
# Afterward, flatten the matrix to a vector and loop over the vector.
# In each loop iteration, add a random number between 1 and 10000.
# TODO: Now, do the same with a list of the same size and fill it with random numbers.
# Then, sort the list as you have done with the Numpy vector before.
# You can use the `time` module to compare the runtime of both approaches.
import time
= time.time()
start # Your code here
= time.time()
end print(end - start) # time in seconds
1.0967254638671875e-05
And that’s it for todays lecture!
You now have the basic knowledge to start working with scientific computing.
. . .
For more interesting literature to learn more about Python, take a look at the literature list of this course.